1
# Copyright (C) 2007 Canonical Ltd
2
# Copyright (C) 2009-2010 Jelmer Vernooij <jelmer@samba.org>
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.
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.
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
18
"""An adapter between a Git Branch and a Bazaar Branch"""
20
from collections import defaultdict
22
from dulwich.objects import (
33
repository as _mod_repository,
38
from bzrlib.decorators import (
41
from bzrlib.revision import (
44
from bzrlib.trace import (
49
from bzrlib.plugins.git.config import (
52
from bzrlib.plugins.git.errors import (
56
from bzrlib.plugins.git.refs import (
63
from bzrlib.plugins.git.unpeel_map import (
67
from bzrlib.foreign import ForeignBranch
70
class GitPullResult(branch.PullResult):
71
"""Result of a pull from a Git branch."""
73
def _lookup_revno(self, revid):
74
assert isinstance(revid, str), "was %r" % revid
75
# Try in source branch first, it'll be faster
76
self.target_branch.lock_read()
78
return self.target_branch.revision_id_to_revno(revid)
80
self.target_branch.unlock()
84
return self._lookup_revno(self.old_revid)
88
return self._lookup_revno(self.new_revid)
91
class GitTags(tag.BasicTags):
92
"""Ref-based tag dictionary."""
94
def __init__(self, branch):
96
self.repository = branch.repository
99
raise NotImplementedError(self.get_refs)
101
def _iter_tag_refs(self, refs):
102
raise NotImplementedError(self._iter_tag_refs)
104
def _merge_to_git(self, to_tags, refs, overwrite=False):
105
target_repo = to_tags.repository
107
for k, v in refs.iteritems():
110
if overwrite or not k in target_repo._git.refs:
111
target_repo._git.refs[k] = v
112
elif target_repo._git.refs[k] == v:
115
conflicts.append((ref_to_tag_name(k), v, target_repo.refs[k]))
118
def _merge_to_non_git(self, to_tags, refs, overwrite=False):
119
unpeeled_map = defaultdict(set)
121
result = dict(to_tags.get_tag_dict())
122
for n, peeled, unpeeled, bzr_revid in self._iter_tag_refs(refs):
123
if unpeeled is not None:
124
unpeeled_map[peeled].add(unpeeled)
125
if n not in result or overwrite:
126
result[n] = bzr_revid
127
elif result[n] == bzr_revid:
130
conflicts.append((n, result[n], bzr_revid))
131
to_tags._set_tag_dict(result)
132
if len(unpeeled_map) > 0:
133
map_file = UnpeelMap.from_repository(to_tags.branch.repository)
134
map_file.update(unpeeled_map)
135
map_file.save_in_repository(to_tags.branch.repository)
138
def merge_to(self, to_tags, overwrite=False, ignore_master=False,
140
"""See Tags.merge_to."""
141
if source_refs is None:
142
source_refs = self.get_refs()
145
if isinstance(to_tags, GitTags):
146
return self._merge_to_git(to_tags, source_refs,
152
master = to_tags.branch.get_master_branch()
153
conflicts = self._merge_to_non_git(to_tags, source_refs,
155
if master is not None:
156
conflicts += self.merge_to(master.tags, overwrite=overwrite,
157
source_refs=source_refs,
158
ignore_master=ignore_master)
161
def get_tag_dict(self):
163
refs = self.get_refs()
164
for (name, peeled, unpeeled, bzr_revid) in self._iter_tag_refs(refs):
165
ret[name] = bzr_revid
169
class LocalGitTagDict(GitTags):
170
"""Dictionary with tags in a local repository."""
172
def __init__(self, branch):
173
super(LocalGitTagDict, self).__init__(branch)
174
self.refs = self.repository._git.refs
177
return self.repository._git.get_refs()
179
def _iter_tag_refs(self, refs):
180
"""Iterate over the tag refs.
182
:param refs: Refs dictionary (name -> git sha1)
183
:return: iterator over (name, peeled_sha1, unpeeled_sha1, bzr_revid)
185
for k, (peeled, unpeeled) in extract_tags(refs).iteritems():
187
obj = self.repository._git[peeled]
189
mutter("Tag %s points at unknown object %s, ignoring", peeled,
192
# FIXME: this shouldn't really be necessary, the repository
193
# already should have these unpeeled.
194
while isinstance(obj, Tag):
195
peeled = obj.object[1]
196
obj = self.repository._git[peeled]
197
if not isinstance(obj, Commit):
198
mutter("Tag %s points at object %r that is not a commit, "
201
yield (k, peeled, unpeeled,
202
self.branch.lookup_foreign_revision_id(peeled))
204
def _set_tag_dict(self, to_dict):
205
extra = set(self.get_refs().keys())
206
for k, revid in to_dict.iteritems():
207
name = tag_name_to_ref(k)
210
self.set_tag(k, revid)
213
del self.repository._git[name]
215
def set_tag(self, name, revid):
217
git_sha, mapping = self.branch.lookup_bzr_revision_id(revid)
218
except errors.NoSuchRevision:
219
raise errors.GhostTagsNotSupported(self)
220
self.refs[tag_name_to_ref(name)] = git_sha
223
class DictTagDict(tag.BasicTags):
225
def __init__(self, branch, tags):
226
super(DictTagDict, self).__init__(branch)
229
def get_tag_dict(self):
233
class GitSymrefBranchFormat(branch.BranchFormat):
235
def get_format_description(self):
236
return 'Git Symbolic Reference Branch'
238
def network_name(self):
241
def get_reference(self, controldir, name=None):
242
return controldir.get_branch_reference(name)
244
def set_reference(self, controldir, name, target):
245
return controldir.set_branch_reference(name, target)
248
class GitBranchFormat(branch.BranchFormat):
250
def get_format_description(self):
253
def network_name(self):
256
def supports_tags(self):
259
def supports_leaving_lock(self):
262
def supports_tags_referencing_ghosts(self):
265
def tags_are_versioned(self):
269
def _matchingbzrdir(self):
270
from bzrlib.plugins.git.dir import LocalGitControlDirFormat
271
return LocalGitControlDirFormat()
273
def get_foreign_tests_branch_factory(self):
274
from bzrlib.plugins.git.tests.test_branch import ForeignTestsBranchFactory
275
return ForeignTestsBranchFactory()
277
def make_tags(self, branch):
278
if getattr(branch.repository, "get_refs", None) is not None:
279
from bzrlib.plugins.git.remote import RemoteGitTagDict
280
return RemoteGitTagDict(branch)
282
return LocalGitTagDict(branch)
284
def initialize(self, a_bzrdir, name=None, repository=None,
285
append_revisions_only=None):
286
from bzrlib.plugins.git.dir import LocalGitDir
287
if not isinstance(a_bzrdir, LocalGitDir):
288
raise errors.IncompatibleFormat(self, a_bzrdir._format)
289
return a_bzrdir.create_branch(repository=repository, name=name,
290
append_revisions_only=append_revisions_only)
293
class GitReadLock(object):
295
def __init__(self, unlock):
299
class GitWriteLock(object):
301
def __init__(self, unlock):
302
self.branch_token = None
306
class GitBranch(ForeignBranch):
307
"""An adapter to git repositories for bzr Branch objects."""
310
def control_transport(self):
311
return self.bzrdir.control_transport
313
def __init__(self, bzrdir, repository, ref, lockfiles, tagsdict=None):
314
self.base = bzrdir.root_transport.base
315
self.repository = repository
316
self._format = GitBranchFormat()
317
self.control_files = lockfiles
319
self._lock_mode = None
321
super(GitBranch, self).__init__(repository.get_mapping())
322
if tagsdict is not None:
323
self.tags = DictTagDict(self, tagsdict)
326
self.name = ref_to_branch_name(ref)
331
def _get_checkout_format(self, lightweight=False):
332
"""Return the most suitable metadir for a checkout of this branch.
333
Weaves are used if this branch's repository uses weaves.
335
return bzrdir.format_registry.make_bzrdir("default")
337
def get_child_submit_format(self):
338
"""Return the preferred format of submissions to this branch."""
339
ret = self.get_config().get_user_option("child_submit_format")
344
def _get_nick(self, local=False, possible_master_transports=None):
345
"""Find the nick name for this branch.
349
return self.name or "HEAD"
351
def _set_nick(self, nick):
352
raise NotImplementedError
354
nick = property(_get_nick, _set_nick)
357
return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
360
def generate_revision_history(self, revid, old_revid=None):
361
if revid == NULL_REVISION:
364
# FIXME: Check that old_revid is in the ancestry of revid
365
newhead, self.mapping = self.repository.lookup_bzr_revision_id(revid)
366
if self.mapping is None:
368
self._set_head(newhead)
370
def lock_write(self, token=None):
371
if token is not None:
372
raise errors.TokenLockingNotSupported(self)
374
if self._lock_mode == 'r':
375
raise errors.ReadOnlyError(self)
376
self._lock_count += 1
378
self._lock_mode = 'w'
380
self.repository.lock_write()
381
return GitWriteLock(self.unlock)
383
def get_stacked_on_url(self):
384
# Git doesn't do stacking (yet...)
385
raise errors.UnstackableBranchFormat(self._format, self.base)
387
def get_parent(self):
388
"""See Branch.get_parent()."""
389
# FIXME: Set "origin" url from .git/config ?
392
def set_parent(self, url):
393
# FIXME: Set "origin" url in .git/config ?
398
assert self._lock_mode in ('r', 'w')
399
self._lock_count += 1
401
self._lock_mode = 'r'
403
self.repository.lock_read()
404
return GitReadLock(self.unlock)
406
def peek_lock_mode(self):
407
return self._lock_mode
410
return (self._lock_mode is not None)
413
"""See Branch.unlock()."""
414
self._lock_count -= 1
415
if self._lock_count == 0:
416
self._lock_mode = None
417
self._clear_cached_state()
418
self.repository.unlock()
420
def get_physical_lock_status(self):
424
def last_revision(self):
425
# perhaps should escape this ?
426
if self.head is None:
427
return revision.NULL_REVISION
428
return self.lookup_foreign_revision_id(self.head)
430
def _basic_push(self, target, overwrite=False, stop_revision=None):
431
return branch.InterBranch.get(self, target)._basic_push(
432
overwrite, stop_revision)
434
def lookup_foreign_revision_id(self, foreign_revid):
435
return self.repository.lookup_foreign_revision_id(foreign_revid,
438
def lookup_bzr_revision_id(self, revid):
439
return self.repository.lookup_bzr_revision_id(
440
revid, mapping=self.mapping)
443
class LocalGitBranch(GitBranch):
444
"""A local Git branch."""
446
def __init__(self, bzrdir, repository, ref, lockfiles, tagsdict=None):
447
super(LocalGitBranch, self).__init__(bzrdir, repository, ref,
449
refs = repository._git.get_refs()
450
if not (ref in refs.keys() or "HEAD" in refs.keys()):
451
raise errors.NotBranchError(self.base)
453
def create_checkout(self, to_location, revision_id=None, lightweight=False,
454
accelerator_tree=None, hardlink=False):
456
t = transport.get_transport(to_location)
458
format = self._get_checkout_format(lightweight=True)
459
checkout = format.initialize_on_transport(t)
460
from_branch = branch.BranchReferenceFormat().initialize(checkout,
462
tree = checkout.create_workingtree(revision_id,
463
from_branch=from_branch, hardlink=hardlink)
466
return self._create_heavyweight_checkout(to_location, revision_id,
469
def _create_heavyweight_checkout(self, to_location, revision_id=None,
471
"""Create a new heavyweight checkout of this branch.
473
:param to_location: URL of location to create the new checkout in.
474
:param revision_id: Revision that should be the tip of the checkout.
475
:param hardlink: Whether to hardlink
476
:return: WorkingTree object of checkout.
478
checkout_branch = bzrdir.BzrDir.create_branch_convenience(
479
to_location, force_new_tree=False,
480
format=self._get_checkout_format(lightweight=False))
481
checkout = checkout_branch.bzrdir
482
checkout_branch.bind(self)
483
# pull up to the specified revision_id to set the initial
484
# branch tip correctly, and seed it with history.
485
checkout_branch.pull(self, stop_revision=revision_id)
486
return checkout.create_workingtree(revision_id, hardlink=hardlink)
488
def _gen_revision_history(self):
489
if self.head is None:
491
graph = self.repository.get_graph()
492
ret = list(graph.iter_lefthand_ancestry(self.last_revision(),
493
(revision.NULL_REVISION, )))
499
return self.repository._git.ref(self.ref or "HEAD")
503
def _read_last_revision_info(self):
504
last_revid = self.last_revision()
505
graph = self.repository.get_graph()
506
revno = graph.find_distance_to_null(last_revid,
507
[(revision.NULL_REVISION, 0)])
508
return revno, last_revid
510
def set_last_revision_info(self, revno, revision_id):
511
self.set_last_revision(revision_id)
512
self._last_revision_info_cache = revno, revision_id
514
def set_last_revision(self, revid):
515
if not revid or not isinstance(revid, basestring):
516
raise errors.InvalidRevisionId(revision_id=revid, branch=self)
517
if revid == NULL_REVISION:
520
(newhead, self.mapping) = self.repository.lookup_bzr_revision_id(revid)
521
if self.mapping is None:
523
self._set_head(newhead)
525
def _set_head(self, value):
527
self.repository._git.refs[self.ref or "HEAD"] = self._head
528
self._clear_cached_state()
530
head = property(_get_head, _set_head)
532
def get_config(self):
533
return GitBranchConfig(self)
535
def get_push_location(self):
536
"""See Branch.get_push_location."""
537
push_loc = self.get_config().get_user_option('push_location')
540
def set_push_location(self, location):
541
"""See Branch.set_push_location."""
542
self.get_config().set_user_option('push_location', location,
543
store=config.STORE_LOCATION)
545
def supports_tags(self):
549
def _quick_lookup_revno(local_branch, remote_branch, revid):
550
assert isinstance(revid, str), "was %r" % revid
551
# Try in source branch first, it'll be faster
552
local_branch.lock_read()
555
return local_branch.revision_id_to_revno(revid)
556
except errors.NoSuchRevision:
557
graph = local_branch.repository.get_graph()
559
return graph.find_distance_to_null(revid,
560
[(revision.NULL_REVISION, 0)])
561
except errors.GhostRevisionsHaveNoRevno:
562
# FIXME: Check using graph.find_distance_to_null() ?
563
remote_branch.lock_read()
565
return remote_branch.revision_id_to_revno(revid)
567
remote_branch.unlock()
569
local_branch.unlock()
572
class GitBranchPullResult(branch.PullResult):
575
super(GitBranchPullResult, self).__init__()
576
self.new_git_head = None
577
self._old_revno = None
578
self._new_revno = None
580
def report(self, to_file):
582
if self.old_revid == self.new_revid:
583
to_file.write('No revisions to pull.\n')
584
elif self.new_git_head is not None:
585
to_file.write('Now on revision %d (git sha: %s).\n' %
586
(self.new_revno, self.new_git_head))
588
to_file.write('Now on revision %d.\n' % (self.new_revno,))
589
self._show_tag_conficts(to_file)
591
def _lookup_revno(self, revid):
592
return _quick_lookup_revno(self.target_branch, self.source_branch,
595
def _get_old_revno(self):
596
if self._old_revno is not None:
597
return self._old_revno
598
return self._lookup_revno(self.old_revid)
600
def _set_old_revno(self, revno):
601
self._old_revno = revno
603
old_revno = property(_get_old_revno, _set_old_revno)
605
def _get_new_revno(self):
606
if self._new_revno is not None:
607
return self._new_revno
608
return self._lookup_revno(self.new_revid)
610
def _set_new_revno(self, revno):
611
self._new_revno = revno
613
new_revno = property(_get_new_revno, _set_new_revno)
616
class GitBranchPushResult(branch.BranchPushResult):
618
def _lookup_revno(self, revid):
619
return _quick_lookup_revno(self.source_branch, self.target_branch,
624
return self._lookup_revno(self.old_revid)
628
new_original_revno = getattr(self, "new_original_revno", None)
629
if new_original_revno:
630
return new_original_revno
631
if getattr(self, "new_original_revid", None) is not None:
632
return self._lookup_revno(self.new_original_revid)
633
return self._lookup_revno(self.new_revid)
636
class InterFromGitBranch(branch.GenericInterBranch):
637
"""InterBranch implementation that pulls from Git into bzr."""
640
def _get_branch_formats_to_test():
642
default_format = branch.format_registry.get_default()
643
except AttributeError:
644
default_format = branch.BranchFormat._default_format
646
(GitBranchFormat(), GitBranchFormat()),
647
(GitBranchFormat(), default_format)]
650
def _get_interrepo(self, source, target):
651
return _mod_repository.InterRepository.get(source.repository, target.repository)
654
def is_compatible(cls, source, target):
655
if not isinstance(source, GitBranch):
657
if isinstance(target, GitBranch):
658
# InterLocalGitRemoteGitBranch or InterToGitBranch should be used
660
if getattr(cls._get_interrepo(source, target), "fetch_objects", None) is None:
661
# fetch_objects is necessary for this to work
665
def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
666
self.fetch_objects(stop_revision, fetch_tags=fetch_tags, limit=limit)
668
def fetch_objects(self, stop_revision, fetch_tags, limit=None):
669
interrepo = self._get_interrepo(self.source, self.target)
670
if fetch_tags is None:
671
c = self.source.get_config()
672
fetch_tags = c.get_user_option_as_bool('branch.fetch_tags')
673
def determine_wants(heads):
674
if self.source.ref is not None and not self.source.ref in heads:
675
raise NoSuchRef(self.source.ref, self.source.user_url, heads.keys())
677
if stop_revision is None:
678
if self.source.ref is not None:
679
head = heads[self.source.ref]
682
self._last_revid = self.source.lookup_foreign_revision_id(head)
684
self._last_revid = stop_revision
685
real = interrepo.get_determine_wants_revids(
686
[self._last_revid], include_tags=fetch_tags)
688
pack_hint, head, refs = interrepo.fetch_objects(
689
determine_wants, self.source.mapping, limit=limit)
690
if (pack_hint is not None and
691
self.target.repository._format.pack_compresses):
692
self.target.repository.pack(hint=pack_hint)
695
def _update_revisions(self, stop_revision=None, overwrite=False):
696
head, refs = self.fetch_objects(stop_revision, fetch_tags=None)
698
prev_last_revid = None
700
prev_last_revid = self.target.last_revision()
701
self.target.generate_revision_history(self._last_revid,
702
prev_last_revid, self.source)
705
def pull(self, overwrite=False, stop_revision=None,
706
possible_transports=None, _hook_master=None, run_hooks=True,
707
_override_hook_target=None, local=False):
710
:param _hook_master: Private parameter - set the branch to
711
be supplied as the master to pull hooks.
712
:param run_hooks: Private parameter - if false, this branch
713
is being called because it's the master of the primary branch,
714
so it should not run its hooks.
715
:param _override_hook_target: Private parameter - set the branch to be
716
supplied as the target_branch to pull hooks.
718
# This type of branch can't be bound.
720
raise errors.LocalRequiresBoundBranch()
721
result = GitBranchPullResult()
722
result.source_branch = self.source
723
if _override_hook_target is None:
724
result.target_branch = self.target
726
result.target_branch = _override_hook_target
727
self.source.lock_read()
729
self.target.lock_write()
731
# We assume that during 'pull' the target repository is closer than
733
(result.old_revno, result.old_revid) = \
734
self.target.last_revision_info()
735
result.new_git_head, remote_refs = self._update_revisions(
736
stop_revision, overwrite=overwrite)
737
result.tag_conflicts = self.source.tags.merge_to(
738
self.target.tags, overwrite)
739
(result.new_revno, result.new_revid) = \
740
self.target.last_revision_info()
742
result.master_branch = _hook_master
743
result.local_branch = result.target_branch
745
result.master_branch = result.target_branch
746
result.local_branch = None
748
for hook in branch.Branch.hooks['post_pull']:
756
def _basic_push(self, overwrite=False, stop_revision=None):
757
result = branch.BranchPushResult()
758
result.source_branch = self.source
759
result.target_branch = self.target
760
result.old_revno, result.old_revid = self.target.last_revision_info()
761
result.new_git_head, remote_refs = self._update_revisions(
762
stop_revision, overwrite=overwrite)
763
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
765
result.new_revno, result.new_revid = self.target.last_revision_info()
769
class InterGitBranch(branch.GenericInterBranch):
770
"""InterBranch implementation that pulls between Git branches."""
773
class InterLocalGitRemoteGitBranch(InterGitBranch):
774
"""InterBranch that copies from a local to a remote git branch."""
777
def _get_branch_formats_to_test():
782
def is_compatible(self, source, target):
783
from bzrlib.plugins.git.remote import RemoteGitBranch
784
return (isinstance(source, LocalGitBranch) and
785
isinstance(target, RemoteGitBranch))
787
def _basic_push(self, overwrite=False, stop_revision=None):
788
result = GitBranchPushResult()
789
result.source_branch = self.source
790
result.target_branch = self.target
791
if stop_revision is None:
792
stop_revision = self.source.last_revision()
793
# FIXME: Check for diverged branches
794
def get_changed_refs(old_refs):
795
old_ref = old_refs.get(self.target.ref, ZERO_SHA)
796
result.old_revid = self.target.lookup_foreign_revision_id(old_ref)
797
refs = { self.target.ref: self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
798
result.new_revid = stop_revision
799
for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
800
refs[tag_name_to_ref(name)] = sha
802
self.target.repository.send_pack(get_changed_refs,
803
self.source.repository._git.object_store.generate_pack_contents)
807
class InterGitLocalGitBranch(InterGitBranch):
808
"""InterBranch that copies from a remote to a local git branch."""
811
def _get_branch_formats_to_test():
816
def is_compatible(self, source, target):
817
return (isinstance(source, GitBranch) and
818
isinstance(target, LocalGitBranch))
820
def _basic_push(self, overwrite=False, stop_revision=None):
821
result = GitBranchPushResult()
822
result.source_branch = self.source
823
result.target_branch = self.target
824
result.old_revid = self.target.last_revision()
825
refs, stop_revision = self.update_refs(stop_revision)
826
self.target.generate_revision_history(stop_revision, result.old_revid)
827
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
828
source_refs=refs, overwrite=overwrite)
829
result.new_revid = self.target.last_revision()
832
def update_refs(self, stop_revision=None):
833
interrepo = _mod_repository.InterRepository.get(self.source.repository,
834
self.target.repository)
835
if stop_revision is None:
836
refs = interrepo.fetch(branches=["HEAD"])
837
stop_revision = self.target.lookup_foreign_revision_id(refs["HEAD"])
839
refs = interrepo.fetch(revision_id=stop_revision)
840
return refs, stop_revision
842
def pull(self, stop_revision=None, overwrite=False,
843
possible_transports=None, run_hooks=True,local=False):
844
# This type of branch can't be bound.
846
raise errors.LocalRequiresBoundBranch()
847
result = GitPullResult()
848
result.source_branch = self.source
849
result.target_branch = self.target
850
self.source.lock_read()
852
self.target.lock_write()
854
result.old_revid = self.target.last_revision()
855
refs, stop_revision = self.update_refs(stop_revision)
856
self.target.generate_revision_history(stop_revision, result.old_revid)
857
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
858
overwrite=overwrite, source_refs=refs)
859
result.new_revid = self.target.last_revision()
860
result.local_branch = None
861
result.master_branch = result.target_branch
863
for hook in branch.Branch.hooks['post_pull']:
872
class InterToGitBranch(branch.GenericInterBranch):
873
"""InterBranch implementation that pulls into a Git branch."""
875
def __init__(self, source, target):
876
super(InterToGitBranch, self).__init__(source, target)
877
self.interrepo = _mod_repository.InterRepository.get(source.repository,
881
def _get_branch_formats_to_test():
883
default_format = branch.format_registry.get_default()
884
except AttributeError:
885
default_format = branch.BranchFormat._default_format
886
return [(default_format, GitBranchFormat())]
889
def is_compatible(self, source, target):
890
return (not isinstance(source, GitBranch) and
891
isinstance(target, GitBranch))
893
def _get_new_refs(self, stop_revision=None, fetch_tags=None):
894
if stop_revision is None:
895
(stop_revno, stop_revision) = self.source.last_revision_info()
897
stop_revno = self.source.revision_id_to_revno(stop_revision)
898
assert type(stop_revision) is str
899
main_ref = self.target.ref or "refs/heads/master"
900
refs = { main_ref: (None, stop_revision) }
901
if fetch_tags is None:
902
c = self.source.get_config()
903
fetch_tags = c.get_user_option_as_bool('branch.fetch_tags')
905
for name, revid in self.source.tags.get_tag_dict().iteritems():
906
if self.source.repository.has_revision(revid):
907
refs[tag_name_to_ref(name)] = (None, revid)
908
return refs, main_ref, (stop_revno, stop_revision)
910
def pull(self, overwrite=False, stop_revision=None, local=False,
911
possible_transports=None, run_hooks=True):
912
result = GitBranchPullResult()
913
result.source_branch = self.source
914
result.target_branch = self.target
915
self.source.lock_read()
917
self.target.lock_write()
919
new_refs, main_ref, stop_revinfo = self._get_new_refs(stop_revision)
920
def update_refs(old_refs):
921
mutter("updating refs. old refs: %r, new refs: %r",
923
# FIXME: Check for diverged branches
926
result.revidmap, old_refs, new_refs = self.interrepo.fetch_refs(
927
update_refs, lossy=False)
928
except NoPushSupport:
929
raise errors.NoRoundtrippingSupport(self.source, self.target)
930
(result.old_revid, old_sha1) = old_refs.get(main_ref, (ZERO_SHA, NULL_REVISION))
931
if result.old_revid is None:
932
result.old_revid = self.target.lookup_foreign_revision_id(old_sha1)
933
result.new_revid = new_refs[main_ref][1]
934
result.local_branch = None
935
result.master_branch = self.target
937
for hook in branch.Branch.hooks['post_pull']:
945
def push(self, overwrite=False, stop_revision=None, lossy=False,
946
_override_hook_source_branch=None):
947
result = GitBranchPushResult()
948
result.source_branch = self.source
949
result.target_branch = self.target
950
result.local_branch = None
951
result.master_branch = result.target_branch
952
new_refs, main_ref, stop_revinfo = self._get_new_refs(stop_revision)
953
def update_refs(old_refs):
954
mutter("updating refs. old refs: %r, new refs: %r",
956
# FIXME: Check for diverged branches
959
result.revidmap, old_refs, new_refs = self.interrepo.fetch_refs(
960
update_refs, lossy=lossy)
961
except NoPushSupport:
962
raise errors.NoRoundtrippingSupport(self.source, self.target)
963
(old_sha1, result.old_revid) = old_refs.get(main_ref, (ZERO_SHA, NULL_REVISION))
964
if result.old_revid is None:
965
result.old_revid = self.target.lookup_foreign_revision_id(old_sha1)
966
result.new_revid = new_refs[main_ref][1]
967
(result.new_original_revno, result.new_original_revid) = stop_revinfo
968
for hook in branch.Branch.hooks['post_push']:
972
def lossy_push(self, stop_revision=None):
973
# For compatibility with bzr < 2.4
974
return self.push(lossy=True, stop_revision=stop_revision)
977
branch.InterBranch.register_optimiser(InterGitLocalGitBranch)
978
branch.InterBranch.register_optimiser(InterFromGitBranch)
979
branch.InterBranch.register_optimiser(InterToGitBranch)
980
branch.InterBranch.register_optimiser(InterLocalGitRemoteGitBranch)