/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

  • Committer: Jelmer Vernooij
  • Date: 2010-05-13 08:40:18 UTC
  • mto: (0.200.912 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20100513084018-xp9tbrasde6gih3o
Checks for roundtripping.

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
# Copyright (C) 2009-2010 Jelmer Vernooij <jelmer@samba.org>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
5
5
# it under the terms of the GNU General Public License as published by
24
24
 
25
25
from bzrlib import (
26
26
    branch,
 
27
    bzrdir,
27
28
    config,
28
29
    errors,
29
 
    foreign,
30
30
    repository,
31
31
    revision,
32
32
    tag,
40
40
    mutter,
41
41
    )
42
42
 
 
43
from bzrlib.plugins.git import (
 
44
    get_rich_root_format,
 
45
    )
43
46
from bzrlib.plugins.git.config import (
44
47
    GitBranchConfig,
45
48
    )
47
50
    NoPushSupport,
48
51
    NoSuchRef,
49
52
    )
50
 
 
51
 
try:
52
 
    from bzrlib.foreign import ForeignBranch
53
 
except ImportError:
54
 
    class ForeignBranch(branch.Branch):
55
 
        def __init__(self, mapping):
56
 
            self.mapping = mapping
57
 
            super(ForeignBranch, self).__init__()
58
 
 
59
 
 
60
 
def extract_tags(refs, mapping):
61
 
    ret = {}
62
 
    for k,v in refs.iteritems():
63
 
        if k.startswith("refs/tags/") and not k.endswith("^{}"):
64
 
            v = refs.get(k+"^{}", v)
65
 
            ret[k[len("refs/tags/"):]] = mapping.revision_id_foreign_to_bzr(v)
66
 
    return ret
 
53
from bzrlib.plugins.git.refs import (
 
54
    ref_to_branch_name,
 
55
    extract_tags,
 
56
    tag_name_to_ref,
 
57
    )
 
58
 
 
59
from bzrlib.foreign import ForeignBranch
67
60
 
68
61
 
69
62
class GitPullResult(branch.PullResult):
91
84
 
92
85
    def get_tag_dict(self):
93
86
        ret = {}
94
 
        for k,v in self.repository._git.refs.as_dict("refs/tags").iteritems():
95
 
            obj = self.repository._git.get_object(v)
 
87
        for k,v in extract_tags(self.repository._git.get_refs()).iteritems():
 
88
            try:
 
89
                obj = self.repository._git[v]
 
90
            except KeyError:
 
91
                mutter("Tag %s points at unknown object %s, ignoring", v, obj)
 
92
                continue
96
93
            while isinstance(obj, Tag):
97
94
                v = obj.object[1]
98
 
                obj = self.repository._git.get_object(v)
 
95
                obj = self.repository._git[v]
99
96
            if not isinstance(obj, Commit):
100
97
                mutter("Tag %s points at object %r that is not a commit, "
101
98
                       "ignoring", k, obj)
103
100
            ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
104
101
        return ret
105
102
 
 
103
    def _set_tag_dict(self, to_dict):
 
104
        extra = set(self.repository._git.get_refs().keys())
 
105
        for k, revid in to_dict.iteritems():
 
106
            name = tag_name_to_ref(k)
 
107
            if name in extra:
 
108
                extra.remove(name)
 
109
            self.set_tag(k, revid)
 
110
        for name in extra:
 
111
            if name.startswith("refs/tags/"):
 
112
                del self.repository._git[name]
 
113
        
106
114
    def set_tag(self, name, revid):
107
 
        self.repository._git.refs["refs/tags/%s" % name], _ = \
 
115
        self.repository._git.refs[tag_name_to_ref(name)], _ = \
108
116
            self.branch.mapping.revision_id_bzr_to_foreign(revid)
109
117
 
110
118
 
 
119
class DictTagDict(LocalGitTagDict):
 
120
 
 
121
    def __init__(self, branch, tags):
 
122
        super(DictTagDict, self).__init__(branch)
 
123
        self._tags = tags
 
124
 
 
125
    def get_tag_dict(self):
 
126
        return self._tags
 
127
 
 
128
 
111
129
class GitBranchFormat(branch.BranchFormat):
112
130
 
113
131
    def get_format_description(self):
114
132
        return 'Git Branch'
115
133
 
 
134
    def network_name(self):
 
135
        return "git"
 
136
 
116
137
    def supports_tags(self):
117
138
        return True
118
139
 
 
140
    def get_foreign_tests_branch_factory(self):
 
141
        from bzrlib.plugins.git.tests.test_branch import ForeignTestsBranchFactory
 
142
        return ForeignTestsBranchFactory()
 
143
 
119
144
    def make_tags(self, branch):
120
145
        if getattr(branch.repository, "get_refs", None) is not None:
121
146
            from bzrlib.plugins.git.remote import RemoteGitTagDict
127
152
class GitBranch(ForeignBranch):
128
153
    """An adapter to git repositories for bzr Branch objects."""
129
154
 
130
 
    def __init__(self, bzrdir, repository, name, lockfiles):
 
155
    def __init__(self, bzrdir, repository, ref, lockfiles, tagsdict=None):
131
156
        self.repository = repository
132
157
        self._format = GitBranchFormat()
133
158
        self.control_files = lockfiles
134
159
        self.bzrdir = bzrdir
135
160
        super(GitBranch, self).__init__(repository.get_mapping())
136
 
        self.name = name
 
161
        if tagsdict is not None:
 
162
            self.tags = DictTagDict(self, tagsdict)
 
163
        self.ref = ref
 
164
        self.name = ref_to_branch_name(ref)
137
165
        self._head = None
138
 
        self.base = bzrdir.transport.base
 
166
        self.base = bzrdir.root_transport.base
 
167
 
 
168
    def _get_checkout_format(self):
 
169
        """Return the most suitable metadir for a checkout of this branch.
 
170
        Weaves are used if this branch's repository uses weaves.
 
171
        """
 
172
        return get_rich_root_format()
 
173
 
 
174
    def get_child_submit_format(self):
 
175
        """Return the preferred format of submissions to this branch."""
 
176
        ret = self.get_config().get_user_option("child_submit_format")
 
177
        if ret is not None:
 
178
            return ret
 
179
        return "git"
139
180
 
140
181
    def _get_nick(self, local=False, possible_master_transports=None):
141
182
        """Find the nick name for this branch.
150
191
    nick = property(_get_nick, _set_nick)
151
192
 
152
193
    def __repr__(self):
153
 
        return "%s(%r, %r)" % (self.__class__.__name__, self.repository.base, self.name)
154
 
 
155
 
    def dpull(self, source, stop_revision=None):
156
 
        return branch.InterBranch.get(source, self).lossy_push()
 
194
        return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
 
195
            self.ref)
157
196
 
158
197
    def generate_revision_history(self, revid, old_revid=None):
159
198
        # FIXME: Check that old_revid is in the ancestry of revid
165
204
 
166
205
    def get_stacked_on_url(self):
167
206
        # Git doesn't do stacking (yet...)
168
 
        return None
 
207
        raise errors.UnstackableBranchFormat(self._format, self.base)
169
208
 
170
209
    def get_parent(self):
171
210
        """See Branch.get_parent()."""
195
234
            return revision.NULL_REVISION
196
235
        return self.mapping.revision_id_foreign_to_bzr(self.head)
197
236
 
198
 
 
 
237
    def _basic_push(self, target, overwrite=False, stop_revision=None):
 
238
        return branch.InterBranch.get(self, target)._basic_push(
 
239
            overwrite, stop_revision)
 
240
 
 
241
 
199
242
class LocalGitBranch(GitBranch):
200
243
    """A local Git branch."""
201
244
 
202
 
    def _get_checkout_format(self):
203
 
        """Return the most suitable metadir for a checkout of this branch.
204
 
        Weaves are used if this branch's repository uses weaves.
205
 
        """
206
 
        format = self.repository.bzrdir.checkout_metadir()
207
 
        format.set_branch_format(self._format)
208
 
        return format
 
245
    def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
 
246
        super(LocalGitBranch, self).__init__(bzrdir, repository, name, 
 
247
              lockfiles, tagsdict)
 
248
        if not name in repository._git.get_refs().keys():
 
249
            raise errors.NotBranchError(self.base)
209
250
 
210
251
    def create_checkout(self, to_location, revision_id=None, lightweight=False,
211
252
        accelerator_tree=None, hardlink=False):
214
255
            t.ensure_base()
215
256
            format = self._get_checkout_format()
216
257
            checkout = format.initialize_on_transport(t)
217
 
            from_branch = branch.BranchReferenceFormat().initialize(checkout, 
 
258
            from_branch = branch.BranchReferenceFormat().initialize(checkout,
218
259
                self)
219
260
            tree = checkout.create_workingtree(revision_id,
220
261
                from_branch=from_branch, hardlink=hardlink)
223
264
            return self._create_heavyweight_checkout(to_location, revision_id,
224
265
            hardlink)
225
266
 
226
 
    def _create_heavyweight_checkout(self, to_location, revision_id=None, 
 
267
    def _create_heavyweight_checkout(self, to_location, revision_id=None,
227
268
                                     hardlink=False):
228
269
        """Create a new heavyweight checkout of this branch.
229
270
 
232
273
        :param hardlink: Whether to hardlink
233
274
        :return: WorkingTree object of checkout.
234
275
        """
235
 
        checkout_branch = BzrDir.create_branch_convenience(
 
276
        checkout_branch = bzrdir.BzrDir.create_branch_convenience(
236
277
            to_location, force_new_tree=False, format=get_rich_root_format())
237
278
        checkout = checkout_branch.bzrdir
238
279
        checkout_branch.bind(self)
239
 
        # pull up to the specified revision_id to set the initial 
 
280
        # pull up to the specified revision_id to set the initial
240
281
        # branch tip correctly, and seed it with history.
241
282
        checkout_branch.pull(self, stop_revision=revision_id)
242
283
        return checkout.create_workingtree(revision_id, hardlink=hardlink)
251
292
 
252
293
    def _get_head(self):
253
294
        try:
254
 
            return self.repository._git.ref(self.name)
 
295
            return self.repository._git.ref(self.ref)
255
296
        except KeyError:
256
297
            return None
257
298
 
 
299
    def set_last_revision_info(self, revno, revid):
 
300
        self.set_last_revision(revid)
 
301
 
 
302
    def set_last_revision(self, revid):
 
303
        (newhead, self.mapping) = self.repository.lookup_bzr_revision_id(revid)
 
304
        self.head = newhead
 
305
 
258
306
    def _set_head(self, value):
259
307
        self._head = value
260
 
        self.repository._git.refs[self.name] = self._head
 
308
        self.repository._git.refs[self.ref] = self._head
261
309
        self._clear_cached_state()
262
310
 
263
311
    head = property(_get_head, _set_head)
281
329
 
282
330
class GitBranchPullResult(branch.PullResult):
283
331
 
 
332
    def __init__(self):
 
333
        super(GitBranchPullResult, self).__init__()
 
334
        self.new_git_head = None
 
335
        self._old_revno = None
 
336
        self._new_revno = None
 
337
 
284
338
    def report(self, to_file):
285
339
        if not is_quiet():
286
340
            if self.old_revid == self.new_revid:
287
341
                to_file.write('No revisions to pull.\n')
288
 
            else:
289
 
                to_file.write('Now on revision %d (git sha: %s).\n' % 
 
342
            elif self.new_git_head is not None:
 
343
                to_file.write('Now on revision %d (git sha: %s).\n' %
290
344
                        (self.new_revno, self.new_git_head))
 
345
            else:
 
346
                to_file.write('Now on revision %d.\n' % (self.new_revno,))
291
347
        self._show_tag_conficts(to_file)
292
348
 
293
 
 
294
 
class InterFromGitBranch(branch.InterBranch):
 
349
    def _lookup_revno(self, revid):
 
350
        assert isinstance(revid, str), "was %r" % revid
 
351
        # Try in source branch first, it'll be faster
 
352
        try:
 
353
            return self.source_branch.revision_id_to_revno(revid)
 
354
        except errors.NoSuchRevision:
 
355
            # FIXME: Check using graph.find_distance_to_null() ?
 
356
            return self.target_branch.revision_id_to_revno(revid)
 
357
 
 
358
    def _get_old_revno(self):
 
359
        if self._old_revno is not None:
 
360
            return self._old_revno
 
361
        return self._lookup_revno(self.old_revid)
 
362
 
 
363
    def _set_old_revno(self, revno):
 
364
        self._old_revno = revno
 
365
 
 
366
    old_revno = property(_get_old_revno, _set_old_revno)
 
367
 
 
368
    def _get_new_revno(self):
 
369
        if self._new_revno is not None:
 
370
            return self._new_revno
 
371
        return self._lookup_revno(self.new_revid)
 
372
 
 
373
    def _set_new_revno(self, revno):
 
374
        self._new_revno = revno
 
375
    
 
376
    new_revno = property(_get_new_revno, _set_new_revno)
 
377
 
 
378
 
 
379
class GitBranchPushResult(branch.BranchPushResult):
 
380
 
 
381
    def _lookup_revno(self, revid):
 
382
        assert isinstance(revid, str), "was %r" % revid
 
383
        # Try in source branch first, it'll be faster
 
384
        try:
 
385
            return self.source_branch.revision_id_to_revno(revid)
 
386
        except errors.NoSuchRevision:
 
387
            # FIXME: Check using graph.find_distance_to_null() ?
 
388
            return self.target_branch.revision_id_to_revno(revid)
 
389
 
 
390
    @property
 
391
    def old_revno(self):
 
392
        return self._lookup_revno(self.old_revid)
 
393
 
 
394
    @property
 
395
    def new_revno(self):
 
396
        return self._lookup_revno(self.new_revid)
 
397
 
 
398
 
 
399
class InterFromGitBranch(branch.GenericInterBranch):
295
400
    """InterBranch implementation that pulls from Git into bzr."""
296
401
 
297
402
    @classmethod
298
 
    def is_compatible(self, source, target):
299
 
        return (isinstance(source, GitBranch) and 
300
 
                not isinstance(target, GitBranch))
301
 
 
302
 
    def update_revisions(self, stop_revision=None, overwrite=False,
303
 
        graph=None):
304
 
        """See InterBranch.update_revisions()."""
305
 
        interrepo = repository.InterRepository.get(self.source.repository, 
306
 
            self.target.repository)
307
 
        self._head = None
308
 
        self._last_revid = None
 
403
    def _get_interrepo(self, source, target):
 
404
        return repository.InterRepository.get(source.repository,
 
405
            target.repository)
 
406
 
 
407
    @classmethod
 
408
    def is_compatible(cls, source, target):
 
409
        return (isinstance(source, GitBranch) and
 
410
                not isinstance(target, GitBranch) and
 
411
                (getattr(cls._get_interrepo(source, target), "fetch_objects", None) is not None))
 
412
 
 
413
    def _update_revisions(self, stop_revision=None, overwrite=False,
 
414
        graph=None, limit=None):
 
415
        """Like InterBranch.update_revisions(), but with additions.
 
416
 
 
417
        Compared to the `update_revisions()` below, this function takes a
 
418
        `limit` argument that limits how many git commits will be converted
 
419
        and returns the new git head.
 
420
        """
 
421
        interrepo = self._get_interrepo(self.source, self.target)
309
422
        def determine_wants(heads):
310
 
            if not self.source.name in heads:
311
 
                raise NoSuchRef(self.source.name, heads.keys())
 
423
            if not self.source.ref in heads:
 
424
                raise NoSuchRef(self.source.ref, heads.keys())
312
425
            if stop_revision is not None:
313
426
                self._last_revid = stop_revision
314
 
                self._head, mapping = self.source.repository.lookup_git_revid(
 
427
                head, mapping = self.source.repository.lookup_bzr_revision_id(
315
428
                    stop_revision)
316
429
            else:
317
 
                self._head = heads[self.source.name]
318
 
                self._last_revid = \
319
 
                    self.source.mapping.revision_id_foreign_to_bzr(self._head)
 
430
                head = heads[self.source.ref]
 
431
                self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
 
432
                    head)
320
433
            if self.target.repository.has_revision(self._last_revid):
321
434
                return []
322
 
            return [self._head]
323
 
        interrepo.fetch_objects(determine_wants, self.source.mapping)
 
435
            return [head]
 
436
        pack_hint, head = interrepo.fetch_objects(
 
437
            determine_wants, self.source.mapping, limit=limit)
 
438
        if pack_hint is not None and self.target.repository._format.pack_compresses:
 
439
            self.target.repository.pack(hint=pack_hint)
 
440
        if head is not None:
 
441
            self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
324
442
        if overwrite:
325
443
            prev_last_revid = None
326
444
        else:
327
445
            prev_last_revid = self.target.last_revision()
328
 
        self.target.generate_revision_history(self._last_revid, prev_last_revid)
 
446
        self.target.generate_revision_history(self._last_revid,
 
447
            prev_last_revid)
 
448
        return head
 
449
 
 
450
    def update_revisions(self, stop_revision=None, overwrite=False,
 
451
                         graph=None):
 
452
        """See InterBranch.update_revisions()."""
 
453
        self._update_revisions(stop_revision, overwrite, graph)
329
454
 
330
455
    def pull(self, overwrite=False, stop_revision=None,
331
456
             possible_transports=None, _hook_master=None, run_hooks=True,
332
 
             _override_hook_target=None, local=False):
 
457
             _override_hook_target=None, local=False, limit=None):
333
458
        """See Branch.pull.
334
459
 
335
460
        :param _hook_master: Private parameter - set the branch to
339
464
            so it should not run its hooks.
340
465
        :param _override_hook_target: Private parameter - set the branch to be
341
466
            supplied as the target_branch to pull hooks.
 
467
        :param limit: Only import this many revisons.  `None`, the default,
 
468
            means import all revisions.
342
469
        """
343
470
        # This type of branch can't be bound.
344
471
        if local:
354
481
            # We assume that during 'pull' the target repository is closer than
355
482
            # the source one.
356
483
            graph = self.target.repository.get_graph(self.source.repository)
357
 
            result.old_revno, result.old_revid = \
 
484
            (result.old_revno, result.old_revid) = \
358
485
                self.target.last_revision_info()
359
 
            self.update_revisions(stop_revision, overwrite=overwrite, 
360
 
                graph=graph)
361
 
            result.new_git_head = self._head
 
486
            result.new_git_head = self._update_revisions(
 
487
                stop_revision, overwrite=overwrite, graph=graph, limit=limit)
362
488
            result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
363
489
                overwrite)
364
 
            result.new_revno, result.new_revid = self.target.last_revision_info()
 
490
            (result.new_revno, result.new_revid) = \
 
491
                self.target.last_revision_info()
365
492
            if _hook_master:
366
493
                result.master_branch = _hook_master
367
494
                result.local_branch = result.target_branch
375
502
            self.source.unlock()
376
503
        return result
377
504
 
378
 
 
379
 
class InterGitRemoteLocalBranch(branch.InterBranch):
 
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
        graph = self.target.repository.get_graph(self.source.repository)
 
510
        result.old_revno, result.old_revid = self.target.last_revision_info()
 
511
        result.new_git_head = self._update_revisions(
 
512
            stop_revision, overwrite=overwrite, graph=graph)
 
513
        result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
 
514
            overwrite)
 
515
        result.new_revno, result.new_revid = self.target.last_revision_info()
 
516
        return result
 
517
 
 
518
 
 
519
class InterGitBranch(branch.GenericInterBranch):
380
520
    """InterBranch implementation that pulls between Git branches."""
381
521
 
382
 
    @classmethod
383
 
    def is_compatible(self, source, target):
384
 
        from bzrlib.plugins.git.remote import RemoteGitBranch
385
 
        return (isinstance(source, RemoteGitBranch) and 
 
522
 
 
523
class InterGitLocalRemoteBranch(InterGitBranch):
 
524
    """InterBranch that copies from a local to a remote git branch."""
 
525
 
 
526
    @classmethod
 
527
    def is_compatible(self, source, target):
 
528
        from bzrlib.plugins.git.remote import RemoteGitBranch
 
529
        return (isinstance(source, LocalGitBranch) and
 
530
                isinstance(target, RemoteGitBranch))
 
531
 
 
532
    def _basic_push(self, overwrite=False, stop_revision=None):
 
533
        from dulwich.protocol import ZERO_SHA
 
534
        result = GitBranchPushResult()
 
535
        result.source_branch = self.source
 
536
        result.target_branch = self.target
 
537
        if stop_revision is None:
 
538
            stop_revision = self.source.last_revision()
 
539
        # FIXME: Check for diverged branches
 
540
        def get_changed_refs(old_refs):
 
541
            result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get(self.target.ref, ZERO_SHA))
 
542
            refs = { self.target.ref: self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
 
543
            result.new_revid = stop_revision
 
544
            for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
 
545
                refs[tag_name_to_ref(name)] = sha
 
546
            return refs
 
547
        self.target.repository.send_pack(get_changed_refs,
 
548
            self.source.repository._git.object_store.generate_pack_contents)
 
549
        return result
 
550
 
 
551
 
 
552
class InterGitRemoteLocalBranch(InterGitBranch):
 
553
    """InterBranch that copies from a remote to a local git branch."""
 
554
 
 
555
    @classmethod
 
556
    def is_compatible(self, source, target):
 
557
        from bzrlib.plugins.git.remote import RemoteGitBranch
 
558
        return (isinstance(source, RemoteGitBranch) and
386
559
                isinstance(target, LocalGitBranch))
387
560
 
388
 
    def pull(self, stop_revision=None, overwrite=False, 
389
 
        possible_transports=None, local=False):
390
 
        # This type of branch can't be bound.
391
 
        if local:
392
 
            raise errors.LocalRequiresBoundBranch()
393
 
        result = GitPullResult()
 
561
    def _basic_push(self, overwrite=False, stop_revision=None):
 
562
        result = branch.BranchPushResult()
394
563
        result.source_branch = self.source
395
564
        result.target_branch = self.target
396
 
        interrepo = repository.InterRepository.get(self.source.repository, 
 
565
        result.old_revid = self.target.last_revision()
 
566
        refs, stop_revision = self.update_refs(stop_revision)
 
567
        self.target.generate_revision_history(stop_revision, result.old_revid)
 
568
        self.update_tags(refs)
 
569
        result.new_revid = self.target.last_revision()
 
570
        return result
 
571
 
 
572
    def update_tags(self, refs):
 
573
        for name, v in extract_tags(refs).iteritems():
 
574
            revid = self.target.mapping.revision_id_foreign_to_bzr(v)
 
575
            self.target.tags.set_tag(name, revid)
 
576
 
 
577
    def update_refs(self, stop_revision=None):
 
578
        interrepo = repository.InterRepository.get(self.source.repository,
397
579
            self.target.repository)
398
 
        result.old_revid = self.target.last_revision()
399
580
        if stop_revision is None:
400
581
            refs = interrepo.fetch_refs(branches=["HEAD"])
401
582
            stop_revision = self.target.mapping.revision_id_foreign_to_bzr(refs["HEAD"])
402
583
        else:
403
584
            refs = interrepo.fetch_refs(revision_id=stop_revision)
 
585
        return refs, stop_revision
 
586
 
 
587
    def pull(self, stop_revision=None, overwrite=False,
 
588
        possible_transports=None, run_hooks=True,local=False):
 
589
        # This type of branch can't be bound.
 
590
        if local:
 
591
            raise errors.LocalRequiresBoundBranch()
 
592
        result = GitPullResult()
 
593
        result.source_branch = self.source
 
594
        result.target_branch = self.target
 
595
        result.old_revid = self.target.last_revision()
 
596
        refs, stop_revision = self.update_refs(stop_revision)
404
597
        self.target.generate_revision_history(stop_revision, result.old_revid)
405
 
        for name, revid in extract_tags(refs, self.target.mapping).iteritems():
406
 
            self.target.tags.set_tag(name, revid)
 
598
        self.update_tags(refs)
407
599
        result.new_revid = self.target.last_revision()
408
600
        return result
409
601
 
410
 
    
 
602
 
411
603
class InterToGitBranch(branch.InterBranch):
412
604
    """InterBranch implementation that pulls from Git into bzr."""
413
605
 
 
606
    @staticmethod
 
607
    def _get_branch_formats_to_test():
 
608
        return None, None
 
609
 
414
610
    @classmethod
415
611
    def is_compatible(self, source, target):
416
 
        return (not isinstance(source, GitBranch) and 
 
612
        return (not isinstance(source, GitBranch) and
417
613
                isinstance(target, GitBranch))
418
614
 
419
 
    def push(self, overwrite=True, stop_revision=None, 
420
 
             _override_hook_source_branch=None):
 
615
    def update_revisions(self, *args, **kwargs):
421
616
        raise NoPushSupport()
422
617
 
423
 
    def lossy_push(self, stop_revision=None):
 
618
    def _get_new_refs(self, stop_revision=None):
424
619
        if stop_revision is None:
425
620
            stop_revision = self.source.last_revision()
426
 
        # FIXME: Check for diverged branches
427
 
        refs = { "refs/heads/master": stop_revision }
 
621
        refs = { self.target.ref: stop_revision }
428
622
        for name, revid in self.source.tags.get_tag_dict().iteritems():
429
623
            if self.source.repository.has_revision(revid):
430
 
                refs["refs/tags/%s" % name] = revid
431
 
        revidmap, new_refs = self.target.repository.dfetch_refs(
 
624
                refs[tag_name_to_ref(name)] = revid
 
625
        return refs
 
626
 
 
627
    def pull(self, overwrite=False, stop_revision=None, local=False,
 
628
             possible_transports=None):
 
629
        from dulwich.protocol import ZERO_SHA
 
630
        result = GitBranchPullResult()
 
631
        result.source_branch = self.source
 
632
        result.target_branch = self.target
 
633
        # FIXME: Check for diverged branches
 
634
        old_refs = self.target.repository._git.get_refs()
 
635
        refs = dict(old_refs)
 
636
        refs.update(self._get_new_refs(stop_revision))
 
637
        self.target.repository.fetch_refs(self.source.repository, refs)
 
638
        result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(
 
639
            old_refs.get(self.target.ref, ZERO_SHA))
 
640
        result.new_revid = refs[self.target.ref]
 
641
        return result
 
642
 
 
643
    def push(self, overwrite=False, stop_revision=None,
 
644
             _override_hook_source_branch=None):
 
645
        from dulwich.protocol import ZERO_SHA
 
646
        result = GitBranchPushResult()
 
647
        result.source_branch = self.source
 
648
        result.target_branch = self.target
 
649
        # FIXME: Check for diverged branches
 
650
        old_refs = self.target.repository._git.get_refs()
 
651
        refs = dict(old_refs)
 
652
        refs.update(self._get_new_refs(stop_revision))
 
653
        self.target.repository.fetch_refs(self.source.repository, refs)
 
654
        result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(
 
655
            old_refs.get(self.target.ref, ZERO_SHA))
 
656
        result.new_revid = refs[self.target.ref]
 
657
        return result
 
658
 
 
659
    def lossy_push(self, stop_revision=None):
 
660
        from dulwich.protocol import ZERO_SHA
 
661
        result = GitBranchPushResult()
 
662
        result.source_branch = self.source
 
663
        result.target_branch = self.target
 
664
        # FIXME: Check for diverged branches
 
665
        refs = self._get_new_refs(stop_revision)
 
666
        result.revidmap, old_refs, new_refs = self.target.repository.dfetch_refs(
432
667
            self.source.repository, refs)
433
 
        if revidmap != {}:
434
 
            self.target.generate_revision_history(revidmap[stop_revision])
435
 
        return revidmap
 
668
        result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(
 
669
            old_refs.get(self.target.ref, ZERO_SHA))
 
670
        result.new_revid = self.target.mapping.revision_id_foreign_to_bzr(
 
671
            new_refs[self.target.ref])
 
672
        return result
436
673
 
437
674
 
438
675
branch.InterBranch.register_optimiser(InterGitRemoteLocalBranch)
439
676
branch.InterBranch.register_optimiser(InterFromGitBranch)
440
677
branch.InterBranch.register_optimiser(InterToGitBranch)
 
678
branch.InterBranch.register_optimiser(InterGitLocalRemoteBranch)