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 (
64
from bzrlib.plugins.git.unpeel_map import (
68
from bzrlib.foreign import ForeignBranch
71
class GitPullResult(branch.PullResult):
72
"""Result of a pull from a Git branch."""
74
def _lookup_revno(self, revid):
75
assert isinstance(revid, str), "was %r" % revid
76
# Try in source branch first, it'll be faster
77
self.target_branch.lock_read()
79
return self.target_branch.revision_id_to_revno(revid)
81
self.target_branch.unlock()
85
return self._lookup_revno(self.old_revid)
89
return self._lookup_revno(self.new_revid)
92
class GitTags(tag.BasicTags):
93
"""Ref-based tag dictionary."""
95
def __init__(self, branch):
97
self.repository = branch.repository
100
raise NotImplementedError(self.get_refs)
102
def _iter_tag_refs(self, refs):
103
raise NotImplementedError(self._iter_tag_refs)
105
def _merge_to_git(self, to_tags, refs, overwrite=False):
106
target_repo = to_tags.repository
108
for k, v in refs.iteritems():
111
if overwrite or not k in target_repo._git.refs:
112
target_repo._git.refs[k] = v
113
elif target_repo._git.refs[k] == v:
116
conflicts.append((ref_to_tag_name(k), v, target_repo.refs[k]))
119
def _merge_to_non_git(self, to_tags, refs, overwrite=False):
120
unpeeled_map = defaultdict(set)
122
result = dict(to_tags.get_tag_dict())
123
for n, peeled, unpeeled, bzr_revid in self._iter_tag_refs(refs):
124
if unpeeled is not None:
125
unpeeled_map[peeled].add(unpeeled)
126
if n not in result or overwrite:
127
result[n] = bzr_revid
128
elif result[n] == bzr_revid:
131
conflicts.append((n, result[n], bzr_revid))
132
to_tags._set_tag_dict(result)
133
if len(unpeeled_map) > 0:
134
map_file = UnpeelMap.from_repository(to_tags.branch.repository)
135
map_file.update(unpeeled_map)
136
map_file.save_in_repository(to_tags.branch.repository)
139
def merge_to(self, to_tags, overwrite=False, ignore_master=False,
141
"""See Tags.merge_to."""
142
if source_refs is None:
143
source_refs = self.get_refs()
146
if isinstance(to_tags, GitTags):
147
return self._merge_to_git(to_tags, source_refs,
153
master = to_tags.branch.get_master_branch()
154
conflicts = self._merge_to_non_git(to_tags, source_refs,
156
if master is not None:
157
conflicts += self.merge_to(master.tags, overwrite=overwrite,
158
source_refs=source_refs,
159
ignore_master=ignore_master)
162
def get_tag_dict(self):
164
refs = self.get_refs()
165
for (name, peeled, unpeeled, bzr_revid) in self._iter_tag_refs(refs):
166
ret[name] = bzr_revid
170
class LocalGitTagDict(GitTags):
171
"""Dictionary with tags in a local repository."""
173
def __init__(self, branch):
174
super(LocalGitTagDict, self).__init__(branch)
175
self.refs = self.repository._git.refs
178
return self.repository._git.get_refs()
180
def _iter_tag_refs(self, refs):
181
"""Iterate over the tag refs.
183
:param refs: Refs dictionary (name -> git sha1)
184
:return: iterator over (name, peeled_sha1, unpeeled_sha1, bzr_revid)
186
for k, (peeled, unpeeled) in extract_tags(refs).iteritems():
188
obj = self.repository._git[peeled]
190
mutter("Tag %s points at unknown object %s, ignoring", peeled,
193
# FIXME: this shouldn't really be necessary, the repository
194
# already should have these unpeeled.
195
while isinstance(obj, Tag):
196
peeled = obj.object[1]
197
obj = self.repository._git[peeled]
198
if not isinstance(obj, Commit):
199
mutter("Tag %s points at object %r that is not a commit, "
202
yield (k, peeled, unpeeled,
203
self.branch.lookup_foreign_revision_id(peeled))
205
def _set_tag_dict(self, to_dict):
206
extra = set(self.get_refs().keys())
207
for k, revid in to_dict.iteritems():
208
name = tag_name_to_ref(k)
211
self.set_tag(k, revid)
214
del self.repository._git[name]
216
def set_tag(self, name, revid):
218
git_sha, mapping = self.branch.lookup_bzr_revision_id(revid)
219
except errors.NoSuchRevision:
220
raise errors.GhostTagsNotSupported(self)
221
self.refs[tag_name_to_ref(name)] = git_sha
224
class DictTagDict(tag.BasicTags):
226
def __init__(self, branch, tags):
227
super(DictTagDict, self).__init__(branch)
230
def get_tag_dict(self):
234
class GitSymrefBranchFormat(branch.BranchFormat):
236
def get_format_description(self):
237
return 'Git Symbolic Reference Branch'
239
def network_name(self):
242
def get_reference(self, controldir, name=None):
243
return controldir.get_branch_reference(name)
245
def set_reference(self, controldir, name, target):
246
return controldir.set_branch_reference(name, target)
249
class GitBranchFormat(branch.BranchFormat):
251
def get_format_description(self):
254
def network_name(self):
257
def supports_tags(self):
260
def supports_leaving_lock(self):
263
def supports_tags_referencing_ghosts(self):
266
def tags_are_versioned(self):
270
def _matchingbzrdir(self):
271
from bzrlib.plugins.git.dir import LocalGitControlDirFormat
272
return LocalGitControlDirFormat()
274
def get_foreign_tests_branch_factory(self):
275
from bzrlib.plugins.git.tests.test_branch import ForeignTestsBranchFactory
276
return ForeignTestsBranchFactory()
278
def make_tags(self, branch):
279
if getattr(branch.repository, "get_refs", None) is not None:
280
from bzrlib.plugins.git.remote import RemoteGitTagDict
281
return RemoteGitTagDict(branch)
283
return LocalGitTagDict(branch)
285
def initialize(self, a_bzrdir, name=None, repository=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
if repository is None:
290
repository = a_bzrdir.open_repository()
291
ref = branch_name_to_ref(name, "HEAD")
292
repository._git[ref] = ZERO_SHA
293
return LocalGitBranch(a_bzrdir, repository, ref, a_bzrdir._lockfiles)
296
class GitReadLock(object):
298
def __init__(self, unlock):
302
class GitWriteLock(object):
304
def __init__(self, unlock):
305
self.branch_token = None
309
class GitBranch(ForeignBranch):
310
"""An adapter to git repositories for bzr Branch objects."""
313
def control_transport(self):
314
return self.bzrdir.control_transport
316
def __init__(self, bzrdir, repository, ref, lockfiles, tagsdict=None):
317
self.base = bzrdir.root_transport.base
318
self.repository = repository
319
self._format = GitBranchFormat()
320
self.control_files = lockfiles
322
self._lock_mode = None
324
super(GitBranch, self).__init__(repository.get_mapping())
325
if tagsdict is not None:
326
self.tags = DictTagDict(self, tagsdict)
329
self.name = ref_to_branch_name(ref)
334
def _get_checkout_format(self, lightweight=False):
335
"""Return the most suitable metadir for a checkout of this branch.
336
Weaves are used if this branch's repository uses weaves.
338
return bzrdir.format_registry.make_bzrdir("default")
340
def get_child_submit_format(self):
341
"""Return the preferred format of submissions to this branch."""
342
ret = self.get_config().get_user_option("child_submit_format")
347
def _get_nick(self, local=False, possible_master_transports=None):
348
"""Find the nick name for this branch.
352
return self.name or "HEAD"
354
def _set_nick(self, nick):
355
raise NotImplementedError
357
nick = property(_get_nick, _set_nick)
360
return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
363
def generate_revision_history(self, revid, old_revid=None):
364
if revid == NULL_REVISION:
367
# FIXME: Check that old_revid is in the ancestry of revid
368
newhead, self.mapping = self.repository.lookup_bzr_revision_id(revid)
369
if self.mapping is None:
371
self._set_head(newhead)
373
def lock_write(self, token=None):
374
if token is not None:
375
raise errors.TokenLockingNotSupported(self)
377
if self._lock_mode == 'r':
378
raise errors.ReadOnlyError(self)
379
self._lock_count += 1
381
self._lock_mode = 'w'
383
self.repository.lock_write()
384
return GitWriteLock(self.unlock)
386
def get_stacked_on_url(self):
387
# Git doesn't do stacking (yet...)
388
raise errors.UnstackableBranchFormat(self._format, self.base)
390
def get_parent(self):
391
"""See Branch.get_parent()."""
392
# FIXME: Set "origin" url from .git/config ?
395
def set_parent(self, url):
396
# FIXME: Set "origin" url in .git/config ?
401
assert self._lock_mode in ('r', 'w')
402
self._lock_count += 1
404
self._lock_mode = 'r'
406
self.repository.lock_read()
407
return GitReadLock(self.unlock)
409
def peek_lock_mode(self):
410
return self._lock_mode
413
return (self._lock_mode is not None)
416
"""See Branch.unlock()."""
417
self._lock_count -= 1
418
if self._lock_count == 0:
419
self._lock_mode = None
420
self._clear_cached_state()
421
self.repository.unlock()
423
def get_physical_lock_status(self):
427
def last_revision(self):
428
# perhaps should escape this ?
429
if self.head is None:
430
return revision.NULL_REVISION
431
return self.lookup_foreign_revision_id(self.head)
433
def _basic_push(self, target, overwrite=False, stop_revision=None):
434
return branch.InterBranch.get(self, target)._basic_push(
435
overwrite, stop_revision)
437
def lookup_foreign_revision_id(self, foreign_revid):
438
return self.repository.lookup_foreign_revision_id(foreign_revid,
441
def lookup_bzr_revision_id(self, revid):
442
return self.repository.lookup_bzr_revision_id(
443
revid, mapping=self.mapping)
446
class LocalGitBranch(GitBranch):
447
"""A local Git branch."""
449
def __init__(self, bzrdir, repository, ref, lockfiles, tagsdict=None):
450
super(LocalGitBranch, self).__init__(bzrdir, repository, ref,
452
refs = repository._git.get_refs()
453
if not (ref in refs.keys() or "HEAD" in refs.keys()):
454
raise errors.NotBranchError(self.base)
456
def create_checkout(self, to_location, revision_id=None, lightweight=False,
457
accelerator_tree=None, hardlink=False):
459
t = transport.get_transport(to_location)
461
format = self._get_checkout_format(lightweight=True)
462
checkout = format.initialize_on_transport(t)
463
from_branch = branch.BranchReferenceFormat().initialize(checkout,
465
tree = checkout.create_workingtree(revision_id,
466
from_branch=from_branch, hardlink=hardlink)
469
return self._create_heavyweight_checkout(to_location, revision_id,
472
def _create_heavyweight_checkout(self, to_location, revision_id=None,
474
"""Create a new heavyweight checkout of this branch.
476
:param to_location: URL of location to create the new checkout in.
477
:param revision_id: Revision that should be the tip of the checkout.
478
:param hardlink: Whether to hardlink
479
:return: WorkingTree object of checkout.
481
checkout_branch = bzrdir.BzrDir.create_branch_convenience(
482
to_location, force_new_tree=False,
483
format=self._get_checkout_format(lightweight=False))
484
checkout = checkout_branch.bzrdir
485
checkout_branch.bind(self)
486
# pull up to the specified revision_id to set the initial
487
# branch tip correctly, and seed it with history.
488
checkout_branch.pull(self, stop_revision=revision_id)
489
return checkout.create_workingtree(revision_id, hardlink=hardlink)
491
def _gen_revision_history(self):
492
if self.head is None:
494
graph = self.repository.get_graph()
495
ret = list(graph.iter_lefthand_ancestry(self.last_revision(),
496
(revision.NULL_REVISION, )))
502
return self.repository._git.ref(self.ref or "HEAD")
506
def _read_last_revision_info(self):
507
last_revid = self.last_revision()
508
graph = self.repository.get_graph()
509
revno = graph.find_distance_to_null(last_revid,
510
[(revision.NULL_REVISION, 0)])
511
return revno, last_revid
513
def set_last_revision_info(self, revno, revision_id):
514
self.set_last_revision(revision_id)
515
self._last_revision_info_cache = revno, revision_id
517
def set_last_revision(self, revid):
518
if not revid or not isinstance(revid, basestring):
519
raise errors.InvalidRevisionId(revision_id=revid, branch=self)
520
if revid == NULL_REVISION:
523
(newhead, self.mapping) = self.repository.lookup_bzr_revision_id(revid)
524
if self.mapping is None:
526
self._set_head(newhead)
528
def _set_head(self, value):
530
self.repository._git.refs[self.ref or "HEAD"] = self._head
531
self._clear_cached_state()
533
head = property(_get_head, _set_head)
535
def get_config(self):
536
return GitBranchConfig(self)
538
def get_push_location(self):
539
"""See Branch.get_push_location."""
540
push_loc = self.get_config().get_user_option('push_location')
543
def set_push_location(self, location):
544
"""See Branch.set_push_location."""
545
self.get_config().set_user_option('push_location', location,
546
store=config.STORE_LOCATION)
548
def supports_tags(self):
552
def _quick_lookup_revno(local_branch, remote_branch, revid):
553
assert isinstance(revid, str), "was %r" % revid
554
# Try in source branch first, it'll be faster
555
local_branch.lock_read()
558
return local_branch.revision_id_to_revno(revid)
559
except errors.NoSuchRevision:
560
graph = local_branch.repository.get_graph()
562
return graph.find_distance_to_null(revid,
563
[(revision.NULL_REVISION, 0)])
564
except errors.GhostRevisionsHaveNoRevno:
565
# FIXME: Check using graph.find_distance_to_null() ?
566
remote_branch.lock_read()
568
return remote_branch.revision_id_to_revno(revid)
570
remote_branch.unlock()
572
local_branch.unlock()
575
class GitBranchPullResult(branch.PullResult):
578
super(GitBranchPullResult, self).__init__()
579
self.new_git_head = None
580
self._old_revno = None
581
self._new_revno = None
583
def report(self, to_file):
585
if self.old_revid == self.new_revid:
586
to_file.write('No revisions to pull.\n')
587
elif self.new_git_head is not None:
588
to_file.write('Now on revision %d (git sha: %s).\n' %
589
(self.new_revno, self.new_git_head))
591
to_file.write('Now on revision %d.\n' % (self.new_revno,))
592
self._show_tag_conficts(to_file)
594
def _lookup_revno(self, revid):
595
return _quick_lookup_revno(self.target_branch, self.source_branch,
598
def _get_old_revno(self):
599
if self._old_revno is not None:
600
return self._old_revno
601
return self._lookup_revno(self.old_revid)
603
def _set_old_revno(self, revno):
604
self._old_revno = revno
606
old_revno = property(_get_old_revno, _set_old_revno)
608
def _get_new_revno(self):
609
if self._new_revno is not None:
610
return self._new_revno
611
return self._lookup_revno(self.new_revid)
613
def _set_new_revno(self, revno):
614
self._new_revno = revno
616
new_revno = property(_get_new_revno, _set_new_revno)
619
class GitBranchPushResult(branch.BranchPushResult):
621
def _lookup_revno(self, revid):
622
return _quick_lookup_revno(self.source_branch, self.target_branch,
627
return self._lookup_revno(self.old_revid)
631
new_original_revno = getattr(self, "new_original_revno", None)
632
if new_original_revno:
633
return new_original_revno
634
if getattr(self, "new_original_revid", None) is not None:
635
return self._lookup_revno(self.new_original_revid)
636
return self._lookup_revno(self.new_revid)
639
class InterFromGitBranch(branch.GenericInterBranch):
640
"""InterBranch implementation that pulls from Git into bzr."""
643
def _get_branch_formats_to_test():
645
default_format = branch.format_registry.get_default()
646
except AttributeError:
647
default_format = branch.BranchFormat._default_format
649
(GitBranchFormat(), GitBranchFormat()),
650
(GitBranchFormat(), default_format)]
653
def _get_interrepo(self, source, target):
654
return _mod_repository.InterRepository.get(source.repository, target.repository)
657
def is_compatible(cls, source, target):
658
if not isinstance(source, GitBranch):
660
if isinstance(target, GitBranch):
661
# InterLocalGitRemoteGitBranch or InterToGitBranch should be used
663
if getattr(cls._get_interrepo(source, target), "fetch_objects", None) is None:
664
# fetch_objects is necessary for this to work
668
def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
669
self.fetch_objects(stop_revision, fetch_tags=fetch_tags, limit=limit)
671
def fetch_objects(self, stop_revision, fetch_tags, limit=None):
672
interrepo = self._get_interrepo(self.source, self.target)
673
if fetch_tags is None:
674
c = self.source.get_config()
675
fetch_tags = c.get_user_option_as_bool('branch.fetch_tags')
676
def determine_wants(heads):
677
if self.source.ref is not None and not self.source.ref in heads:
678
raise NoSuchRef(self.source.ref, heads.keys())
680
if stop_revision is None:
681
if self.source.ref is not None:
682
head = heads[self.source.ref]
685
self._last_revid = self.source.lookup_foreign_revision_id(head)
687
self._last_revid = stop_revision
688
real = interrepo.get_determine_wants_revids(
689
[self._last_revid], include_tags=fetch_tags)
691
pack_hint, head, refs = interrepo.fetch_objects(
692
determine_wants, self.source.mapping, limit=limit)
693
if (pack_hint is not None and
694
self.target.repository._format.pack_compresses):
695
self.target.repository.pack(hint=pack_hint)
698
def _update_revisions(self, stop_revision=None, overwrite=False):
699
head, refs = self.fetch_objects(stop_revision, fetch_tags=None)
701
prev_last_revid = None
703
prev_last_revid = self.target.last_revision()
704
self.target.generate_revision_history(self._last_revid,
705
prev_last_revid, self.source)
708
def pull(self, overwrite=False, stop_revision=None,
709
possible_transports=None, _hook_master=None, run_hooks=True,
710
_override_hook_target=None, local=False):
713
:param _hook_master: Private parameter - set the branch to
714
be supplied as the master to pull hooks.
715
:param run_hooks: Private parameter - if false, this branch
716
is being called because it's the master of the primary branch,
717
so it should not run its hooks.
718
:param _override_hook_target: Private parameter - set the branch to be
719
supplied as the target_branch to pull hooks.
721
# This type of branch can't be bound.
723
raise errors.LocalRequiresBoundBranch()
724
result = GitBranchPullResult()
725
result.source_branch = self.source
726
if _override_hook_target is None:
727
result.target_branch = self.target
729
result.target_branch = _override_hook_target
730
self.source.lock_read()
732
self.target.lock_write()
734
# We assume that during 'pull' the target repository is closer than
736
(result.old_revno, result.old_revid) = \
737
self.target.last_revision_info()
738
result.new_git_head, remote_refs = self._update_revisions(
739
stop_revision, overwrite=overwrite)
740
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
742
(result.new_revno, result.new_revid) = \
743
self.target.last_revision_info()
745
result.master_branch = _hook_master
746
result.local_branch = result.target_branch
748
result.master_branch = result.target_branch
749
result.local_branch = None
751
for hook in branch.Branch.hooks['post_pull']:
759
def _basic_push(self, overwrite=False, stop_revision=None):
760
result = branch.BranchPushResult()
761
result.source_branch = self.source
762
result.target_branch = self.target
763
result.old_revno, result.old_revid = self.target.last_revision_info()
764
result.new_git_head, remote_refs = self._update_revisions(
765
stop_revision, overwrite=overwrite)
766
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
768
result.new_revno, result.new_revid = self.target.last_revision_info()
772
class InterGitBranch(branch.GenericInterBranch):
773
"""InterBranch implementation that pulls between Git branches."""
776
class InterLocalGitRemoteGitBranch(InterGitBranch):
777
"""InterBranch that copies from a local to a remote git branch."""
780
def _get_branch_formats_to_test():
785
def is_compatible(self, source, target):
786
from bzrlib.plugins.git.remote import RemoteGitBranch
787
return (isinstance(source, LocalGitBranch) and
788
isinstance(target, RemoteGitBranch))
790
def _basic_push(self, overwrite=False, stop_revision=None):
791
result = GitBranchPushResult()
792
result.source_branch = self.source
793
result.target_branch = self.target
794
if stop_revision is None:
795
stop_revision = self.source.last_revision()
796
# FIXME: Check for diverged branches
797
def get_changed_refs(old_refs):
798
old_ref = old_refs.get(self.target.ref, ZERO_SHA)
799
result.old_revid = self.target.lookup_foreign_revision_id(old_ref)
800
refs = { self.target.ref: self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
801
result.new_revid = stop_revision
802
for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
803
refs[tag_name_to_ref(name)] = sha
805
self.target.repository.send_pack(get_changed_refs,
806
self.source.repository._git.object_store.generate_pack_contents)
810
class InterGitLocalGitBranch(InterGitBranch):
811
"""InterBranch that copies from a remote to a local git branch."""
814
def _get_branch_formats_to_test():
819
def is_compatible(self, source, target):
820
return (isinstance(source, GitBranch) and
821
isinstance(target, LocalGitBranch))
823
def _basic_push(self, overwrite=False, stop_revision=None):
824
result = GitBranchPushResult()
825
result.source_branch = self.source
826
result.target_branch = self.target
827
result.old_revid = self.target.last_revision()
828
refs, stop_revision = self.update_refs(stop_revision)
829
self.target.generate_revision_history(stop_revision, result.old_revid)
830
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
831
source_refs=refs, overwrite=overwrite)
832
result.new_revid = self.target.last_revision()
835
def update_refs(self, stop_revision=None):
836
interrepo = _mod_repository.InterRepository.get(self.source.repository,
837
self.target.repository)
838
if stop_revision is None:
839
refs = interrepo.fetch(branches=["HEAD"])
840
stop_revision = self.target.lookup_foreign_revision_id(refs["HEAD"])
842
refs = interrepo.fetch(revision_id=stop_revision)
843
return refs, stop_revision
845
def pull(self, stop_revision=None, overwrite=False,
846
possible_transports=None, run_hooks=True,local=False):
847
# This type of branch can't be bound.
849
raise errors.LocalRequiresBoundBranch()
850
result = GitPullResult()
851
result.source_branch = self.source
852
result.target_branch = self.target
853
self.source.lock_read()
855
self.target.lock_write()
857
result.old_revid = self.target.last_revision()
858
refs, stop_revision = self.update_refs(stop_revision)
859
self.target.generate_revision_history(stop_revision, result.old_revid)
860
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
861
overwrite=overwrite, source_refs=refs)
862
result.new_revid = self.target.last_revision()
863
result.local_branch = None
864
result.master_branch = result.target_branch
866
for hook in branch.Branch.hooks['post_pull']:
875
class InterToGitBranch(branch.GenericInterBranch):
876
"""InterBranch implementation that pulls into a Git branch."""
878
def __init__(self, source, target):
879
super(InterToGitBranch, self).__init__(source, target)
880
self.interrepo = _mod_repository.InterRepository.get(source.repository,
884
def _get_branch_formats_to_test():
886
default_format = branch.format_registry.get_default()
887
except AttributeError:
888
default_format = branch.BranchFormat._default_format
889
return [(default_format, GitBranchFormat())]
892
def is_compatible(self, source, target):
893
return (not isinstance(source, GitBranch) and
894
isinstance(target, GitBranch))
896
def _get_new_refs(self, stop_revision=None, fetch_tags=None):
897
if stop_revision is None:
898
(stop_revno, stop_revision) = self.source.last_revision_info()
900
stop_revno = self.source.revision_id_to_revno(stop_revision)
901
assert type(stop_revision) is str
902
main_ref = self.target.ref or "refs/heads/master"
903
refs = { main_ref: (None, stop_revision) }
904
if fetch_tags is None:
905
c = self.source.get_config()
906
fetch_tags = c.get_user_option_as_bool('branch.fetch_tags')
908
for name, revid in self.source.tags.get_tag_dict().iteritems():
909
if self.source.repository.has_revision(revid):
910
refs[tag_name_to_ref(name)] = (None, revid)
911
return refs, main_ref, (stop_revno, stop_revision)
913
def pull(self, overwrite=False, stop_revision=None, local=False,
914
possible_transports=None, run_hooks=True):
915
result = GitBranchPullResult()
916
result.source_branch = self.source
917
result.target_branch = self.target
918
self.source.lock_read()
920
self.target.lock_write()
922
new_refs, main_ref, stop_revinfo = self._get_new_refs(stop_revision)
923
def update_refs(old_refs):
924
# FIXME: Check for diverged branches
927
result.revidmap, old_refs, new_refs = self.interrepo.fetch_refs(
928
update_refs, lossy=False)
929
except NoPushSupport:
930
raise errors.NoRoundtrippingSupport(self.source, self.target)
931
(result.old_revid, old_sha1) = old_refs.get(main_ref, (ZERO_SHA, NULL_REVISION))
932
if result.old_revid is None:
933
result.old_revid = self.target.lookup_foreign_revision_id(old_sha1)
934
result.new_revid = new_refs[main_ref][1]
935
result.local_branch = None
936
result.master_branch = self.target
938
for hook in branch.Branch.hooks['post_pull']:
946
def push(self, overwrite=False, stop_revision=None, lossy=False,
947
_override_hook_source_branch=None):
948
result = GitBranchPushResult()
949
result.source_branch = self.source
950
result.target_branch = self.target
951
result.local_branch = None
952
result.master_branch = result.target_branch
953
new_refs, main_ref, stop_revinfo = self._get_new_refs(stop_revision)
954
def update_refs(old_refs):
955
# FIXME: Check for diverged branches
958
result.revidmap, old_refs, new_refs = self.interrepo.fetch_refs(
959
update_refs, lossy=lossy)
960
except NoPushSupport:
961
raise errors.NoRoundtrippingSupport(self.source, self.target)
962
(old_sha1, result.old_revid) = old_refs.get(main_ref, (ZERO_SHA, NULL_REVISION))
963
if result.old_revid is None:
964
result.old_revid = self.target.lookup_foreign_revision_id(old_sha1)
965
result.new_revid = new_refs[main_ref][1]
966
(result.new_original_revno, result.new_original_revid) = stop_revinfo
967
for hook in branch.Branch.hooks['post_push']:
971
def lossy_push(self, stop_revision=None):
972
# For compatibility with bzr < 2.4
973
return self.push(lossy=True, stop_revision=stop_revision)
976
branch.InterBranch.register_optimiser(InterGitLocalGitBranch)
977
branch.InterBranch.register_optimiser(InterFromGitBranch)
978
branch.InterBranch.register_optimiser(InterToGitBranch)
979
branch.InterBranch.register_optimiser(InterLocalGitRemoteGitBranch)