/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
1
# Copyright (C) 2007 Canonical Ltd
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
2
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
"""An adapter between a Git Branch and a Bazaar Branch"""
19
0.200.261 by Jelmer Vernooij
More formatting fixes.
20
from dulwich.objects import (
21
    Commit,
22
    Tag,
23
    )
24
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
25
from bzrlib import (
26
    branch,
0.200.513 by Jelmer Vernooij
Fix imports.
27
    bzrdir,
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
28
    config,
0.200.446 by Jelmer Vernooij
Support new 'local' argument.
29
    errors,
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
30
    repository,
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
31
    revision,
0.200.82 by Jelmer Vernooij
Support listing tags.
32
    tag,
0.230.1 by Jelmer Vernooij
Support lightweight checkouts.
33
    transport,
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
34
    )
0.200.261 by Jelmer Vernooij
More formatting fixes.
35
from bzrlib.decorators import (
36
    needs_read_lock,
37
    )
38
from bzrlib.trace import (
0.200.342 by Jelmer Vernooij
Report git sha during pull.
39
    is_quiet,
0.200.261 by Jelmer Vernooij
More formatting fixes.
40
    mutter,
41
    )
42
0.200.513 by Jelmer Vernooij
Fix imports.
43
from bzrlib.plugins.git import (
44
    get_rich_root_format,
45
    )
0.200.386 by Jelmer Vernooij
Move config to a separate file, support BranchConfig.username().
46
from bzrlib.plugins.git.config import (
47
    GitBranchConfig,
48
    )
0.200.278 by Jelmer Vernooij
Update branch head appropriately during dpull.
49
from bzrlib.plugins.git.errors import (
0.200.472 by Jelmer Vernooij
Fix printing error when user attempts to push into git.
50
    NoPushSupport,
0.200.278 by Jelmer Vernooij
Update branch head appropriately during dpull.
51
    NoSuchRef,
52
    )
0.200.261 by Jelmer Vernooij
More formatting fixes.
53
0.238.5 by Jelmer Vernooij
Remove old backwards compatibility code.
54
from bzrlib.foreign import ForeignBranch
0.200.388 by Jelmer Vernooij
Support bzr 1.14 as well.
55
0.200.261 by Jelmer Vernooij
More formatting fixes.
56
0.200.648 by Jelmer Vernooij
Fix tag handling when encountering packed refs.
57
def extract_tags(refs):
0.200.462 by Jelmer Vernooij
Import tags when pulling.
58
    ret = {}
59
    for k,v in refs.iteritems():
60
        if k.startswith("refs/tags/") and not k.endswith("^{}"):
61
            v = refs.get(k+"^{}", v)
0.200.648 by Jelmer Vernooij
Fix tag handling when encountering packed refs.
62
            ret[k[len("refs/tags/"):]] = v
0.200.462 by Jelmer Vernooij
Import tags when pulling.
63
    return ret
64
65
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
66
class GitPullResult(branch.PullResult):
67
68
    def _lookup_revno(self, revid):
69
        assert isinstance(revid, str), "was %r" % revid
70
        # Try in source branch first, it'll be faster
71
        return self.target_branch.revision_id_to_revno(revid)
72
73
    @property
74
    def old_revno(self):
75
        return self._lookup_revno(self.old_revid)
76
77
    @property
78
    def new_revno(self):
79
        return self._lookup_revno(self.new_revid)
80
81
0.200.261 by Jelmer Vernooij
More formatting fixes.
82
class LocalGitTagDict(tag.BasicTags):
83
    """Dictionary with tags in a local repository."""
0.200.82 by Jelmer Vernooij
Support listing tags.
84
0.200.89 by Jelmer Vernooij
Support sprouting branches.
85
    def __init__(self, branch):
86
        self.branch = branch
87
        self.repository = branch.repository
0.200.82 by Jelmer Vernooij
Support listing tags.
88
89
    def get_tag_dict(self):
90
        ret = {}
0.200.648 by Jelmer Vernooij
Fix tag handling when encountering packed refs.
91
        for k,v in extract_tags(self.repository._git.get_refs()).iteritems():
0.200.609 by Jelmer Vernooij
Cope with tags pointing at nonexisting objects.
92
            try:
0.200.647 by Jelmer Vernooij
Fix use of packed refs.
93
                obj = self.repository._git[v]
0.200.609 by Jelmer Vernooij
Cope with tags pointing at nonexisting objects.
94
            except KeyError:
95
                mutter("Tag %s points at unknown object %s, ignoring", v, obj)
96
                continue
0.200.194 by Jelmer Vernooij
Look for commit object in heavyweight tags.
97
            while isinstance(obj, Tag):
98
                v = obj.object[1]
0.200.647 by Jelmer Vernooij
Fix use of packed refs.
99
                obj = self.repository._git[v]
0.200.194 by Jelmer Vernooij
Look for commit object in heavyweight tags.
100
            if not isinstance(obj, Commit):
0.200.261 by Jelmer Vernooij
More formatting fixes.
101
                mutter("Tag %s points at object %r that is not a commit, "
102
                       "ignoring", k, obj)
0.200.194 by Jelmer Vernooij
Look for commit object in heavyweight tags.
103
                continue
0.200.180 by Jelmer Vernooij
Simplify tag handling.
104
            ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
0.200.82 by Jelmer Vernooij
Support listing tags.
105
        return ret
106
0.200.86 by Jelmer Vernooij
Clearer error when setting tags.
107
    def set_tag(self, name, revid):
0.200.480 by Jelmer Vernooij
Cope with API changes in Dulwich.
108
        self.repository._git.refs["refs/tags/%s" % name], _ = \
0.200.462 by Jelmer Vernooij
Import tags when pulling.
109
            self.branch.mapping.revision_id_bzr_to_foreign(revid)
0.200.86 by Jelmer Vernooij
Clearer error when setting tags.
110
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
111
0.239.1 by Jelmer Vernooij
Avoid re-connecting to fetch tags we already know.
112
class DictTagDict(LocalGitTagDict):
113
114
115
    def __init__(self, branch, tags):
116
        super(DictTagDict, self).__init__(branch)
117
        self._tags = tags
118
119
    def get_tag_dict(self):
120
        return self._tags
121
122
123
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
124
class GitBranchFormat(branch.BranchFormat):
125
0.200.70 by Jelmer Vernooij
Implement GitBranchFormat.get_format_description.
126
    def get_format_description(self):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
127
        return 'Git Branch'
128
0.243.1 by Jelmer Vernooij
Use foreign branch testing infrastructure.
129
    def network_name(self):
130
        return "git"
131
0.200.82 by Jelmer Vernooij
Support listing tags.
132
    def supports_tags(self):
133
        return True
134
0.243.1 by Jelmer Vernooij
Use foreign branch testing infrastructure.
135
    def get_foreign_tests_branch_factory(self):
136
        from bzrlib.plugins.git.tests.test_branch import ForeignTestsBranchFactory
137
        return ForeignTestsBranchFactory()
138
0.200.246 by Jelmer Vernooij
Cope with API changes in 1.13.
139
    def make_tags(self, branch):
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
140
        if getattr(branch.repository, "get_refs", None) is not None:
141
            from bzrlib.plugins.git.remote import RemoteGitTagDict
142
            return RemoteGitTagDict(branch)
0.200.261 by Jelmer Vernooij
More formatting fixes.
143
        else:
144
            return LocalGitTagDict(branch)
0.200.246 by Jelmer Vernooij
Cope with API changes in 1.13.
145
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
146
0.200.388 by Jelmer Vernooij
Support bzr 1.14 as well.
147
class GitBranch(ForeignBranch):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
148
    """An adapter to git repositories for bzr Branch objects."""
149
0.239.1 by Jelmer Vernooij
Avoid re-connecting to fetch tags we already know.
150
    def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
0.200.82 by Jelmer Vernooij
Support listing tags.
151
        self.repository = repository
0.200.246 by Jelmer Vernooij
Cope with API changes in 1.13.
152
        self._format = GitBranchFormat()
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
153
        self.control_files = lockfiles
0.200.59 by Jelmer Vernooij
Add more tests, fix revision history.
154
        self.bzrdir = bzrdir
0.231.1 by Jelmer Vernooij
Check that regenerated objects have the expected sha1.
155
        super(GitBranch, self).__init__(repository.get_mapping())
0.239.1 by Jelmer Vernooij
Avoid re-connecting to fetch tags we already know.
156
        if tagsdict is not None:
157
            self.tags = DictTagDict(self, tagsdict)
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
158
        self.name = name
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
159
        self._head = None
0.200.630 by Jelmer Vernooij
Fix base url of Git branches - use the working tree path rather than the control directory path.
160
        self.base = bzrdir.root_transport.base
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
161
0.239.8 by Jelmer Vernooij
Support checkouts.
162
    def _get_checkout_format(self):
163
        """Return the most suitable metadir for a checkout of this branch.
164
        Weaves are used if this branch's repository uses weaves.
165
        """
166
        return get_rich_root_format()
167
0.238.3 by Jelmer Vernooij
Remove svn references, prefer git send format when submitting changes against a git branch.
168
    def get_child_submit_format(self):
169
        """Return the preferred format of submissions to this branch."""
170
        ret = self.get_config().get_user_option("child_submit_format")
171
        if ret is not None:
172
            return ret
173
        return "git"
174
0.200.293 by Jelmer Vernooij
Fix branch nicks.
175
    def _get_nick(self, local=False, possible_master_transports=None):
176
        """Find the nick name for this branch.
177
178
        :return: Branch nick
179
        """
180
        return self.name
181
0.200.331 by Jelmer Vernooij
Add stub for setting nick function.
182
    def _set_nick(self, nick):
183
        raise NotImplementedError
184
185
    nick = property(_get_nick, _set_nick)
0.200.293 by Jelmer Vernooij
Fix branch nicks.
186
0.200.412 by Jelmer Vernooij
Implement GitBranch.__repr__.
187
    def __repr__(self):
188
        return "%s(%r, %r)" % (self.__class__.__name__, self.repository.base, self.name)
189
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
190
    def generate_revision_history(self, revid, old_revid=None):
191
        # FIXME: Check that old_revid is in the ancestry of revid
192
        newhead, self.mapping = self.mapping.revision_id_bzr_to_foreign(revid)
193
        self._set_head(newhead)
194
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
195
    def lock_write(self):
196
        self.control_files.lock_write()
197
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
198
    def get_stacked_on_url(self):
199
        # Git doesn't do stacking (yet...)
0.200.631 by Jelmer Vernooij
Raise proper exception in Branch.get_stacked_on_url().
200
        raise errors.UnstackableBranchFormat(self._format, self.base)
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
201
202
    def get_parent(self):
203
        """See Branch.get_parent()."""
0.200.312 by Jelmer Vernooij
Add notes about parent locations.
204
        # FIXME: Set "origin" url from .git/config ?
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
205
        return None
206
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
207
    def set_parent(self, url):
0.200.312 by Jelmer Vernooij
Add notes about parent locations.
208
        # FIXME: Set "origin" url in .git/config ?
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
209
        pass
210
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
211
    def lock_read(self):
212
        self.control_files.lock_read()
213
0.200.432 by Jelmer Vernooij
Support Branch.is_locked, required for loggerhead.
214
    def is_locked(self):
215
        return self.control_files.is_locked()
216
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
217
    def unlock(self):
218
        self.control_files.unlock()
219
220
    def get_physical_lock_status(self):
221
        return False
222
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
223
    @needs_read_lock
224
    def last_revision(self):
225
        # perhaps should escape this ?
0.200.57 by Jelmer Vernooij
Fix more tests.
226
        if self.head is None:
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
227
            return revision.NULL_REVISION
0.200.112 by Jelmer Vernooij
Fix the build.
228
        return self.mapping.revision_id_foreign_to_bzr(self.head)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
229
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
230
    def _basic_push(self, target, overwrite=False, stop_revision=None):
231
        return branch.InterBranch.get(self, target)._basic_push(
232
            overwrite, stop_revision)
233
0.200.465 by Jelmer Vernooij
Use dulwich standard functionality for finding missing revisions.
234
 
235
class LocalGitBranch(GitBranch):
236
    """A local Git branch."""
237
0.200.261 by Jelmer Vernooij
More formatting fixes.
238
    def create_checkout(self, to_location, revision_id=None, lightweight=False,
239
        accelerator_tree=None, hardlink=False):
0.200.210 by Jelmer Vernooij
properly error out about not support lightweight checkouts.
240
        if lightweight:
0.230.1 by Jelmer Vernooij
Support lightweight checkouts.
241
            t = transport.get_transport(to_location)
242
            t.ensure_base()
243
            format = self._get_checkout_format()
244
            checkout = format.initialize_on_transport(t)
245
            from_branch = branch.BranchReferenceFormat().initialize(checkout, 
246
                self)
247
            tree = checkout.create_workingtree(revision_id,
248
                from_branch=from_branch, hardlink=hardlink)
249
            return tree
250
        else:
251
            return self._create_heavyweight_checkout(to_location, revision_id,
0.200.261 by Jelmer Vernooij
More formatting fixes.
252
            hardlink)
0.200.210 by Jelmer Vernooij
properly error out about not support lightweight checkouts.
253
254
    def _create_heavyweight_checkout(self, to_location, revision_id=None, 
255
                                     hardlink=False):
256
        """Create a new heavyweight checkout of this branch.
257
258
        :param to_location: URL of location to create the new checkout in.
259
        :param revision_id: Revision that should be the tip of the checkout.
260
        :param hardlink: Whether to hardlink
261
        :return: WorkingTree object of checkout.
262
        """
0.200.513 by Jelmer Vernooij
Fix imports.
263
        checkout_branch = bzrdir.BzrDir.create_branch_convenience(
0.200.210 by Jelmer Vernooij
properly error out about not support lightweight checkouts.
264
            to_location, force_new_tree=False, format=get_rich_root_format())
265
        checkout = checkout_branch.bzrdir
266
        checkout_branch.bind(self)
267
        # pull up to the specified revision_id to set the initial 
268
        # branch tip correctly, and seed it with history.
269
        checkout_branch.pull(self, stop_revision=revision_id)
270
        return checkout.create_workingtree(revision_id, hardlink=hardlink)
271
0.200.57 by Jelmer Vernooij
Fix more tests.
272
    def _gen_revision_history(self):
0.200.58 by Jelmer Vernooij
Fix remaining tests.
273
        if self.head is None:
274
            return []
0.200.261 by Jelmer Vernooij
More formatting fixes.
275
        ret = list(self.repository.iter_reverse_revision_history(
276
            self.last_revision()))
0.200.59 by Jelmer Vernooij
Add more tests, fix revision history.
277
        ret.reverse()
0.200.57 by Jelmer Vernooij
Fix more tests.
278
        return ret
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
279
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
280
    def _get_head(self):
0.200.480 by Jelmer Vernooij
Cope with API changes in Dulwich.
281
        try:
282
            return self.repository._git.ref(self.name)
283
        except KeyError:
284
            return None
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
285
0.200.507 by Jelmer Vernooij
Implement set_last_revision{_info,}.
286
    def set_last_revision_info(self, revno, revid):
287
        self.set_last_revision(revid)
288
289
    def set_last_revision(self, revid):
0.200.523 by Jelmer Vernooij
Fix undefined error.
290
        (newhead, self.mapping) = self.mapping.revision_id_bzr_to_foreign(
0.200.507 by Jelmer Vernooij
Implement set_last_revision{_info,}.
291
                revid)
0.200.523 by Jelmer Vernooij
Fix undefined error.
292
        self.head = newhead
0.200.507 by Jelmer Vernooij
Implement set_last_revision{_info,}.
293
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
294
    def _set_head(self, value):
295
        self._head = value
0.200.480 by Jelmer Vernooij
Cope with API changes in Dulwich.
296
        self.repository._git.refs[self.name] = self._head
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
297
        self._clear_cached_state()
298
299
    head = property(_get_head, _set_head)
300
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
301
    def get_config(self):
302
        return GitBranchConfig(self)
303
304
    def get_push_location(self):
305
        """See Branch.get_push_location."""
306
        push_loc = self.get_config().get_user_option('push_location')
307
        return push_loc
308
309
    def set_push_location(self, location):
310
        """See Branch.set_push_location."""
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
311
        self.get_config().set_user_option('push_location', location,
0.217.54 by John Carr
set_user_option breaks - doesnt have a local option in BranchConfig. Follow the bzr.dev syntax instead.
312
                                          store=config.STORE_LOCATION)
0.200.43 by David Allouche
Ultra-experimental support for "bzr pull". No test. No sanity.
313
314
    def supports_tags(self):
0.200.82 by Jelmer Vernooij
Support listing tags.
315
        return True
0.200.96 by Jelmer Vernooij
Fix branch.
316
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
317
0.200.342 by Jelmer Vernooij
Report git sha during pull.
318
class GitBranchPullResult(branch.PullResult):
319
320
    def report(self, to_file):
321
        if not is_quiet():
322
            if self.old_revid == self.new_revid:
323
                to_file.write('No revisions to pull.\n')
324
            else:
325
                to_file.write('Now on revision %d (git sha: %s).\n' % 
326
                        (self.new_revno, self.new_git_head))
327
        self._show_tag_conficts(to_file)
328
329
0.200.504 by Jelmer Vernooij
Lazily find revno's for git branches.
330
class GitBranchPushResult(branch.BranchPushResult):
331
332
    def _lookup_revno(self, revid):
333
        assert isinstance(revid, str), "was %r" % revid
334
        # Try in source branch first, it'll be faster
335
        try:
336
            return self.source_branch.revision_id_to_revno(revid)
0.200.523 by Jelmer Vernooij
Fix undefined error.
337
        except errors.NoSuchRevision:
0.200.504 by Jelmer Vernooij
Lazily find revno's for git branches.
338
            # FIXME: Check using graph.find_distance_to_null() ?
339
            return self.target_branch.revision_id_to_revno(revid)
340
341
    @property
342
    def old_revno(self):
343
        return self._lookup_revno(self.old_revid)
344
345
    @property
346
    def new_revno(self):
347
        return self._lookup_revno(self.new_revid)
348
349
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
350
class InterFromGitBranch(branch.GenericInterBranch):
0.200.261 by Jelmer Vernooij
More formatting fixes.
351
    """InterBranch implementation that pulls from Git into bzr."""
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
352
353
    @classmethod
354
    def is_compatible(self, source, target):
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
355
        return (isinstance(source, GitBranch) and 
356
                not isinstance(target, GitBranch))
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
357
358
    def update_revisions(self, stop_revision=None, overwrite=False,
359
        graph=None):
360
        """See InterBranch.update_revisions()."""
0.226.2 by Jelmer Vernooij
Cope with new fetch_spec argument.
361
        interrepo = repository.InterRepository.get(self.source.repository, 
362
            self.target.repository)
0.200.342 by Jelmer Vernooij
Report git sha during pull.
363
        self._head = None
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
364
        self._last_revid = None
365
        def determine_wants(heads):
366
            if not self.source.name in heads:
0.200.278 by Jelmer Vernooij
Update branch head appropriately during dpull.
367
                raise NoSuchRef(self.source.name, heads.keys())
0.200.314 by Jelmer Vernooij
Support stop_revision.
368
            if stop_revision is not None:
369
                self._last_revid = stop_revision
0.200.650 by Jelmer Vernooij
Use standard names for lookup functions.
370
                self._head, mapping = self.source.repository.lookup_bzr_revision_id(
0.200.316 by Jelmer Vernooij
Fix formatting.
371
                    stop_revision)
0.200.314 by Jelmer Vernooij
Support stop_revision.
372
            else:
0.200.342 by Jelmer Vernooij
Report git sha during pull.
373
                self._head = heads[self.source.name]
0.200.316 by Jelmer Vernooij
Fix formatting.
374
                self._last_revid = \
0.200.342 by Jelmer Vernooij
Report git sha during pull.
375
                    self.source.mapping.revision_id_foreign_to_bzr(self._head)
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
376
            if self.target.repository.has_revision(self._last_revid):
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
377
                return []
0.200.342 by Jelmer Vernooij
Report git sha during pull.
378
            return [self._head]
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
379
        interrepo.fetch_objects(determine_wants, self.source.mapping)
0.200.313 by Jelmer Vernooij
Support overwrite parameter.
380
        if overwrite:
0.200.314 by Jelmer Vernooij
Support stop_revision.
381
            prev_last_revid = None
0.200.313 by Jelmer Vernooij
Support overwrite parameter.
382
        else:
0.200.314 by Jelmer Vernooij
Support stop_revision.
383
            prev_last_revid = self.target.last_revision()
384
        self.target.generate_revision_history(self._last_revid, prev_last_revid)
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
385
0.200.338 by Jelmer Vernooij
Fix dpushing without changes necessary.
386
    def pull(self, overwrite=False, stop_revision=None,
387
             possible_transports=None, _hook_master=None, run_hooks=True,
0.200.446 by Jelmer Vernooij
Support new 'local' argument.
388
             _override_hook_target=None, local=False):
0.200.338 by Jelmer Vernooij
Fix dpushing without changes necessary.
389
        """See Branch.pull.
390
391
        :param _hook_master: Private parameter - set the branch to
392
            be supplied as the master to pull hooks.
393
        :param run_hooks: Private parameter - if false, this branch
394
            is being called because it's the master of the primary branch,
395
            so it should not run its hooks.
396
        :param _override_hook_target: Private parameter - set the branch to be
397
            supplied as the target_branch to pull hooks.
398
        """
0.200.446 by Jelmer Vernooij
Support new 'local' argument.
399
        # This type of branch can't be bound.
400
        if local:
401
            raise errors.LocalRequiresBoundBranch()
0.200.342 by Jelmer Vernooij
Report git sha during pull.
402
        result = GitBranchPullResult()
0.200.338 by Jelmer Vernooij
Fix dpushing without changes necessary.
403
        result.source_branch = self.source
404
        if _override_hook_target is None:
405
            result.target_branch = self.target
406
        else:
407
            result.target_branch = _override_hook_target
408
        self.source.lock_read()
409
        try:
410
            # We assume that during 'pull' the target repository is closer than
411
            # the source one.
412
            graph = self.target.repository.get_graph(self.source.repository)
413
            result.old_revno, result.old_revid = \
414
                self.target.last_revision_info()
0.200.342 by Jelmer Vernooij
Report git sha during pull.
415
            self.update_revisions(stop_revision, overwrite=overwrite, 
416
                graph=graph)
417
            result.new_git_head = self._head
0.200.338 by Jelmer Vernooij
Fix dpushing without changes necessary.
418
            result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
419
                overwrite)
420
            result.new_revno, result.new_revid = self.target.last_revision_info()
421
            if _hook_master:
422
                result.master_branch = _hook_master
423
                result.local_branch = result.target_branch
424
            else:
425
                result.master_branch = result.target_branch
426
                result.local_branch = None
427
            if run_hooks:
428
                for hook in branch.Branch.hooks['post_pull']:
429
                    hook(result)
430
        finally:
431
            self.source.unlock()
432
        return result
433
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
434
    def _basic_push(self, overwrite=False, stop_revision=None):
435
        result = branch.BranchPushResult()
436
        result.source_branch = self.source
437
        result.target_branch = self.target
0.200.505 by Jelmer Vernooij
Remove duplicate code.
438
        graph = self.target.repository.get_graph(self.source.repository)
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
439
        result.old_revno, result.old_revid = self.target.last_revision_info()
440
        self.update_revisions(stop_revision, overwrite=overwrite, 
441
            graph=graph)
442
        result.new_git_head = self._head
443
        result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
444
            overwrite)
445
        result.new_revno, result.new_revid = self.target.last_revision_info()
446
        return result
447
0.200.338 by Jelmer Vernooij
Fix dpushing without changes necessary.
448
0.200.512 by Jelmer Vernooij
Support pushing git->git.
449
class InterGitBranch(branch.GenericInterBranch):
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
450
    """InterBranch implementation that pulls between Git branches."""
451
0.200.512 by Jelmer Vernooij
Support pushing git->git.
452
453
class InterGitLocalRemoteBranch(InterGitBranch):
454
    """InterBranch that copies from a local to a remote git branch."""
455
456
    @classmethod
457
    def is_compatible(self, source, target):
458
        from bzrlib.plugins.git.remote import RemoteGitBranch
459
        return (isinstance(source, LocalGitBranch) and 
460
                isinstance(target, RemoteGitBranch))
461
462
    def _basic_push(self, overwrite=False, stop_revision=None):
463
        result = GitBranchPushResult()
464
        result.source_branch = self.source
465
        result.target_branch = self.target
466
        if stop_revision is None:
467
            stop_revision = self.source.last_revision()
468
        # FIXME: Check for diverged branches
469
        def get_changed_refs(old_refs):
0.200.544 by Jelmer Vernooij
Support pushing from git -> empty git repo.
470
            result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get("refs/heads/master", "0" * 40))
0.200.650 by Jelmer Vernooij
Use standard names for lookup functions.
471
            refs = { "refs/heads/master": self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
0.200.512 by Jelmer Vernooij
Support pushing git->git.
472
            result.new_revid = stop_revision
473
            for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
474
                refs["refs/tags/%s" % name] = sha
475
            return refs
476
        self.target.repository.send_pack(get_changed_refs, 
477
                self.source.repository._git.object_store.generate_pack_contents)
478
        return result
479
480
481
class InterGitRemoteLocalBranch(InterGitBranch):
482
    """InterBranch that copies from a remote to a local git branch."""
483
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
484
    @classmethod
485
    def is_compatible(self, source, target):
486
        from bzrlib.plugins.git.remote import RemoteGitBranch
487
        return (isinstance(source, RemoteGitBranch) and 
488
                isinstance(target, LocalGitBranch))
489
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
490
    def _basic_push(self, overwrite=False, stop_revision=None):
491
        result = branch.BranchPushResult()
492
        result.source_branch = self.source
493
        result.target_branch = self.target
494
        result.old_revid = self.target.last_revision()
495
        refs, stop_revision = self.update_refs(stop_revision)
496
        self.target.generate_revision_history(stop_revision, result.old_revid)
497
        self.update_tags(refs)
0.200.505 by Jelmer Vernooij
Remove duplicate code.
498
        result.new_revid = self.target.last_revision()
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
499
        return result
500
501
    def update_tags(self, refs):
0.200.648 by Jelmer Vernooij
Fix tag handling when encountering packed refs.
502
        for name, v in extract_tags(refs).iteritems():
503
            revid = self.target.mapping.revision_id_foreign_to_bzr(v)
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
504
            self.target.tags.set_tag(name, revid)
505
506
    def update_refs(self, stop_revision=None):
507
        interrepo = repository.InterRepository.get(self.source.repository, 
508
            self.target.repository)
509
        if stop_revision is None:
510
            refs = interrepo.fetch_refs(branches=["HEAD"])
511
            stop_revision = self.target.mapping.revision_id_foreign_to_bzr(refs["HEAD"])
512
        else:
513
            refs = interrepo.fetch_refs(revision_id=stop_revision)
514
        return refs, stop_revision
515
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
516
    def pull(self, stop_revision=None, overwrite=False, 
0.200.446 by Jelmer Vernooij
Support new 'local' argument.
517
        possible_transports=None, local=False):
518
        # This type of branch can't be bound.
519
        if local:
520
            raise errors.LocalRequiresBoundBranch()
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
521
        result = GitPullResult()
522
        result.source_branch = self.source
523
        result.target_branch = self.target
524
        result.old_revid = self.target.last_revision()
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
525
        refs, stop_revision = self.update_refs(stop_revision)
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
526
        self.target.generate_revision_history(stop_revision, result.old_revid)
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
527
        self.update_tags(refs)
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
528
        result.new_revid = self.target.last_revision()
529
        return result
530
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
531
    
532
class InterToGitBranch(branch.InterBranch):
533
    """InterBranch implementation that pulls from Git into bzr."""
534
0.200.631 by Jelmer Vernooij
Raise proper exception in Branch.get_stacked_on_url().
535
    @staticmethod
536
    def _get_branch_formats_to_test():
537
        return None, None
538
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
539
    @classmethod
540
    def is_compatible(self, source, target):
541
        return (not isinstance(source, GitBranch) and 
542
                isinstance(target, GitBranch))
543
0.200.542 by Jelmer Vernooij
Proper error for push in 1.14.
544
    def update_revisions(self, *args, **kwargs):
545
        raise NoPushSupport()
546
0.200.472 by Jelmer Vernooij
Fix printing error when user attempts to push into git.
547
    def push(self, overwrite=True, stop_revision=None, 
548
             _override_hook_source_branch=None):
549
        raise NoPushSupport()
550
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
551
    def lossy_push(self, stop_revision=None):
0.200.504 by Jelmer Vernooij
Lazily find revno's for git branches.
552
        result = GitBranchPushResult()
0.200.503 by Jelmer Vernooij
Remove dpull, return BranchPushResult in lossy_push.
553
        result.source_branch = self.source
554
        result.target_branch = self.target
0.239.14 by Jelmer Vernooij
Cope with pushing to (not yet) existing branches.
555
        try:
556
            result.old_revid = self.target.last_revision()
557
        except NoSuchRef:
558
            result.old_revid = revision.NULL_REVISION
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
559
        if stop_revision is None:
560
            stop_revision = self.source.last_revision()
561
        # FIXME: Check for diverged branches
562
        refs = { "refs/heads/master": stop_revision }
563
        for name, revid in self.source.tags.get_tag_dict().iteritems():
564
            if self.source.repository.has_revision(revid):
565
                refs["refs/tags/%s" % name] = revid
566
        revidmap, new_refs = self.target.repository.dfetch_refs(
567
            self.source.repository, refs)
568
        if revidmap != {}:
569
            self.target.generate_revision_history(revidmap[stop_revision])
0.200.520 by Jelmer Vernooij
Proper output from dpush.
570
            result.new_revid = revidmap[stop_revision]
571
        else:
572
            result.new_revid = result.old_revid
0.200.503 by Jelmer Vernooij
Remove dpull, return BranchPushResult in lossy_push.
573
        result.revidmap = revidmap
574
        return result
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
575
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
576
577
branch.InterBranch.register_optimiser(InterGitRemoteLocalBranch)
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
578
branch.InterBranch.register_optimiser(InterFromGitBranch)
579
branch.InterBranch.register_optimiser(InterToGitBranch)
0.200.512 by Jelmer Vernooij
Support pushing git->git.
580
branch.InterBranch.register_optimiser(InterGitLocalRemoteBranch)