76
88
return self._lookup_revno(self.new_revid)
79
class LocalGitTagDict(tag.BasicTags):
80
"""Dictionary with tags in a local repository."""
91
class GitTags(tag.BasicTags):
92
"""Ref-based tag dictionary."""
82
94
def __init__(self, branch):
83
95
self.branch = branch
84
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)
86
161
def get_tag_dict(self):
88
for k,v in extract_tags(self.repository._git.get_refs()).iteritems():
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():
90
obj = self.repository._git[v]
187
obj = self.repository._git[peeled]
92
mutter("Tag %s points at unknown object %s, ignoring", v, obj)
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.
94
194
while isinstance(obj, Tag):
96
obj = self.repository._git[v]
195
peeled = obj.object[1]
196
obj = self.repository._git[peeled]
97
197
if not isinstance(obj, Commit):
98
198
mutter("Tag %s points at object %r that is not a commit, "
99
199
"ignoring", k, obj)
101
ret[k] = self.branch.lookup_foreign_revision_id(v)
201
yield (k, peeled, unpeeled,
202
self.branch.lookup_foreign_revision_id(peeled))
104
204
def _set_tag_dict(self, to_dict):
105
extra = set(self.repository._git.get_refs().keys())
205
extra = set(self.get_refs().keys())
106
206
for k, revid in to_dict.iteritems():
107
207
name = tag_name_to_ref(k)
108
208
if name in extra:
109
209
extra.remove(name)
110
210
self.set_tag(k, revid)
111
211
for name in extra:
112
if name.startswith("refs/tags/"):
113
213
del self.repository._git[name]
115
215
def set_tag(self, name, revid):
116
self.repository._git.refs[tag_name_to_ref(name)], _ = \
117
self.branch.lookup_bzr_revision_id(revid)
120
class DictTagDict(LocalGitTagDict):
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):
122
225
def __init__(self, branch, tags):
123
226
super(DictTagDict, self).__init__(branch)
427
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)]
431
650
def _get_interrepo(self, source, target):
432
return repository.InterRepository.get(source.repository,
651
return _mod_repository.InterRepository.get(source.repository, target.repository)
436
654
def is_compatible(cls, source, target):
437
return (isinstance(source, GitBranch) and
438
not isinstance(target, GitBranch) and
439
(getattr(cls._get_interrepo(source, target), "fetch_objects", None) is not None))
441
def _update_revisions(self, stop_revision=None, overwrite=False,
442
graph=None, limit=None):
443
"""Like InterBranch.update_revisions(), but with additions.
445
Compared to the `update_revisions()` below, this function takes a
446
`limit` argument that limits how many git commits will be converted
447
and returns the new git head.
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):
449
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')
450
673
def determine_wants(heads):
451
674
if self.source.ref is not None and not self.source.ref in heads:
452
675
raise NoSuchRef(self.source.ref, heads.keys())
453
if stop_revision is not None:
454
self._last_revid = stop_revision
455
head, mapping = self.source.repository.lookup_bzr_revision_id(
677
if stop_revision is None:
458
678
if self.source.ref is not None:
459
679
head = heads[self.source.ref]
461
681
head = heads["HEAD"]
462
682
self._last_revid = self.source.lookup_foreign_revision_id(head)
463
if self.target.repository.has_revision(self._last_revid):
684
self._last_revid = stop_revision
685
real = interrepo.get_determine_wants_revids(
686
[self._last_revid], include_tags=fetch_tags)
466
688
pack_hint, head, refs = interrepo.fetch_objects(
467
689
determine_wants, self.source.mapping, limit=limit)
468
690
if (pack_hint is not None and
469
691
self.target.repository._format.pack_compresses):
470
692
self.target.repository.pack(hint=pack_hint)
472
self._last_revid = self.source.lookup_foreign_revision_id(head)
695
def _update_revisions(self, stop_revision=None, overwrite=False):
696
head, refs = self.fetch_objects(stop_revision, fetch_tags=None)
474
698
prev_last_revid = None
476
700
prev_last_revid = self.target.last_revision()
477
701
self.target.generate_revision_history(self._last_revid,
481
def update_revisions(self, stop_revision=None, overwrite=False,
483
"""See InterBranch.update_revisions()."""
484
self._update_revisions(stop_revision, overwrite, graph)
702
prev_last_revid, self.source)
486
705
def pull(self, overwrite=False, stop_revision=None,
487
706
possible_transports=None, _hook_master=None, run_hooks=True,
488
_override_hook_target=None, local=False, limit=None):
707
_override_hook_target=None, local=False):
489
708
"""See Branch.pull.
491
710
:param _hook_master: Private parameter - set the branch to
509
726
result.target_branch = _override_hook_target
510
727
self.source.lock_read()
512
# We assume that during 'pull' the target repository is closer than
514
graph = self.target.repository.get_graph(self.source.repository)
515
(result.old_revno, result.old_revid) = \
516
self.target.last_revision_info()
517
result.new_git_head = self._update_revisions(
518
stop_revision, overwrite=overwrite, graph=graph, limit=limit)
519
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
521
(result.new_revno, result.new_revid) = \
522
self.target.last_revision_info()
524
result.master_branch = _hook_master
525
result.local_branch = result.target_branch
527
result.master_branch = result.target_branch
528
result.local_branch = None
530
for hook in branch.Branch.hooks['post_pull']:
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(self.target.tags,
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']:
533
753
self.source.unlock()
587
class InterGitRemoteLocalBranch(InterGitBranch):
807
class InterGitLocalGitBranch(InterGitBranch):
588
808
"""InterBranch that copies from a remote to a local git branch."""
591
811
def _get_branch_formats_to_test():
595
816
def is_compatible(self, source, target):
596
from bzrlib.plugins.git.remote import RemoteGitBranch
597
return (isinstance(source, RemoteGitBranch) and
817
return (isinstance(source, GitBranch) and
598
818
isinstance(target, LocalGitBranch))
600
820
def _basic_push(self, overwrite=False, stop_revision=None):
601
result = branch.BranchPushResult()
821
result = GitBranchPushResult()
602
822
result.source_branch = self.source
603
823
result.target_branch = self.target
604
824
result.old_revid = self.target.last_revision()
605
825
refs, stop_revision = self.update_refs(stop_revision)
606
826
self.target.generate_revision_history(stop_revision, result.old_revid)
607
self.update_tags(refs)
827
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
828
source_refs=refs, overwrite=overwrite)
608
829
result.new_revid = self.target.last_revision()
611
def update_tags(self, refs):
612
for name, v in extract_tags(refs).iteritems():
613
revid = self.target.lookup_foreign_revision_id(v)
614
self.target.tags.set_tag(name, revid)
616
832
def update_refs(self, stop_revision=None):
617
interrepo = repository.InterRepository.get(self.source.repository,
833
interrepo = _mod_repository.InterRepository.get(self.source.repository,
618
834
self.target.repository)
619
835
if stop_revision is None:
620
836
refs = interrepo.fetch(branches=["HEAD"])
631
847
result = GitPullResult()
632
848
result.source_branch = self.source
633
849
result.target_branch = self.target
634
result.old_revid = self.target.last_revision()
635
refs, stop_revision = self.update_refs(stop_revision)
636
self.target.generate_revision_history(stop_revision, result.old_revid)
637
self.update_tags(refs)
638
result.new_revid = self.target.last_revision()
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']:
642
872
class InterToGitBranch(branch.GenericInterBranch):
643
"""InterBranch implementation that pulls from Git into bzr."""
873
"""InterBranch implementation that pulls into a Git branch."""
645
875
def __init__(self, source, target):
646
876
super(InterToGitBranch, self).__init__(source, target)
647
self.interrepo = repository.InterRepository.get(source.repository,
877
self.interrepo = _mod_repository.InterRepository.get(source.repository,
648
878
target.repository)
651
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())]
655
889
def is_compatible(self, source, target):
656
890
return (not isinstance(source, GitBranch) and
657
891
isinstance(target, GitBranch))
659
def update_revisions(self, *args, **kwargs):
660
raise NoPushSupport()
662
def _get_new_refs(self, stop_revision=None):
893
def _get_new_refs(self, stop_revision=None, fetch_tags=None):
663
894
if stop_revision is None:
664
stop_revision = self.source.last_revision()
895
(stop_revno, stop_revision) = self.source.last_revision_info()
897
stop_revno = self.source.revision_id_to_revno(stop_revision)
665
898
assert type(stop_revision) is str
666
899
main_ref = self.target.ref or "refs/heads/master"
667
900
refs = { main_ref: (None, stop_revision) }
668
for name, revid in self.source.tags.get_tag_dict().iteritems():
669
if self.source.repository.has_revision(revid):
670
refs[tag_name_to_ref(name)] = (None, revid)
671
return refs, main_ref
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)
673
910
def pull(self, overwrite=False, stop_revision=None, local=False,
674
possible_transports=None):
675
from dulwich.protocol import ZERO_SHA
911
possible_transports=None, run_hooks=True):
676
912
result = GitBranchPullResult()
677
913
result.source_branch = self.source
678
914
result.target_branch = self.target
679
new_refs, main_ref = self._get_new_refs(stop_revision)
680
def update_refs(old_refs):
681
refs = dict(old_refs)
682
# FIXME: Check for diverged branches
683
refs.update(new_refs)
685
old_refs, new_refs = self.interrepo.fetch_refs(update_refs)
686
result.old_revid = self.target.lookup_foreign_revision_id(
687
old_refs.get(main_ref, ZERO_SHA))
688
result.new_revid = new_refs[main_ref]
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']:
691
def push(self, overwrite=False, stop_revision=None,
945
def push(self, overwrite=False, stop_revision=None, lossy=False,
692
946
_override_hook_source_branch=None):
693
from dulwich.protocol import ZERO_SHA
694
947
result = GitBranchPushResult()
695
948
result.source_branch = self.source
696
949
result.target_branch = self.target
697
new_refs, main_ref = self._get_new_refs(stop_revision)
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)
698
953
def update_refs(old_refs):
699
refs = dict(old_refs)
954
mutter("updating refs. old refs: %r, new refs: %r",
700
956
# FIXME: Check for diverged branches
701
refs.update(new_refs)
703
old_refs, new_refs = self.interrepo.fetch_refs(update_refs)
704
(result.old_revid, old_sha1) = old_refs.get(main_ref, (ZERO_SHA, NULL_REVISION))
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))
705
964
if result.old_revid is None:
706
965
result.old_revid = self.target.lookup_foreign_revision_id(old_sha1)
707
result.new_revid = new_refs[main_ref]
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']:
710
972
def lossy_push(self, stop_revision=None):
711
result = GitBranchPushResult()
712
result.source_branch = self.source
713
result.target_branch = self.target
714
new_refs, main_ref = self._get_new_refs(stop_revision)
715
def update_refs(old_refs):
716
refs = dict(old_refs)
717
# FIXME: Check for diverged branches
718
refs.update(new_refs)
720
result.revidmap, old_refs, new_refs = self.interrepo.dfetch_refs(
722
result.old_revid = old_refs.get(self.target.ref, (None, NULL_REVISION))[1]
723
result.new_revid = new_refs[main_ref][1]
727
branch.InterBranch.register_optimiser(InterGitRemoteLocalBranch)
973
# For compatibility with bzr < 2.4
974
return self.push(lossy=True, stop_revision=stop_revision)
977
branch.InterBranch.register_optimiser(InterGitLocalGitBranch)
728
978
branch.InterBranch.register_optimiser(InterFromGitBranch)
729
979
branch.InterBranch.register_optimiser(InterToGitBranch)
730
branch.InterBranch.register_optimiser(InterGitLocalRemoteBranch)
980
branch.InterBranch.register_optimiser(InterLocalGitRemoteGitBranch)