1
# Copyright (C) 2007,2012 Canonical Ltd
2
# Copyright (C) 2009-2018 Jelmer Vernooij <jelmer@jelmer.uk>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
"""An adapter between a Git Branch and a Bazaar Branch"""
20
from __future__ import absolute_import
22
from io import BytesIO
23
from collections import defaultdict
25
from dulwich.objects import (
29
from dulwich.repo import check_ref_format
37
repository as _mod_repository,
44
from ..foreign import ForeignBranch
45
from ..revision import (
48
from ..sixish import (
73
remote_refs_dict_to_tag_refs,
76
from .unpeel_map import (
85
class GitPullResult(branch.PullResult):
86
"""Result of a pull from a Git branch."""
88
def _lookup_revno(self, revid):
89
if not isinstance(revid, bytes):
90
raise TypeError(revid)
91
# Try in source branch first, it'll be faster
92
with self.target_branch.lock_read():
93
return self.target_branch.revision_id_to_revno(revid)
97
return self._lookup_revno(self.old_revid)
101
return self._lookup_revno(self.new_revid)
104
class GitTags(tag.BasicTags):
105
"""Ref-based tag dictionary."""
107
def __init__(self, branch):
109
self.repository = branch.repository
111
def _merge_to_remote_git(self, target_repo, source_tag_refs,
116
def get_changed_refs(old_refs):
118
for ref_name, tag_name, peeled, unpeeled in (
119
source_tag_refs.iteritems()):
120
if old_refs.get(ref_name) == unpeeled:
122
elif overwrite or ref_name not in old_refs:
123
ret[ref_name] = unpeeled
124
updates[tag_name] = target_repo.lookup_foreign_revision_id(
129
self.repository.lookup_foreign_revision_id(peeled),
130
target_repo.lookup_foreign_revision_id(
131
old_refs[ref_name])))
133
target_repo.controldir.send_pack(
134
get_changed_refs, lambda have, want: [])
135
return updates, conflicts
137
def _merge_to_local_git(self, target_repo, source_tag_refs,
141
for ref_name, tag_name, peeled, unpeeled in source_tag_refs:
142
if target_repo._git.refs.get(ref_name) == unpeeled:
144
elif overwrite or ref_name not in target_repo._git.refs:
145
target_repo._git.refs[ref_name] = unpeeled or peeled
147
updates[tag_name] = (
148
self.repository.lookup_foreign_revision_id(peeled))
150
trace.warning('%s does not point to a valid object',
155
source_revid = self.repository.lookup_foreign_revision_id(
157
target_revid = target_repo.lookup_foreign_revision_id(
158
target_repo._git.refs[ref_name])
160
trace.warning('%s does not point to a valid object',
163
conflicts.append((tag_name, source_revid, target_revid))
164
return updates, conflicts
166
def _merge_to_git(self, to_tags, source_tag_refs, overwrite=False):
167
target_repo = to_tags.repository
168
if self.repository.has_same_location(target_repo):
171
if getattr(target_repo, "_git", None):
172
return self._merge_to_local_git(
173
target_repo, source_tag_refs, overwrite)
175
return self._merge_to_remote_git(
176
target_repo, source_tag_refs, overwrite)
178
to_tags.branch._tag_refs = None
180
def _merge_to_non_git(self, to_tags, source_tag_refs, overwrite=False):
181
unpeeled_map = defaultdict(set)
184
result = dict(to_tags.get_tag_dict())
185
for ref_name, tag_name, peeled, unpeeled in source_tag_refs:
186
if unpeeled is not None:
187
unpeeled_map[peeled].add(unpeeled)
189
bzr_revid = self.branch.lookup_foreign_revision_id(peeled)
190
except NotCommitError:
192
if result.get(tag_name) == bzr_revid:
194
elif tag_name not in result or overwrite:
195
result[tag_name] = bzr_revid
196
updates[tag_name] = bzr_revid
198
conflicts.append((tag_name, bzr_revid, result[tag_name]))
199
to_tags._set_tag_dict(result)
200
if len(unpeeled_map) > 0:
201
map_file = UnpeelMap.from_repository(to_tags.branch.repository)
202
map_file.update(unpeeled_map)
203
map_file.save_in_repository(to_tags.branch.repository)
204
return updates, conflicts
206
def merge_to(self, to_tags, overwrite=False, ignore_master=False,
207
source_tag_refs=None):
208
"""See Tags.merge_to."""
209
if source_tag_refs is None:
210
source_tag_refs = self.branch.get_tag_refs()
213
if isinstance(to_tags, GitTags):
214
return self._merge_to_git(to_tags, source_tag_refs,
220
master = to_tags.branch.get_master_branch()
221
if master is not None:
224
updates, conflicts = self._merge_to_non_git(
225
to_tags, source_tag_refs, overwrite=overwrite)
226
if master is not None:
227
extra_updates, extra_conflicts = self.merge_to(
228
master.tags, overwrite=overwrite,
229
source_tag_refs=source_tag_refs,
230
ignore_master=ignore_master)
231
updates.update(extra_updates)
232
conflicts += extra_conflicts
233
return updates, conflicts
235
if master is not None:
238
def get_tag_dict(self):
240
for (ref_name, tag_name, peeled, unpeeled) in (
241
self.branch.get_tag_refs()):
243
bzr_revid = self.branch.lookup_foreign_revision_id(peeled)
244
except NotCommitError:
247
ret[tag_name] = bzr_revid
251
class LocalGitTagDict(GitTags):
252
"""Dictionary with tags in a local repository."""
254
def __init__(self, branch):
255
super(LocalGitTagDict, self).__init__(branch)
256
self.refs = self.repository.controldir._git.refs
258
def _set_tag_dict(self, to_dict):
259
extra = set(self.refs.allkeys())
260
for k, revid in viewitems(to_dict):
261
name = tag_name_to_ref(k)
264
self.set_tag(k, revid)
267
del self.repository._git[name]
269
def set_tag(self, name, revid):
271
git_sha, mapping = self.branch.lookup_bzr_revision_id(revid)
272
except errors.NoSuchRevision:
273
raise errors.GhostTagsNotSupported(self)
274
self.refs[tag_name_to_ref(name)] = git_sha
275
self.branch._tag_refs = None
277
def delete_tag(self, name):
278
ref = tag_name_to_ref(name)
279
if ref not in self.refs:
280
raise errors.NoSuchTag(name)
282
self.branch._tag_refs = None
285
class GitBranchFormat(branch.BranchFormat):
287
def network_name(self):
290
def supports_tags(self):
293
def supports_leaving_lock(self):
296
def supports_tags_referencing_ghosts(self):
299
def tags_are_versioned(self):
302
def get_foreign_tests_branch_factory(self):
303
from .tests.test_branch import ForeignTestsBranchFactory
304
return ForeignTestsBranchFactory()
306
def make_tags(self, branch):
309
except AttributeError:
311
if getattr(branch.repository, "_git", None) is None:
312
from .remote import RemoteGitTagDict
313
return RemoteGitTagDict(branch)
315
return LocalGitTagDict(branch)
317
def initialize(self, a_controldir, name=None, repository=None,
318
append_revisions_only=None):
319
raise NotImplementedError(self.initialize)
321
def get_reference(self, controldir, name=None):
322
return controldir.get_branch_reference(name=name)
324
def set_reference(self, controldir, name, target):
325
return controldir.set_branch_reference(target, name)
328
class LocalGitBranchFormat(GitBranchFormat):
330
def get_format_description(self):
331
return 'Local Git Branch'
334
def _matchingcontroldir(self):
335
from .dir import LocalGitControlDirFormat
336
return LocalGitControlDirFormat()
338
def initialize(self, a_controldir, name=None, repository=None,
339
append_revisions_only=None):
340
from .dir import LocalGitDir
341
if not isinstance(a_controldir, LocalGitDir):
342
raise errors.IncompatibleFormat(self, a_controldir._format)
343
return a_controldir.create_branch(
344
repository=repository, name=name,
345
append_revisions_only=append_revisions_only)
348
class GitBranch(ForeignBranch):
349
"""An adapter to git repositories for bzr Branch objects."""
352
def control_transport(self):
353
return self._control_transport
356
def user_transport(self):
357
return self._user_transport
359
def __init__(self, controldir, repository, ref, format):
360
self.repository = repository
361
self._format = format
362
self.controldir = controldir
363
self._lock_mode = None
365
super(GitBranch, self).__init__(repository.get_mapping())
368
self._user_transport = controldir.user_transport.clone('.')
369
self._control_transport = controldir.control_transport.clone('.')
370
self._tag_refs = None
373
self.name = ref_to_branch_name(ref)
376
if self.ref is not None:
377
params = {"ref": urlutils.escape(self.ref)}
380
params = {"branch": urlutils.escape(self.name)}
381
for k, v in params.items():
382
self._user_transport.set_segment_parameter(k, v)
383
self._control_transport.set_segment_parameter(k, v)
384
self.base = controldir.user_transport.base
386
def _get_checkout_format(self, lightweight=False):
387
"""Return the most suitable metadir for a checkout of this branch.
388
Weaves are used if this branch's repository uses weaves.
391
return controldir.format_registry.make_controldir("git")
393
return controldir.format_registry.make_controldir("default")
395
def get_child_submit_format(self):
396
"""Return the preferred format of submissions to this branch."""
397
ret = self.get_config_stack().get("child_submit_format")
402
def get_config(self):
403
return GitBranchConfig(self)
405
def get_config_stack(self):
406
return GitBranchStack(self)
408
def _get_nick(self, local=False, possible_master_transports=None):
409
"""Find the nick name for this branch.
413
if getattr(self.repository, '_git', None):
414
cs = self.repository._git.get_config_stack()
416
return cs.get((b"branch", self.name.encode('utf-8')),
417
b"nick").decode("utf-8")
420
return self.name or u"HEAD"
422
def _set_nick(self, nick):
423
cf = self.repository._git.get_config()
424
cf.set((b"branch", self.name.encode('utf-8')),
425
b"nick", nick.encode("utf-8"))
428
self.repository._git._put_named_file('config', f.getvalue())
430
nick = property(_get_nick, _set_nick)
433
return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
436
def generate_revision_history(self, revid, last_rev=None,
438
if last_rev is not None:
439
graph = self.repository.get_graph()
440
if not graph.is_ancestor(last_rev, revid):
441
# our previous tip is not merged into stop_revision
442
raise errors.DivergedBranches(self, other_branch)
444
self.set_last_revision(revid)
446
def lock_write(self, token=None):
447
if token is not None:
448
raise errors.TokenLockingNotSupported(self)
450
if self._lock_mode == 'r':
451
raise errors.ReadOnlyError(self)
452
self._lock_count += 1
455
self._lock_mode = 'w'
457
self.repository.lock_write()
458
return lock.LogicalLockResult(self.unlock)
460
def leave_lock_in_place(self):
461
raise NotImplementedError(self.leave_lock_in_place)
463
def dont_leave_lock_in_place(self):
464
raise NotImplementedError(self.dont_leave_lock_in_place)
466
def get_stacked_on_url(self):
467
# Git doesn't do stacking (yet...)
468
raise branch.UnstackableBranchFormat(self._format, self.base)
470
def _get_push_origin(self, cs):
471
"""Get the name for the push origin.
473
The exact behaviour is documented in the git-config(1) manpage.
476
return cs.get((b'branch', self.name.encode('utf-8')), b'pushRemote')
479
return cs.get((b'branch', ), b'remote')
482
return cs.get((b'branch', self.name.encode('utf-8')), b'remote')
486
def _get_origin(self, cs):
488
return cs.get((b'branch', self.name.encode('utf-8')), b'remote')
492
def _get_related_push_branch(self, cs):
493
remote = self._get_push_origin(cs)
495
location = cs.get((b"remote", remote), b"url")
499
return git_url_to_bzr_url(location.decode('utf-8'), ref=self.ref)
501
def _get_related_merge_branch(self, cs):
502
remote = self._get_origin(cs)
504
location = cs.get((b"remote", remote), b"url")
509
ref = cs.get((b"branch", remote), b"merge")
513
return git_url_to_bzr_url(location.decode('utf-8'), ref=ref)
515
def _get_parent_location(self):
516
"""See Branch.get_parent()."""
517
cs = self.repository._git.get_config_stack()
518
return self._get_related_merge_branch(cs)
520
def _write_git_config(self, cs):
523
self.repository._git._put_named_file('config', f.getvalue())
525
def set_parent(self, location):
526
cs = self.repository._git.get_config()
527
remote = self._get_origin(cs)
528
this_url = urlutils.split_segment_parameters(self.user_url)[0]
529
target_url, branch, ref = bzr_url_to_git_url(location)
530
location = urlutils.relative_url(this_url, target_url)
531
cs.set((b"remote", remote), b"url", location)
533
cs.set((b"branch", remote), b"merge", branch_name_to_ref(branch))
535
cs.set((b"branch", remote), b"merge", ref)
537
# TODO(jelmer): Maybe unset rather than setting to HEAD?
538
cs.set((b"branch", remote), b"merge", b'HEAD')
539
self._write_git_config(cs)
541
def break_lock(self):
542
raise NotImplementedError(self.break_lock)
546
if self._lock_mode not in ('r', 'w'):
547
raise ValueError(self._lock_mode)
548
self._lock_count += 1
550
self._lock_mode = 'r'
552
self.repository.lock_read()
553
return lock.LogicalLockResult(self.unlock)
555
def peek_lock_mode(self):
556
return self._lock_mode
559
return (self._lock_mode is not None)
564
def _unlock_ref(self):
568
"""See Branch.unlock()."""
569
if self._lock_count == 0:
570
raise errors.LockNotHeld(self)
572
self._lock_count -= 1
573
if self._lock_count == 0:
574
if self._lock_mode == 'w':
576
self._lock_mode = None
577
self._clear_cached_state()
579
self.repository.unlock()
581
def get_physical_lock_status(self):
584
def last_revision(self):
585
with self.lock_read():
586
# perhaps should escape this ?
587
if self.head is None:
588
return revision.NULL_REVISION
589
return self.lookup_foreign_revision_id(self.head)
591
def _basic_push(self, target, overwrite=False, stop_revision=None):
592
return branch.InterBranch.get(self, target)._basic_push(
593
overwrite, stop_revision)
595
def lookup_foreign_revision_id(self, foreign_revid):
597
return self.repository.lookup_foreign_revision_id(foreign_revid,
601
return self.mapping.revision_id_foreign_to_bzr(foreign_revid)
603
def lookup_bzr_revision_id(self, revid):
604
return self.repository.lookup_bzr_revision_id(
605
revid, mapping=self.mapping)
607
def get_unshelver(self, tree):
608
raise errors.StoringUncommittedNotSupported(self)
610
def _clear_cached_state(self):
611
super(GitBranch, self)._clear_cached_state()
612
self._tag_refs = None
614
def _iter_tag_refs(self, refs):
615
"""Iterate over the tag refs.
617
:param refs: Refs dictionary (name -> git sha1)
618
:return: iterator over (ref_name, tag_name, peeled_sha1, unpeeled_sha1)
620
raise NotImplementedError(self._iter_tag_refs)
622
def get_tag_refs(self):
623
with self.lock_read():
624
if self._tag_refs is None:
625
self._tag_refs = list(self._iter_tag_refs())
626
return self._tag_refs
629
class LocalGitBranch(GitBranch):
630
"""A local Git branch."""
632
def __init__(self, controldir, repository, ref):
633
super(LocalGitBranch, self).__init__(controldir, repository, ref,
634
LocalGitBranchFormat())
636
def create_checkout(self, to_location, revision_id=None, lightweight=False,
637
accelerator_tree=None, hardlink=False):
638
t = transport.get_transport(to_location)
640
format = self._get_checkout_format(lightweight=lightweight)
641
checkout = format.initialize_on_transport(t)
643
from_branch = checkout.set_branch_reference(target_branch=self)
645
policy = checkout.determine_repository_policy()
646
policy.acquire_repository()
647
checkout_branch = checkout.create_branch()
648
checkout_branch.bind(self)
649
checkout_branch.pull(self, stop_revision=revision_id)
651
return checkout.create_workingtree(
652
revision_id, from_branch=from_branch, hardlink=hardlink)
655
self._ref_lock = self.repository._git.refs.lock_ref(self.ref)
657
def _unlock_ref(self):
658
self._ref_lock.unlock()
660
def break_lock(self):
661
self.repository._git.refs.unlock_ref(self.ref)
663
def fetch(self, from_branch, last_revision=None, limit=None):
664
return branch.InterBranch.get(from_branch, self).fetch(
665
stop_revision=last_revision, limit=limit)
667
def _gen_revision_history(self):
668
if self.head is None:
670
last_revid = self.last_revision()
671
graph = self.repository.get_graph()
673
ret = list(graph.iter_lefthand_ancestry(
674
last_revid, (revision.NULL_REVISION, )))
675
except errors.RevisionNotPresent as e:
676
raise errors.GhostRevisionsHaveNoRevno(last_revid, e.revision_id)
682
return self.repository._git.refs[self.ref]
686
def _read_last_revision_info(self):
687
last_revid = self.last_revision()
688
graph = self.repository.get_graph()
690
revno = graph.find_distance_to_null(
691
last_revid, [(revision.NULL_REVISION, 0)])
692
except errors.GhostRevisionsHaveNoRevno:
694
return revno, last_revid
696
def set_last_revision_info(self, revno, revision_id):
697
self.set_last_revision(revision_id)
698
self._last_revision_info_cache = revno, revision_id
700
def set_last_revision(self, revid):
701
if not revid or not isinstance(revid, bytes):
702
raise errors.InvalidRevisionId(revision_id=revid, branch=self)
703
if revid == NULL_REVISION:
706
(newhead, self.mapping) = self.repository.lookup_bzr_revision_id(
708
if self.mapping is None:
710
self._set_head(newhead)
712
def _set_head(self, value):
713
if value == ZERO_SHA:
714
raise ValueError(value)
717
del self.repository._git.refs[self.ref]
719
self.repository._git.refs[self.ref] = self._head
720
self._clear_cached_state()
722
head = property(_get_head, _set_head)
724
def get_push_location(self):
725
"""See Branch.get_push_location."""
726
push_loc = self.get_config_stack().get('push_location')
727
if push_loc is not None:
729
cs = self.repository._git.get_config_stack()
730
return self._get_related_push_branch(cs)
732
def set_push_location(self, location):
733
"""See Branch.set_push_location."""
734
self.get_config().set_user_option('push_location', location,
735
store=config.STORE_LOCATION)
737
def supports_tags(self):
740
def store_uncommitted(self, creator):
741
raise errors.StoringUncommittedNotSupported(self)
743
def _iter_tag_refs(self):
744
"""Iterate over the tag refs.
746
:param refs: Refs dictionary (name -> git sha1)
747
:return: iterator over (ref_name, tag_name, peeled_sha1, unpeeled_sha1)
749
refs = self.repository._git.refs
750
for ref_name, unpeeled in viewitems(refs.as_dict()):
752
tag_name = ref_to_tag_name(ref_name)
753
except (ValueError, UnicodeDecodeError):
755
peeled = refs.get_peeled(ref_name)
758
if not isinstance(tag_name, text_type):
759
raise TypeError(tag_name)
760
yield (ref_name, tag_name, peeled, unpeeled)
762
def create_memorytree(self):
763
from .memorytree import GitMemoryTree
764
return GitMemoryTree(self, self.repository._git.object_store,
767
def reference_parent(self, path, file_id=None, possible_transports=None):
768
"""Return the parent branch for a tree-reference file_id
770
:param path: The path of the file_id in the tree
771
:param file_id: Optional file_id of the tree reference
772
:return: A branch associated with the file_id
774
# FIXME should provide multiple branches, based on config
775
url = urlutils.join(self.user_url, path)
776
return branch.Branch.open(
778
possible_transports=possible_transports)
781
def _quick_lookup_revno(local_branch, remote_branch, revid):
782
if not isinstance(revid, bytes):
783
raise TypeError(revid)
784
# Try in source branch first, it'll be faster
785
with local_branch.lock_read():
787
return local_branch.revision_id_to_revno(revid)
788
except errors.NoSuchRevision:
789
graph = local_branch.repository.get_graph()
791
return graph.find_distance_to_null(
792
revid, [(revision.NULL_REVISION, 0)])
793
except errors.GhostRevisionsHaveNoRevno:
794
# FIXME: Check using graph.find_distance_to_null() ?
795
with remote_branch.lock_read():
796
return remote_branch.revision_id_to_revno(revid)
799
class GitBranchPullResult(branch.PullResult):
802
super(GitBranchPullResult, self).__init__()
803
self.new_git_head = None
804
self._old_revno = None
805
self._new_revno = None
807
def report(self, to_file):
809
if self.old_revid == self.new_revid:
810
to_file.write('No revisions to pull.\n')
811
elif self.new_git_head is not None:
812
to_file.write('Now on revision %d (git sha: %s).\n' %
813
(self.new_revno, self.new_git_head))
815
to_file.write('Now on revision %d.\n' % (self.new_revno,))
816
self._show_tag_conficts(to_file)
818
def _lookup_revno(self, revid):
819
return _quick_lookup_revno(self.target_branch, self.source_branch,
822
def _get_old_revno(self):
823
if self._old_revno is not None:
824
return self._old_revno
825
return self._lookup_revno(self.old_revid)
827
def _set_old_revno(self, revno):
828
self._old_revno = revno
830
old_revno = property(_get_old_revno, _set_old_revno)
832
def _get_new_revno(self):
833
if self._new_revno is not None:
834
return self._new_revno
835
return self._lookup_revno(self.new_revid)
837
def _set_new_revno(self, revno):
838
self._new_revno = revno
840
new_revno = property(_get_new_revno, _set_new_revno)
843
class GitBranchPushResult(branch.BranchPushResult):
845
def _lookup_revno(self, revid):
846
return _quick_lookup_revno(self.source_branch, self.target_branch,
851
return self._lookup_revno(self.old_revid)
855
new_original_revno = getattr(self, "new_original_revno", None)
856
if new_original_revno:
857
return new_original_revno
858
if getattr(self, "new_original_revid", None) is not None:
859
return self._lookup_revno(self.new_original_revid)
860
return self._lookup_revno(self.new_revid)
863
class InterFromGitBranch(branch.GenericInterBranch):
864
"""InterBranch implementation that pulls from Git into bzr."""
867
def _get_branch_formats_to_test():
869
default_format = branch.format_registry.get_default()
870
except AttributeError:
871
default_format = branch.BranchFormat._default_format
872
from .remote import RemoteGitBranchFormat
874
(RemoteGitBranchFormat(), default_format),
875
(LocalGitBranchFormat(), default_format)]
878
def _get_interrepo(self, source, target):
879
return _mod_repository.InterRepository.get(
880
source.repository, target.repository)
883
def is_compatible(cls, source, target):
884
if not isinstance(source, GitBranch):
886
if isinstance(target, GitBranch):
887
# InterLocalGitRemoteGitBranch or InterToGitBranch should be used
889
if (getattr(cls._get_interrepo(source, target), "fetch_objects", None)
891
# fetch_objects is necessary for this to work
895
def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
896
self.fetch_objects(stop_revision, fetch_tags=fetch_tags, limit=limit)
898
def fetch_objects(self, stop_revision, fetch_tags, limit=None):
899
interrepo = self._get_interrepo(self.source, self.target)
900
if fetch_tags is None:
901
c = self.source.get_config_stack()
902
fetch_tags = c.get('branch.fetch_tags')
904
def determine_wants(heads):
905
if stop_revision is None:
907
head = heads[self.source.ref]
909
self._last_revid = revision.NULL_REVISION
911
self._last_revid = self.source.lookup_foreign_revision_id(
914
self._last_revid = stop_revision
915
real = interrepo.get_determine_wants_revids(
916
[self._last_revid], include_tags=fetch_tags)
918
pack_hint, head, refs = interrepo.fetch_objects(
919
determine_wants, self.source.mapping, limit=limit)
920
if (pack_hint is not None and
921
self.target.repository._format.pack_compresses):
922
self.target.repository.pack(hint=pack_hint)
925
def _update_revisions(self, stop_revision=None, overwrite=False):
926
head, refs = self.fetch_objects(stop_revision, fetch_tags=None)
928
prev_last_revid = None
930
prev_last_revid = self.target.last_revision()
931
self.target.generate_revision_history(
932
self._last_revid, last_rev=prev_last_revid,
933
other_branch=self.source)
936
def _basic_pull(self, stop_revision, overwrite, run_hooks,
937
_override_hook_target, _hook_master):
938
if overwrite is True:
939
overwrite = set(["history", "tags"])
942
result = GitBranchPullResult()
943
result.source_branch = self.source
944
if _override_hook_target is None:
945
result.target_branch = self.target
947
result.target_branch = _override_hook_target
948
with self.target.lock_write(), self.source.lock_read():
949
# We assume that during 'pull' the target repository is closer than
951
(result.old_revno, result.old_revid) = \
952
self.target.last_revision_info()
953
result.new_git_head, remote_refs = self._update_revisions(
954
stop_revision, overwrite=("history" in overwrite))
955
tags_ret = self.source.tags.merge_to(
956
self.target.tags, ("tags" in overwrite), ignore_master=True)
957
if isinstance(tags_ret, tuple):
958
result.tag_updates, result.tag_conflicts = tags_ret
960
result.tag_conflicts = tags_ret
961
(result.new_revno, result.new_revid) = \
962
self.target.last_revision_info()
964
result.master_branch = _hook_master
965
result.local_branch = result.target_branch
967
result.master_branch = result.target_branch
968
result.local_branch = None
970
for hook in branch.Branch.hooks['post_pull']:
974
def pull(self, overwrite=False, stop_revision=None,
975
possible_transports=None, _hook_master=None, run_hooks=True,
976
_override_hook_target=None, local=False):
979
:param _hook_master: Private parameter - set the branch to
980
be supplied as the master to pull hooks.
981
:param run_hooks: Private parameter - if false, this branch
982
is being called because it's the master of the primary branch,
983
so it should not run its hooks.
984
:param _override_hook_target: Private parameter - set the branch to be
985
supplied as the target_branch to pull hooks.
987
# This type of branch can't be bound.
988
bound_location = self.target.get_bound_location()
989
if local and not bound_location:
990
raise errors.LocalRequiresBoundBranch()
992
source_is_master = False
993
self.source.lock_read()
995
# bound_location comes from a config file, some care has to be
996
# taken to relate it to source.user_url
997
normalized = urlutils.normalize_url(bound_location)
999
relpath = self.source.user_transport.relpath(normalized)
1000
source_is_master = (relpath == '')
1001
except (errors.PathNotChild, urlutils.InvalidURL):
1002
source_is_master = False
1003
if not local and bound_location and not source_is_master:
1004
# not pulling from master, so we need to update master.
1005
master_branch = self.target.get_master_branch(possible_transports)
1006
master_branch.lock_write()
1010
# pull from source into master.
1011
master_branch.pull(self.source, overwrite, stop_revision,
1013
result = self._basic_pull(stop_revision, overwrite, run_hooks,
1014
_override_hook_target,
1015
_hook_master=master_branch)
1017
self.source.unlock()
1020
master_branch.unlock()
1023
def _basic_push(self, overwrite, stop_revision):
1024
if overwrite is True:
1025
overwrite = set(["history", "tags"])
1028
result = branch.BranchPushResult()
1029
result.source_branch = self.source
1030
result.target_branch = self.target
1031
result.old_revno, result.old_revid = self.target.last_revision_info()
1032
result.new_git_head, remote_refs = self._update_revisions(
1033
stop_revision, overwrite=("history" in overwrite))
1034
tags_ret = self.source.tags.merge_to(
1035
self.target.tags, "tags" in overwrite, ignore_master=True)
1036
(result.tag_updates, result.tag_conflicts) = tags_ret
1037
result.new_revno, result.new_revid = self.target.last_revision_info()
1041
class InterGitBranch(branch.GenericInterBranch):
1042
"""InterBranch implementation that pulls between Git branches."""
1044
def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
1045
raise NotImplementedError(self.fetch)
1048
class InterLocalGitRemoteGitBranch(InterGitBranch):
1049
"""InterBranch that copies from a local to a remote git branch."""
1052
def _get_branch_formats_to_test():
1053
from .remote import RemoteGitBranchFormat
1055
(LocalGitBranchFormat(), RemoteGitBranchFormat())]
1058
def is_compatible(self, source, target):
1059
from .remote import RemoteGitBranch
1060
return (isinstance(source, LocalGitBranch) and
1061
isinstance(target, RemoteGitBranch))
1063
def _basic_push(self, overwrite, stop_revision):
1064
result = GitBranchPushResult()
1065
result.source_branch = self.source
1066
result.target_branch = self.target
1067
if stop_revision is None:
1068
stop_revision = self.source.last_revision()
1070
def get_changed_refs(old_refs):
1071
old_ref = old_refs.get(self.target.ref, None)
1073
result.old_revid = revision.NULL_REVISION
1075
result.old_revid = self.target.lookup_foreign_revision_id(
1077
new_ref = self.source.repository.lookup_bzr_revision_id(
1080
if remote_divergence(
1082
self.source.repository._git.object_store):
1083
raise errors.DivergedBranches(self.source, self.target)
1084
refs = {self.target.ref: new_ref}
1085
result.new_revid = stop_revision
1086
for name, sha in viewitems(
1087
self.source.repository._git.refs.as_dict(b"refs/tags")):
1088
refs[tag_name_to_ref(name)] = sha
1090
self.target.repository.send_pack(
1092
self.source.repository._git.object_store.generate_pack_data)
1096
class InterGitLocalGitBranch(InterGitBranch):
1097
"""InterBranch that copies from a remote to a local git branch."""
1100
def _get_branch_formats_to_test():
1101
from .remote import RemoteGitBranchFormat
1103
(RemoteGitBranchFormat(), LocalGitBranchFormat()),
1104
(LocalGitBranchFormat(), LocalGitBranchFormat())]
1107
def is_compatible(self, source, target):
1108
return (isinstance(source, GitBranch) and
1109
isinstance(target, LocalGitBranch))
1111
def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
1112
interrepo = _mod_repository.InterRepository.get(self.source.repository,
1113
self.target.repository)
1114
if stop_revision is None:
1115
stop_revision = self.source.last_revision()
1116
determine_wants = interrepo.get_determine_wants_revids(
1117
[stop_revision], include_tags=fetch_tags)
1118
interrepo.fetch_objects(determine_wants, limit=limit)
1120
def _basic_push(self, overwrite=False, stop_revision=None):
1121
if overwrite is True:
1122
overwrite = set(["history", "tags"])
1125
result = GitBranchPushResult()
1126
result.source_branch = self.source
1127
result.target_branch = self.target
1128
result.old_revid = self.target.last_revision()
1129
refs, stop_revision = self.update_refs(stop_revision)
1130
self.target.generate_revision_history(
1132
(result.old_revid if ("history" not in overwrite) else None),
1133
other_branch=self.source)
1134
tags_ret = self.source.tags.merge_to(
1136
source_tag_refs=remote_refs_dict_to_tag_refs(refs),
1137
overwrite=("tags" in overwrite))
1138
if isinstance(tags_ret, tuple):
1139
(result.tag_updates, result.tag_conflicts) = tags_ret
1141
result.tag_conflicts = tags_ret
1142
result.new_revid = self.target.last_revision()
1145
def update_refs(self, stop_revision=None):
1146
interrepo = _mod_repository.InterRepository.get(
1147
self.source.repository, self.target.repository)
1148
c = self.source.get_config_stack()
1149
fetch_tags = c.get('branch.fetch_tags')
1151
if stop_revision is None:
1152
refs = interrepo.fetch(branches=[self.source.ref], include_tags=fetch_tags)
1154
head = refs[self.source.ref]
1156
stop_revision = revision.NULL_REVISION
1158
stop_revision = self.target.lookup_foreign_revision_id(head)
1160
refs = interrepo.fetch(
1161
revision_id=stop_revision, include_tags=fetch_tags)
1162
return refs, stop_revision
1164
def pull(self, stop_revision=None, overwrite=False,
1165
possible_transports=None, run_hooks=True, local=False):
1166
# This type of branch can't be bound.
1168
raise errors.LocalRequiresBoundBranch()
1169
if overwrite is True:
1170
overwrite = set(["history", "tags"])
1174
result = GitPullResult()
1175
result.source_branch = self.source
1176
result.target_branch = self.target
1177
with self.target.lock_write(), self.source.lock_read():
1178
result.old_revid = self.target.last_revision()
1179
refs, stop_revision = self.update_refs(stop_revision)
1180
self.target.generate_revision_history(
1182
(result.old_revid if ("history" not in overwrite) else None),
1183
other_branch=self.source)
1184
tags_ret = self.source.tags.merge_to(
1185
self.target.tags, overwrite=("tags" in overwrite),
1186
source_tag_refs=remote_refs_dict_to_tag_refs(refs))
1187
if isinstance(tags_ret, tuple):
1188
(result.tag_updates, result.tag_conflicts) = tags_ret
1190
result.tag_conflicts = tags_ret
1191
result.new_revid = self.target.last_revision()
1192
result.local_branch = None
1193
result.master_branch = result.target_branch
1195
for hook in branch.Branch.hooks['post_pull']:
1200
class InterToGitBranch(branch.GenericInterBranch):
1201
"""InterBranch implementation that pulls into a Git branch."""
1203
def __init__(self, source, target):
1204
super(InterToGitBranch, self).__init__(source, target)
1205
self.interrepo = _mod_repository.InterRepository.get(source.repository,
1209
def _get_branch_formats_to_test():
1211
default_format = branch.format_registry.get_default()
1212
except AttributeError:
1213
default_format = branch.BranchFormat._default_format
1214
from .remote import RemoteGitBranchFormat
1216
(default_format, LocalGitBranchFormat()),
1217
(default_format, RemoteGitBranchFormat())]
1220
def is_compatible(self, source, target):
1221
return (not isinstance(source, GitBranch) and
1222
isinstance(target, GitBranch))
1224
def _get_new_refs(self, stop_revision=None, fetch_tags=None):
1225
if not self.source.is_locked():
1226
raise errors.ObjectNotLocked(self.source)
1227
if stop_revision is None:
1228
(stop_revno, stop_revision) = self.source.last_revision_info()
1230
stop_revno = self.source.revision_id_to_revno(stop_revision)
1231
if not isinstance(stop_revision, bytes):
1232
raise TypeError(stop_revision)
1233
main_ref = self.target.ref
1234
refs = {main_ref: (None, stop_revision)}
1235
if fetch_tags is None:
1236
c = self.source.get_config_stack()
1237
fetch_tags = c.get('branch.fetch_tags')
1238
for name, revid in viewitems(self.source.tags.get_tag_dict()):
1239
if self.source.repository.has_revision(revid):
1240
ref = tag_name_to_ref(name)
1241
if not check_ref_format(ref):
1242
warning("skipping tag with invalid characters %s (%s)",
1246
# FIXME: Skip tags that are not in the ancestry
1247
refs[ref] = (None, revid)
1248
return refs, main_ref, (stop_revno, stop_revision)
1250
def _update_refs(self, result, old_refs, new_refs, overwrite):
1251
mutter("updating refs. old refs: %r, new refs: %r",
1253
result.tag_updates = {}
1254
result.tag_conflicts = []
1255
ret = dict(old_refs)
1257
def ref_equals(refs, ref, git_sha, revid):
1262
if (value[0] is not None and
1263
git_sha is not None and
1264
value[0] == git_sha):
1266
if (value[1] is not None and
1267
revid is not None and
1270
# FIXME: If one side only has the git sha available and the other
1271
# only has the bzr revid, then this will cause us to show a tag as
1272
# updated that hasn't actually been updated.
1274
# FIXME: Check for diverged branches
1275
for ref, (git_sha, revid) in viewitems(new_refs):
1276
if ref_equals(ret, ref, git_sha, revid):
1277
# Already up to date
1279
git_sha = old_refs[ref][0]
1281
revid = old_refs[ref][1]
1282
ret[ref] = new_refs[ref] = (git_sha, revid)
1283
elif ref not in ret or overwrite:
1285
tag_name = ref_to_tag_name(ref)
1289
result.tag_updates[tag_name] = revid
1290
ret[ref] = (git_sha, revid)
1292
# FIXME: Check diverged
1296
name = ref_to_tag_name(ref)
1300
result.tag_conflicts.append(
1301
(name, revid, ret[name][1]))
1303
ret[ref] = (git_sha, revid)
1306
def fetch(self, stop_revision=None, fetch_tags=None, lossy=False,
1308
if stop_revision is None:
1309
stop_revision = self.source.last_revision()
1312
for k, v in viewitems(self.source.tags.get_tag_dict()):
1313
ret.append((None, v))
1314
ret.append((None, stop_revision))
1316
self.interrepo.fetch_objects(ret, lossy=lossy, limit=limit)
1317
except NoPushSupport:
1318
raise errors.NoRoundtrippingSupport(self.source, self.target)
1320
def pull(self, overwrite=False, stop_revision=None, local=False,
1321
possible_transports=None, run_hooks=True):
1322
result = GitBranchPullResult()
1323
result.source_branch = self.source
1324
result.target_branch = self.target
1325
with self.source.lock_read(), self.target.lock_write():
1326
new_refs, main_ref, stop_revinfo = self._get_new_refs(
1329
def update_refs(old_refs):
1330
return self._update_refs(result, old_refs, new_refs, overwrite)
1332
result.revidmap, old_refs, new_refs = (
1333
self.interrepo.fetch_refs(update_refs, lossy=False))
1334
except NoPushSupport:
1335
raise errors.NoRoundtrippingSupport(self.source, self.target)
1336
(old_sha1, result.old_revid) = old_refs.get(
1337
main_ref, (ZERO_SHA, NULL_REVISION))
1338
if result.old_revid is None:
1339
result.old_revid = self.target.lookup_foreign_revision_id(
1341
result.new_revid = new_refs[main_ref][1]
1342
result.local_branch = None
1343
result.master_branch = self.target
1345
for hook in branch.Branch.hooks['post_pull']:
1349
def push(self, overwrite=False, stop_revision=None, lossy=False,
1350
_override_hook_source_branch=None):
1351
result = GitBranchPushResult()
1352
result.source_branch = self.source
1353
result.target_branch = self.target
1354
result.local_branch = None
1355
result.master_branch = result.target_branch
1356
with self.source.lock_read(), self.target.lock_write():
1357
new_refs, main_ref, stop_revinfo = self._get_new_refs(
1360
def update_refs(old_refs):
1361
return self._update_refs(result, old_refs, new_refs, overwrite)
1363
result.revidmap, old_refs, new_refs = (
1364
self.interrepo.fetch_refs(
1365
update_refs, lossy=lossy, overwrite=overwrite))
1366
except NoPushSupport:
1367
raise errors.NoRoundtrippingSupport(self.source, self.target)
1368
(old_sha1, result.old_revid) = old_refs.get(
1369
main_ref, (ZERO_SHA, NULL_REVISION))
1370
if result.old_revid is None:
1371
result.old_revid = self.target.lookup_foreign_revision_id(
1373
result.new_revid = new_refs[main_ref][1]
1374
(result.new_original_revno,
1375
result.new_original_revid) = stop_revinfo
1376
for hook in branch.Branch.hooks['post_push']:
1381
branch.InterBranch.register_optimiser(InterGitLocalGitBranch)
1382
branch.InterBranch.register_optimiser(InterFromGitBranch)
1383
branch.InterBranch.register_optimiser(InterToGitBranch)
1384
branch.InterBranch.register_optimiser(InterLocalGitRemoteGitBranch)