/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to branch.py

Improve the fix dealing with git repo's in home directories.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2007 Canonical Ltd
 
2
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
2
3
#
3
4
# This program is free software; you can redistribute it and/or modify
4
5
# it under the terms of the GNU General Public License as published by
16
17
 
17
18
"""An adapter between a Git Branch and a Bazaar Branch"""
18
19
 
 
20
from dulwich.objects import (
 
21
    Commit,
 
22
    Tag,
 
23
    )
 
24
 
19
25
from bzrlib import (
20
26
    branch,
 
27
    bzrdir,
21
28
    config,
 
29
    errors,
22
30
    repository,
23
31
    revision,
24
32
    tag,
25
 
    )
26
 
from bzrlib.decorators import needs_read_lock
27
 
from bzrlib.trace import mutter
28
 
 
29
 
from bzrlib.plugins.git.foreign import ForeignBranch
30
 
from bzrlib.plugins.git.errors import LightWeightCheckoutsNotSupported
31
 
 
32
 
from dulwich.objects import (
33
 
        Commit,
34
 
        Tag,
35
 
        )
36
 
 
37
 
class GitTagDict(tag.BasicTags):
 
33
    transport,
 
34
    )
 
35
from bzrlib.decorators import (
 
36
    needs_read_lock,
 
37
    )
 
38
from bzrlib.trace import (
 
39
    is_quiet,
 
40
    mutter,
 
41
    )
 
42
 
 
43
from bzrlib.plugins.git import (
 
44
    get_rich_root_format,
 
45
    )
 
46
from bzrlib.plugins.git.config import (
 
47
    GitBranchConfig,
 
48
    )
 
49
from bzrlib.plugins.git.errors import (
 
50
    NoPushSupport,
 
51
    NoSuchRef,
 
52
    )
 
53
 
 
54
from bzrlib.foreign import ForeignBranch
 
55
 
 
56
 
 
57
def extract_tags(refs):
 
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)
 
62
            ret[k[len("refs/tags/"):]] = v
 
63
    return ret
 
64
 
 
65
 
 
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
 
 
82
class LocalGitTagDict(tag.BasicTags):
 
83
    """Dictionary with tags in a local repository."""
38
84
 
39
85
    def __init__(self, branch):
40
86
        self.branch = branch
42
88
 
43
89
    def get_tag_dict(self):
44
90
        ret = {}
45
 
        for k,v in self.repository._git.tags.iteritems():
46
 
            obj = self.repository._git.get_object(v)
 
91
        for k,v in extract_tags(self.repository._git.get_refs()).iteritems():
 
92
            try:
 
93
                obj = self.repository._git[v]
 
94
            except KeyError:
 
95
                mutter("Tag %s points at unknown object %s, ignoring", v, obj)
 
96
                continue
47
97
            while isinstance(obj, Tag):
48
98
                v = obj.object[1]
49
 
                obj = self.repository._git.get_object(v)
 
99
                obj = self.repository._git[v]
50
100
            if not isinstance(obj, Commit):
51
 
                mutter("Tag %s points at object %r that is not a commit, ignoring", k, obj)
 
101
                mutter("Tag %s points at object %r that is not a commit, "
 
102
                       "ignoring", k, obj)
52
103
                continue
53
104
            ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
54
105
        return ret
55
106
 
 
107
    def _set_tag_dict(self, to_dict):
 
108
        extra = set(self.repository._git.get_refs().keys())
 
109
        for k, revid in to_dict.iteritems():
 
110
            name = "refs/tags/%s" % k
 
111
            if name in extra:
 
112
                extra.remove(name)
 
113
            self.set_tag(k, revid)
 
114
        for name in extra:
 
115
            if name.startswith("refs/tags/"):
 
116
                del self.repository._git[name]
 
117
        
56
118
    def set_tag(self, name, revid):
57
 
        self.repository._git.tags[name] = revid
58
 
 
59
 
 
60
 
class GitBranchConfig(config.BranchConfig):
61
 
    """BranchConfig that uses locations.conf in place of branch.conf"""
62
 
 
63
 
    def __init__(self, branch):
64
 
        config.BranchConfig.__init__(self, branch)
65
 
        # do not provide a BranchDataConfig
66
 
        self.option_sources = self.option_sources[0], self.option_sources[2]
67
 
 
68
 
    def set_user_option(self, name, value, store=config.STORE_BRANCH, warn_masked=False):
69
 
        """Force local to True"""
70
 
        config.BranchConfig.set_user_option(self, name, value, store=config.STORE_LOCATION, warn_masked=warn_masked)
 
119
        self.repository._git.refs["refs/tags/%s" % name], _ = \
 
120
            self.branch.mapping.revision_id_bzr_to_foreign(revid)
 
121
 
 
122
 
 
123
class DictTagDict(LocalGitTagDict):
 
124
 
 
125
 
 
126
    def __init__(self, branch, tags):
 
127
        super(DictTagDict, self).__init__(branch)
 
128
        self._tags = tags
 
129
 
 
130
    def get_tag_dict(self):
 
131
        return self._tags
 
132
 
71
133
 
72
134
 
73
135
class GitBranchFormat(branch.BranchFormat):
75
137
    def get_format_description(self):
76
138
        return 'Git Branch'
77
139
 
 
140
    def network_name(self):
 
141
        return "git"
 
142
 
78
143
    def supports_tags(self):
79
144
        return True
80
145
 
 
146
    def get_foreign_tests_branch_factory(self):
 
147
        from bzrlib.plugins.git.tests.test_branch import ForeignTestsBranchFactory
 
148
        return ForeignTestsBranchFactory()
 
149
 
 
150
    def make_tags(self, branch):
 
151
        if getattr(branch.repository, "get_refs", None) is not None:
 
152
            from bzrlib.plugins.git.remote import RemoteGitTagDict
 
153
            return RemoteGitTagDict(branch)
 
154
        else:
 
155
            return LocalGitTagDict(branch)
 
156
 
81
157
 
82
158
class GitBranch(ForeignBranch):
83
159
    """An adapter to git repositories for bzr Branch objects."""
84
160
 
85
 
    def __init__(self, bzrdir, repository, name, head, lockfiles):
 
161
    def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
86
162
        self.repository = repository
87
 
        super(GitBranch, self).__init__(repository.get_mapping())
 
163
        self._format = GitBranchFormat()
88
164
        self.control_files = lockfiles
89
165
        self.bzrdir = bzrdir
 
166
        super(GitBranch, self).__init__(repository.get_mapping())
 
167
        if tagsdict is not None:
 
168
            self.tags = DictTagDict(self, tagsdict)
90
169
        self.name = name
91
 
        self.head = head
92
 
        self.base = bzrdir.transport.base
93
 
        self._format = GitBranchFormat()
94
 
 
95
 
    def dpull(self, source, stop_revision=None):
96
 
        if stop_revision is None:
97
 
            stop_revision = source.last_revision()
98
 
        # FIXME: Check for diverged branches
99
 
        revidmap = self.repository.dfetch(source.repository, stop_revision)
100
 
        self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(revidmap[stop_revision])
101
 
        return revidmap
 
170
        self._head = None
 
171
        self.base = bzrdir.root_transport.base
 
172
 
 
173
    def _get_checkout_format(self):
 
174
        """Return the most suitable metadir for a checkout of this branch.
 
175
        Weaves are used if this branch's repository uses weaves.
 
176
        """
 
177
        return get_rich_root_format()
 
178
 
 
179
    def get_child_submit_format(self):
 
180
        """Return the preferred format of submissions to this branch."""
 
181
        ret = self.get_config().get_user_option("child_submit_format")
 
182
        if ret is not None:
 
183
            return ret
 
184
        return "git"
 
185
 
 
186
    def _get_nick(self, local=False, possible_master_transports=None):
 
187
        """Find the nick name for this branch.
 
188
 
 
189
        :return: Branch nick
 
190
        """
 
191
        return self.name
 
192
 
 
193
    def _set_nick(self, nick):
 
194
        raise NotImplementedError
 
195
 
 
196
    nick = property(_get_nick, _set_nick)
 
197
 
 
198
    def __repr__(self):
 
199
        return "%s(%r, %r)" % (self.__class__.__name__, self.repository.base, self.name)
 
200
 
 
201
    def generate_revision_history(self, revid, old_revid=None):
 
202
        # FIXME: Check that old_revid is in the ancestry of revid
 
203
        newhead, self.mapping = self.mapping.revision_id_bzr_to_foreign(revid)
 
204
        self._set_head(newhead)
102
205
 
103
206
    def lock_write(self):
104
207
        self.control_files.lock_write()
105
208
 
106
209
    def get_stacked_on_url(self):
107
210
        # Git doesn't do stacking (yet...)
108
 
        return None
 
211
        raise errors.UnstackableBranchFormat(self._format, self.base)
109
212
 
110
213
    def get_parent(self):
111
214
        """See Branch.get_parent()."""
 
215
        # FIXME: Set "origin" url from .git/config ?
112
216
        return None
113
217
 
114
218
    def set_parent(self, url):
 
219
        # FIXME: Set "origin" url in .git/config ?
115
220
        pass
116
221
 
117
222
    def lock_read(self):
118
223
        self.control_files.lock_read()
119
224
 
 
225
    def is_locked(self):
 
226
        return self.control_files.is_locked()
 
227
 
120
228
    def unlock(self):
121
229
        self.control_files.unlock()
122
230
 
123
231
    def get_physical_lock_status(self):
124
232
        return False
125
233
 
126
 
 
127
 
class LocalGitBranch(GitBranch):
128
 
 
129
234
    @needs_read_lock
130
235
    def last_revision(self):
131
236
        # perhaps should escape this ?
133
238
            return revision.NULL_REVISION
134
239
        return self.mapping.revision_id_foreign_to_bzr(self.head)
135
240
 
136
 
    def create_checkout(self, to_location, revision_id=None, 
137
 
                        lightweight=False, accelerator_tree=None, hardlink=False):
 
241
    def _basic_push(self, target, overwrite=False, stop_revision=None):
 
242
        return branch.InterBranch.get(self, target)._basic_push(
 
243
            overwrite, stop_revision)
 
244
 
 
245
 
 
246
class LocalGitBranch(GitBranch):
 
247
    """A local Git branch."""
 
248
 
 
249
    def create_checkout(self, to_location, revision_id=None, lightweight=False,
 
250
        accelerator_tree=None, hardlink=False):
138
251
        if lightweight:
139
 
            raise LightWeightCheckoutsNotSupported()
140
 
        return self._create_heavyweight_checkout(to_location, revision_id, hardlink)
 
252
            t = transport.get_transport(to_location)
 
253
            t.ensure_base()
 
254
            format = self._get_checkout_format()
 
255
            checkout = format.initialize_on_transport(t)
 
256
            from_branch = branch.BranchReferenceFormat().initialize(checkout,
 
257
                self)
 
258
            tree = checkout.create_workingtree(revision_id,
 
259
                from_branch=from_branch, hardlink=hardlink)
 
260
            return tree
 
261
        else:
 
262
            return self._create_heavyweight_checkout(to_location, revision_id,
 
263
            hardlink)
141
264
 
142
 
    def _create_heavyweight_checkout(self, to_location, revision_id=None, 
 
265
    def _create_heavyweight_checkout(self, to_location, revision_id=None,
143
266
                                     hardlink=False):
144
267
        """Create a new heavyweight checkout of this branch.
145
268
 
148
271
        :param hardlink: Whether to hardlink
149
272
        :return: WorkingTree object of checkout.
150
273
        """
151
 
        checkout_branch = BzrDir.create_branch_convenience(
 
274
        checkout_branch = bzrdir.BzrDir.create_branch_convenience(
152
275
            to_location, force_new_tree=False, format=get_rich_root_format())
153
276
        checkout = checkout_branch.bzrdir
154
277
        checkout_branch.bind(self)
155
 
        # pull up to the specified revision_id to set the initial 
 
278
        # pull up to the specified revision_id to set the initial
156
279
        # branch tip correctly, and seed it with history.
157
280
        checkout_branch.pull(self, stop_revision=revision_id)
158
281
        return checkout.create_workingtree(revision_id, hardlink=hardlink)
159
282
 
160
 
    def _make_tags(self):
161
 
        return GitTagDict(self)
162
 
 
163
283
    def _gen_revision_history(self):
164
284
        if self.head is None:
165
285
            return []
166
 
        ret = list(self.repository.iter_reverse_revision_history(self.last_revision()))
 
286
        ret = list(self.repository.iter_reverse_revision_history(
 
287
            self.last_revision()))
167
288
        ret.reverse()
168
289
        return ret
169
290
 
 
291
    def _get_head(self):
 
292
        try:
 
293
            return self.repository._git.ref(self.name)
 
294
        except KeyError:
 
295
            return None
 
296
 
 
297
    def set_last_revision_info(self, revno, revid):
 
298
        self.set_last_revision(revid)
 
299
 
 
300
    def set_last_revision(self, revid):
 
301
        (newhead, self.mapping) = self.mapping.revision_id_bzr_to_foreign(
 
302
                revid)
 
303
        self.head = newhead
 
304
 
 
305
    def _set_head(self, value):
 
306
        self._head = value
 
307
        self.repository._git.refs[self.name] = self._head
 
308
        self._clear_cached_state()
 
309
 
 
310
    head = property(_get_head, _set_head)
 
311
 
170
312
    def get_config(self):
171
313
        return GitBranchConfig(self)
172
314
 
183
325
    def supports_tags(self):
184
326
        return True
185
327
 
186
 
    def sprout(self, to_bzrdir, revision_id=None):
187
 
        """See Branch.sprout()."""
188
 
        result = to_bzrdir.create_branch()
189
 
        self.copy_content_into(result, revision_id=revision_id)
190
 
        result.set_parent(self.bzrdir.root_transport.base)
191
 
        return result
192
 
 
193
 
 
194
 
class InterGitGenericBranch(branch.InterBranch):
195
 
 
196
 
    @classmethod
197
 
    def is_compatible(self, source, target):
198
 
        return isinstance(source, GitBranch)
 
328
 
 
329
class GitBranchPullResult(branch.PullResult):
 
330
 
 
331
    def report(self, to_file):
 
332
        if not is_quiet():
 
333
            if self.old_revid == self.new_revid:
 
334
                to_file.write('No revisions to pull.\n')
 
335
            else:
 
336
                to_file.write('Now on revision %d (git sha: %s).\n' %
 
337
                        (self.new_revno, self.new_git_head))
 
338
        self._show_tag_conficts(to_file)
 
339
 
 
340
 
 
341
class GitBranchPushResult(branch.BranchPushResult):
 
342
 
 
343
    def _lookup_revno(self, revid):
 
344
        assert isinstance(revid, str), "was %r" % revid
 
345
        # Try in source branch first, it'll be faster
 
346
        try:
 
347
            return self.source_branch.revision_id_to_revno(revid)
 
348
        except errors.NoSuchRevision:
 
349
            # FIXME: Check using graph.find_distance_to_null() ?
 
350
            return self.target_branch.revision_id_to_revno(revid)
 
351
 
 
352
    @property
 
353
    def old_revno(self):
 
354
        return self._lookup_revno(self.old_revid)
 
355
 
 
356
    @property
 
357
    def new_revno(self):
 
358
        return self._lookup_revno(self.new_revid)
 
359
 
 
360
 
 
361
class InterFromGitBranch(branch.GenericInterBranch):
 
362
    """InterBranch implementation that pulls from Git into bzr."""
 
363
 
 
364
    @classmethod
 
365
    def _get_interrepo(self, source, target):
 
366
        return repository.InterRepository.get(source.repository,
 
367
            target.repository)
 
368
 
 
369
    @classmethod
 
370
    def is_compatible(cls, source, target):
 
371
        return (isinstance(source, GitBranch) and
 
372
                not isinstance(target, GitBranch) and
 
373
                (getattr(cls._get_interrepo(source, target), "fetch_objects", None) is not None))
199
374
 
200
375
    def update_revisions(self, stop_revision=None, overwrite=False,
201
376
        graph=None):
202
377
        """See InterBranch.update_revisions()."""
203
 
        # TODO: stop_revision, overwrite
204
 
        interrepo = repository.InterRepository.get(self.source.repository, self.target.repository)
 
378
        interrepo = self._get_interrepo(self.source, self.target)
 
379
        self._head = None
205
380
        self._last_revid = None
206
381
        def determine_wants(heads):
207
382
            if not self.source.name in heads:
208
 
                raise BzrError("No such remote branch '%s', found: %r" % (
209
 
                    self.source.name, heads.keys()))
210
 
            head = heads[self.source.name]
211
 
            self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
 
383
                raise NoSuchRef(self.source.name, heads.keys())
 
384
            if stop_revision is not None:
 
385
                self._last_revid = stop_revision
 
386
                self._head, mapping = self.source.repository.lookup_bzr_revision_id(
 
387
                    stop_revision)
 
388
            else:
 
389
                self._head = heads[self.source.name]
 
390
                self._last_revid = \
 
391
                    self.source.mapping.revision_id_foreign_to_bzr(self._head)
212
392
            if self.target.repository.has_revision(self._last_revid):
213
393
                return []
214
 
            return [head]
 
394
            return [self._head]
215
395
        interrepo.fetch_objects(determine_wants, self.source.mapping)
216
 
        self.target.generate_revision_history(self._last_revid)
217
 
 
218
 
 
219
 
branch.InterBranch.register_optimiser(InterGitGenericBranch)
 
396
        if overwrite:
 
397
            prev_last_revid = None
 
398
        else:
 
399
            prev_last_revid = self.target.last_revision()
 
400
        self.target.generate_revision_history(self._last_revid, prev_last_revid)
 
401
 
 
402
    def pull(self, overwrite=False, stop_revision=None,
 
403
             possible_transports=None, _hook_master=None, run_hooks=True,
 
404
             _override_hook_target=None, local=False):
 
405
        """See Branch.pull.
 
406
 
 
407
        :param _hook_master: Private parameter - set the branch to
 
408
            be supplied as the master to pull hooks.
 
409
        :param run_hooks: Private parameter - if false, this branch
 
410
            is being called because it's the master of the primary branch,
 
411
            so it should not run its hooks.
 
412
        :param _override_hook_target: Private parameter - set the branch to be
 
413
            supplied as the target_branch to pull hooks.
 
414
        """
 
415
        # This type of branch can't be bound.
 
416
        if local:
 
417
            raise errors.LocalRequiresBoundBranch()
 
418
        result = GitBranchPullResult()
 
419
        result.source_branch = self.source
 
420
        if _override_hook_target is None:
 
421
            result.target_branch = self.target
 
422
        else:
 
423
            result.target_branch = _override_hook_target
 
424
        self.source.lock_read()
 
425
        try:
 
426
            # We assume that during 'pull' the target repository is closer than
 
427
            # the source one.
 
428
            graph = self.target.repository.get_graph(self.source.repository)
 
429
            result.old_revno, result.old_revid = \
 
430
                self.target.last_revision_info()
 
431
            self.update_revisions(stop_revision, overwrite=overwrite,
 
432
                graph=graph)
 
433
            result.new_git_head = self._head
 
434
            result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
 
435
                overwrite)
 
436
            result.new_revno, result.new_revid = self.target.last_revision_info()
 
437
            if _hook_master:
 
438
                result.master_branch = _hook_master
 
439
                result.local_branch = result.target_branch
 
440
            else:
 
441
                result.master_branch = result.target_branch
 
442
                result.local_branch = None
 
443
            if run_hooks:
 
444
                for hook in branch.Branch.hooks['post_pull']:
 
445
                    hook(result)
 
446
        finally:
 
447
            self.source.unlock()
 
448
        return result
 
449
 
 
450
    def _basic_push(self, overwrite=False, stop_revision=None):
 
451
        result = branch.BranchPushResult()
 
452
        result.source_branch = self.source
 
453
        result.target_branch = self.target
 
454
        graph = self.target.repository.get_graph(self.source.repository)
 
455
        result.old_revno, result.old_revid = self.target.last_revision_info()
 
456
        self.update_revisions(stop_revision, overwrite=overwrite, graph=graph)
 
457
        result.new_git_head = self._head
 
458
        result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
 
459
            overwrite)
 
460
        result.new_revno, result.new_revid = self.target.last_revision_info()
 
461
        return result
 
462
 
 
463
 
 
464
class InterGitBranch(branch.GenericInterBranch):
 
465
    """InterBranch implementation that pulls between Git branches."""
 
466
 
 
467
 
 
468
class InterGitLocalRemoteBranch(InterGitBranch):
 
469
    """InterBranch that copies from a local to a remote git branch."""
 
470
 
 
471
    @classmethod
 
472
    def is_compatible(self, source, target):
 
473
        from bzrlib.plugins.git.remote import RemoteGitBranch
 
474
        return (isinstance(source, LocalGitBranch) and
 
475
                isinstance(target, RemoteGitBranch))
 
476
 
 
477
    def _basic_push(self, overwrite=False, stop_revision=None):
 
478
        result = GitBranchPushResult()
 
479
        result.source_branch = self.source
 
480
        result.target_branch = self.target
 
481
        if stop_revision is None:
 
482
            stop_revision = self.source.last_revision()
 
483
        # FIXME: Check for diverged branches
 
484
        def get_changed_refs(old_refs):
 
485
            result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get("refs/heads/master", "0" * 40))
 
486
            refs = { "refs/heads/master": self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
 
487
            result.new_revid = stop_revision
 
488
            for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
 
489
                refs["refs/tags/%s" % name] = sha
 
490
            return refs
 
491
        self.target.repository.send_pack(get_changed_refs,
 
492
                self.source.repository._git.object_store.generate_pack_contents)
 
493
        return result
 
494
 
 
495
 
 
496
class InterGitRemoteLocalBranch(InterGitBranch):
 
497
    """InterBranch that copies from a remote to a local git branch."""
 
498
 
 
499
    @classmethod
 
500
    def is_compatible(self, source, target):
 
501
        from bzrlib.plugins.git.remote import RemoteGitBranch
 
502
        return (isinstance(source, RemoteGitBranch) and
 
503
                isinstance(target, LocalGitBranch))
 
504
 
 
505
    def _basic_push(self, overwrite=False, stop_revision=None):
 
506
        result = branch.BranchPushResult()
 
507
        result.source_branch = self.source
 
508
        result.target_branch = self.target
 
509
        result.old_revid = self.target.last_revision()
 
510
        refs, stop_revision = self.update_refs(stop_revision)
 
511
        self.target.generate_revision_history(stop_revision, result.old_revid)
 
512
        self.update_tags(refs)
 
513
        result.new_revid = self.target.last_revision()
 
514
        return result
 
515
 
 
516
    def update_tags(self, refs):
 
517
        for name, v in extract_tags(refs).iteritems():
 
518
            revid = self.target.mapping.revision_id_foreign_to_bzr(v)
 
519
            self.target.tags.set_tag(name, revid)
 
520
 
 
521
    def update_refs(self, stop_revision=None):
 
522
        interrepo = repository.InterRepository.get(self.source.repository,
 
523
            self.target.repository)
 
524
        if stop_revision is None:
 
525
            refs = interrepo.fetch_refs(branches=["HEAD"])
 
526
            stop_revision = self.target.mapping.revision_id_foreign_to_bzr(refs["HEAD"])
 
527
        else:
 
528
            refs = interrepo.fetch_refs(revision_id=stop_revision)
 
529
        return refs, stop_revision
 
530
 
 
531
    def pull(self, stop_revision=None, overwrite=False,
 
532
        possible_transports=None, local=False):
 
533
        # This type of branch can't be bound.
 
534
        if local:
 
535
            raise errors.LocalRequiresBoundBranch()
 
536
        result = GitPullResult()
 
537
        result.source_branch = self.source
 
538
        result.target_branch = self.target
 
539
        result.old_revid = self.target.last_revision()
 
540
        refs, stop_revision = self.update_refs(stop_revision)
 
541
        self.target.generate_revision_history(stop_revision, result.old_revid)
 
542
        self.update_tags(refs)
 
543
        result.new_revid = self.target.last_revision()
 
544
        return result
 
545
 
 
546
 
 
547
class InterToGitBranch(branch.InterBranch):
 
548
    """InterBranch implementation that pulls from Git into bzr."""
 
549
 
 
550
    @staticmethod
 
551
    def _get_branch_formats_to_test():
 
552
        return None, None
 
553
 
 
554
    @classmethod
 
555
    def is_compatible(self, source, target):
 
556
        return (not isinstance(source, GitBranch) and
 
557
                isinstance(target, GitBranch))
 
558
 
 
559
    def update_revisions(self, *args, **kwargs):
 
560
        raise NoPushSupport()
 
561
 
 
562
    def push(self, overwrite=True, stop_revision=None,
 
563
             _override_hook_source_branch=None):
 
564
        raise NoPushSupport()
 
565
 
 
566
    def lossy_push(self, stop_revision=None):
 
567
        result = GitBranchPushResult()
 
568
        result.source_branch = self.source
 
569
        result.target_branch = self.target
 
570
        try:
 
571
            result.old_revid = self.target.last_revision()
 
572
        except NoSuchRef:
 
573
            result.old_revid = revision.NULL_REVISION
 
574
        if stop_revision is None:
 
575
            stop_revision = self.source.last_revision()
 
576
        # FIXME: Check for diverged branches
 
577
        refs = { "refs/heads/master": stop_revision }
 
578
        for name, revid in self.source.tags.get_tag_dict().iteritems():
 
579
            if self.source.repository.has_revision(revid):
 
580
                refs["refs/tags/%s" % name] = revid
 
581
        revidmap, new_refs = self.target.repository.dfetch_refs(
 
582
            self.source.repository, refs)
 
583
        if revidmap != {}:
 
584
            self.target.generate_revision_history(revidmap[stop_revision])
 
585
            result.new_revid = revidmap[stop_revision]
 
586
        else:
 
587
            result.new_revid = result.old_revid
 
588
        result.revidmap = revidmap
 
589
        return result
 
590
 
 
591
 
 
592
branch.InterBranch.register_optimiser(InterGitRemoteLocalBranch)
 
593
branch.InterBranch.register_optimiser(InterFromGitBranch)
 
594
branch.InterBranch.register_optimiser(InterToGitBranch)
 
595
branch.InterBranch.register_optimiser(InterGitLocalRemoteBranch)