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
38
repository as _mod_repository,
45
from ..foreign import ForeignBranch
46
from ..revision import (
49
from ..sixish import (
74
remote_refs_dict_to_tag_refs,
77
from .unpeel_map import (
86
def _calculate_revnos(branch):
87
if branch._format.stores_revno():
89
config = branch.get_config_stack()
90
return config.get('calculate_revnos')
93
class GitPullResult(branch.PullResult):
94
"""Result of a pull from a Git branch."""
96
def _lookup_revno(self, revid):
97
if not isinstance(revid, bytes):
98
raise TypeError(revid)
99
if not _calculate_revnos(self.target_branch):
101
# Try in source branch first, it'll be faster
102
with self.target_branch.lock_read():
103
return self.target_branch.revision_id_to_revno(revid)
107
return self._lookup_revno(self.old_revid)
111
return self._lookup_revno(self.new_revid)
114
class GitTags(tag.BasicTags):
115
"""Ref-based tag dictionary."""
117
def __init__(self, branch):
119
self.repository = branch.repository
121
def _merge_to_remote_git(self, target_repo, source_tag_refs,
126
def get_changed_refs(old_refs):
128
for ref_name, tag_name, peeled, unpeeled in (
129
source_tag_refs.iteritems()):
130
if old_refs.get(ref_name) == unpeeled:
132
elif overwrite or ref_name not in old_refs:
133
ret[ref_name] = unpeeled
134
updates[tag_name] = target_repo.lookup_foreign_revision_id(
139
self.repository.lookup_foreign_revision_id(peeled),
140
target_repo.lookup_foreign_revision_id(
141
old_refs[ref_name])))
143
target_repo.controldir.send_pack(
144
get_changed_refs, lambda have, want: [])
145
return updates, conflicts
147
def _merge_to_local_git(self, target_repo, source_tag_refs,
151
for ref_name, tag_name, peeled, unpeeled in source_tag_refs:
152
if target_repo._git.refs.get(ref_name) == unpeeled:
154
elif overwrite or ref_name not in target_repo._git.refs:
156
updates[tag_name] = (
157
target_repo.lookup_foreign_revision_id(peeled))
159
trace.warning('%s does not point to a valid object',
162
except NotCommitError:
163
trace.warning('%s points to a non-commit object',
166
target_repo._git.refs[ref_name] = unpeeled or peeled
169
source_revid = self.repository.lookup_foreign_revision_id(
171
target_revid = target_repo.lookup_foreign_revision_id(
172
target_repo._git.refs[ref_name])
174
trace.warning('%s does not point to a valid object',
177
except NotCommitError:
178
trace.warning('%s points to a non-commit object',
181
conflicts.append((tag_name, source_revid, target_revid))
182
return updates, conflicts
184
def _merge_to_git(self, to_tags, source_tag_refs, overwrite=False):
185
target_repo = to_tags.repository
186
if self.repository.has_same_location(target_repo):
189
if getattr(target_repo, "_git", None):
190
return self._merge_to_local_git(
191
target_repo, source_tag_refs, overwrite)
193
return self._merge_to_remote_git(
194
target_repo, source_tag_refs, overwrite)
196
to_tags.branch._tag_refs = None
198
def _merge_to_non_git(self, to_tags, source_tag_refs, overwrite=False):
199
unpeeled_map = defaultdict(set)
202
result = dict(to_tags.get_tag_dict())
203
for ref_name, tag_name, peeled, unpeeled in source_tag_refs:
204
if unpeeled is not None:
205
unpeeled_map[peeled].add(unpeeled)
207
bzr_revid = self.branch.lookup_foreign_revision_id(peeled)
208
except NotCommitError:
210
if result.get(tag_name) == bzr_revid:
212
elif tag_name not in result or overwrite:
213
result[tag_name] = bzr_revid
214
updates[tag_name] = bzr_revid
216
conflicts.append((tag_name, bzr_revid, result[tag_name]))
217
to_tags._set_tag_dict(result)
218
if len(unpeeled_map) > 0:
219
map_file = UnpeelMap.from_repository(to_tags.branch.repository)
220
map_file.update(unpeeled_map)
221
map_file.save_in_repository(to_tags.branch.repository)
222
return updates, conflicts
224
def merge_to(self, to_tags, overwrite=False, ignore_master=False,
225
source_tag_refs=None):
226
"""See Tags.merge_to."""
227
if source_tag_refs is None:
228
source_tag_refs = self.branch.get_tag_refs()
231
if isinstance(to_tags, GitTags):
232
return self._merge_to_git(to_tags, source_tag_refs,
238
master = to_tags.branch.get_master_branch()
239
with cleanup.ExitStack() as es:
240
if master is not None:
241
es.enter_context(master.lock_write())
242
updates, conflicts = self._merge_to_non_git(
243
to_tags, source_tag_refs, overwrite=overwrite)
244
if master is not None:
245
extra_updates, extra_conflicts = self.merge_to(
246
master.tags, overwrite=overwrite,
247
source_tag_refs=source_tag_refs,
248
ignore_master=ignore_master)
249
updates.update(extra_updates)
250
conflicts += extra_conflicts
251
return updates, conflicts
253
def get_tag_dict(self):
255
for (ref_name, tag_name, peeled, unpeeled) in (
256
self.branch.get_tag_refs()):
258
bzr_revid = self.branch.lookup_foreign_revision_id(peeled)
259
except NotCommitError:
262
ret[tag_name] = bzr_revid
266
class LocalGitTagDict(GitTags):
267
"""Dictionary with tags in a local repository."""
269
def __init__(self, branch):
270
super(LocalGitTagDict, self).__init__(branch)
271
self.refs = self.repository.controldir._git.refs
273
def _set_tag_dict(self, to_dict):
274
extra = set(self.refs.allkeys())
275
for k, revid in viewitems(to_dict):
276
name = tag_name_to_ref(k)
279
self.set_tag(k, revid)
282
del self.repository._git[name]
284
def set_tag(self, name, revid):
286
git_sha, mapping = self.branch.lookup_bzr_revision_id(revid)
287
except errors.NoSuchRevision:
288
raise errors.GhostTagsNotSupported(self)
289
self.refs[tag_name_to_ref(name)] = git_sha
290
self.branch._tag_refs = None
292
def delete_tag(self, name):
293
ref = tag_name_to_ref(name)
294
if ref not in self.refs:
295
raise errors.NoSuchTag(name)
297
self.branch._tag_refs = None
300
class GitBranchFormat(branch.BranchFormat):
302
def network_name(self):
305
def supports_tags(self):
308
def supports_leaving_lock(self):
311
def supports_tags_referencing_ghosts(self):
314
def tags_are_versioned(self):
317
def get_foreign_tests_branch_factory(self):
318
from .tests.test_branch import ForeignTestsBranchFactory
319
return ForeignTestsBranchFactory()
321
def make_tags(self, branch):
324
except AttributeError:
326
if getattr(branch.repository, "_git", None) is None:
327
from .remote import RemoteGitTagDict
328
return RemoteGitTagDict(branch)
330
return LocalGitTagDict(branch)
332
def initialize(self, a_controldir, name=None, repository=None,
333
append_revisions_only=None):
334
raise NotImplementedError(self.initialize)
336
def get_reference(self, controldir, name=None):
337
return controldir.get_branch_reference(name=name)
339
def set_reference(self, controldir, name, target):
340
return controldir.set_branch_reference(target, name)
342
def stores_revno(self):
343
"""True if this branch format store revision numbers."""
347
class LocalGitBranchFormat(GitBranchFormat):
349
def get_format_description(self):
350
return 'Local Git Branch'
353
def _matchingcontroldir(self):
354
from .dir import LocalGitControlDirFormat
355
return LocalGitControlDirFormat()
357
def initialize(self, a_controldir, name=None, repository=None,
358
append_revisions_only=None):
359
from .dir import LocalGitDir
360
if not isinstance(a_controldir, LocalGitDir):
361
raise errors.IncompatibleFormat(self, a_controldir._format)
362
return a_controldir.create_branch(
363
repository=repository, name=name,
364
append_revisions_only=append_revisions_only)
367
class GitBranch(ForeignBranch):
368
"""An adapter to git repositories for bzr Branch objects."""
371
def control_transport(self):
372
return self._control_transport
375
def user_transport(self):
376
return self._user_transport
378
def __init__(self, controldir, repository, ref, format):
379
self.repository = repository
380
self._format = format
381
self.controldir = controldir
382
self._lock_mode = None
384
super(GitBranch, self).__init__(repository.get_mapping())
387
self._user_transport = controldir.user_transport.clone('.')
388
self._control_transport = controldir.control_transport.clone('.')
389
self._tag_refs = None
392
self.name = ref_to_branch_name(ref)
395
if self.ref is not None:
396
params = {"ref": urlutils.escape(self.ref)}
399
params = {"branch": urlutils.escape(self.name)}
400
for k, v in params.items():
401
self._user_transport.set_segment_parameter(k, v)
402
self._control_transport.set_segment_parameter(k, v)
403
self.base = controldir.user_transport.base
405
def _get_checkout_format(self, lightweight=False):
406
"""Return the most suitable metadir for a checkout of this branch.
407
Weaves are used if this branch's repository uses weaves.
410
return controldir.format_registry.make_controldir("git")
412
return controldir.format_registry.make_controldir("default")
414
def get_child_submit_format(self):
415
"""Return the preferred format of submissions to this branch."""
416
ret = self.get_config_stack().get("child_submit_format")
421
def get_config(self):
422
return GitBranchConfig(self)
424
def get_config_stack(self):
425
return GitBranchStack(self)
427
def _get_nick(self, local=False, possible_master_transports=None):
428
"""Find the nick name for this branch.
432
if getattr(self.repository, '_git', None):
433
cs = self.repository._git.get_config_stack()
435
return cs.get((b"branch", self.name.encode('utf-8')),
436
b"nick").decode("utf-8")
439
return self.name or u"HEAD"
441
def _set_nick(self, nick):
442
cf = self.repository._git.get_config()
443
cf.set((b"branch", self.name.encode('utf-8')),
444
b"nick", nick.encode("utf-8"))
447
self.repository._git._put_named_file('config', f.getvalue())
449
nick = property(_get_nick, _set_nick)
452
return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
455
def generate_revision_history(self, revid, last_rev=None,
457
if last_rev is not None:
458
graph = self.repository.get_graph()
459
if not graph.is_ancestor(last_rev, revid):
460
# our previous tip is not merged into stop_revision
461
raise errors.DivergedBranches(self, other_branch)
463
self.set_last_revision(revid)
465
def lock_write(self, token=None):
466
if token is not None:
467
raise errors.TokenLockingNotSupported(self)
469
if self._lock_mode == 'r':
470
raise errors.ReadOnlyError(self)
471
self._lock_count += 1
474
self._lock_mode = 'w'
476
self.repository.lock_write()
477
return lock.LogicalLockResult(self.unlock)
479
def leave_lock_in_place(self):
480
raise NotImplementedError(self.leave_lock_in_place)
482
def dont_leave_lock_in_place(self):
483
raise NotImplementedError(self.dont_leave_lock_in_place)
485
def get_stacked_on_url(self):
486
# Git doesn't do stacking (yet...)
487
raise branch.UnstackableBranchFormat(self._format, self.base)
489
def _get_push_origin(self, cs):
490
"""Get the name for the push origin.
492
The exact behaviour is documented in the git-config(1) manpage.
495
return cs.get((b'branch', self.name.encode('utf-8')), b'pushRemote')
498
return cs.get((b'branch', ), b'remote')
501
return cs.get((b'branch', self.name.encode('utf-8')), b'remote')
505
def _get_origin(self, cs):
507
return cs.get((b'branch', self.name.encode('utf-8')), b'remote')
511
def _get_related_push_branch(self, cs):
512
remote = self._get_push_origin(cs)
514
location = cs.get((b"remote", remote), b"url")
518
return git_url_to_bzr_url(location.decode('utf-8'), ref=self.ref)
520
def _get_related_merge_branch(self, cs):
521
remote = self._get_origin(cs)
523
location = cs.get((b"remote", remote), b"url")
528
ref = cs.get((b"branch", remote), b"merge")
532
return git_url_to_bzr_url(location.decode('utf-8'), ref=ref)
534
def _get_parent_location(self):
535
"""See Branch.get_parent()."""
536
cs = self.repository._git.get_config_stack()
537
return self._get_related_merge_branch(cs)
539
def _write_git_config(self, cs):
542
self.repository._git._put_named_file('config', f.getvalue())
544
def set_parent(self, location):
545
cs = self.repository._git.get_config()
546
remote = self._get_origin(cs)
547
this_url = urlutils.strip_segment_parameters(self.user_url)
548
target_url, branch, ref = bzr_url_to_git_url(location)
549
location = urlutils.relative_url(this_url, target_url)
550
cs.set((b"remote", remote), b"url", location)
552
cs.set((b"branch", remote), b"merge", branch_name_to_ref(branch))
554
cs.set((b"branch", remote), b"merge", ref)
556
# TODO(jelmer): Maybe unset rather than setting to HEAD?
557
cs.set((b"branch", remote), b"merge", b'HEAD')
558
self._write_git_config(cs)
560
def break_lock(self):
561
raise NotImplementedError(self.break_lock)
565
if self._lock_mode not in ('r', 'w'):
566
raise ValueError(self._lock_mode)
567
self._lock_count += 1
569
self._lock_mode = 'r'
571
self.repository.lock_read()
572
return lock.LogicalLockResult(self.unlock)
574
def peek_lock_mode(self):
575
return self._lock_mode
578
return (self._lock_mode is not None)
583
def _unlock_ref(self):
587
"""See Branch.unlock()."""
588
if self._lock_count == 0:
589
raise errors.LockNotHeld(self)
591
self._lock_count -= 1
592
if self._lock_count == 0:
593
if self._lock_mode == 'w':
595
self._lock_mode = None
596
self._clear_cached_state()
598
self.repository.unlock()
600
def get_physical_lock_status(self):
603
def last_revision(self):
604
with self.lock_read():
605
# perhaps should escape this ?
606
if self.head is None:
607
return revision.NULL_REVISION
608
return self.lookup_foreign_revision_id(self.head)
610
def _basic_push(self, target, overwrite=False, stop_revision=None):
611
return branch.InterBranch.get(self, target)._basic_push(
612
overwrite, stop_revision)
614
def lookup_foreign_revision_id(self, foreign_revid):
616
return self.repository.lookup_foreign_revision_id(foreign_revid,
620
return self.mapping.revision_id_foreign_to_bzr(foreign_revid)
622
def lookup_bzr_revision_id(self, revid):
623
return self.repository.lookup_bzr_revision_id(
624
revid, mapping=self.mapping)
626
def get_unshelver(self, tree):
627
raise errors.StoringUncommittedNotSupported(self)
629
def _clear_cached_state(self):
630
super(GitBranch, self)._clear_cached_state()
631
self._tag_refs = None
633
def _iter_tag_refs(self, refs):
634
"""Iterate over the tag refs.
636
:param refs: Refs dictionary (name -> git sha1)
637
:return: iterator over (ref_name, tag_name, peeled_sha1, unpeeled_sha1)
639
raise NotImplementedError(self._iter_tag_refs)
641
def get_tag_refs(self):
642
with self.lock_read():
643
if self._tag_refs is None:
644
self._tag_refs = list(self._iter_tag_refs())
645
return self._tag_refs
647
def import_last_revision_info_and_tags(self, source, revno, revid,
649
"""Set the last revision info, importing from another repo if necessary.
651
This is used by the bound branch code to upload a revision to
652
the master branch first before updating the tip of the local branch.
653
Revisions referenced by source's tags are also transferred.
655
:param source: Source branch to optionally fetch from
656
:param revno: Revision number of the new tip
657
:param revid: Revision id of the new tip
658
:param lossy: Whether to discard metadata that can not be
660
:return: Tuple with the new revision number and revision id
661
(should only be different from the arguments when lossy=True)
663
push_result = source.push(
664
self, stop_revision=revid, lossy=lossy, _stop_revno=revno)
665
return (push_result.new_revno, push_result.new_revid)
667
def reconcile(self, thorough=True):
668
"""Make sure the data stored in this branch is consistent."""
669
from ..reconcile import ReconcileResult
671
return ReconcileResult()
674
class LocalGitBranch(GitBranch):
675
"""A local Git branch."""
677
def __init__(self, controldir, repository, ref):
678
super(LocalGitBranch, self).__init__(controldir, repository, ref,
679
LocalGitBranchFormat())
681
def create_checkout(self, to_location, revision_id=None, lightweight=False,
682
accelerator_tree=None, hardlink=False):
683
t = transport.get_transport(to_location)
685
format = self._get_checkout_format(lightweight=lightweight)
686
checkout = format.initialize_on_transport(t)
688
from_branch = checkout.set_branch_reference(target_branch=self)
690
policy = checkout.determine_repository_policy()
691
policy.acquire_repository()
692
checkout_branch = checkout.create_branch()
693
checkout_branch.bind(self)
694
checkout_branch.pull(self, stop_revision=revision_id)
696
return checkout.create_workingtree(
697
revision_id, from_branch=from_branch, hardlink=hardlink)
700
self._ref_lock = self.repository._git.refs.lock_ref(self.ref)
702
def _unlock_ref(self):
703
self._ref_lock.unlock()
705
def break_lock(self):
706
self.repository._git.refs.unlock_ref(self.ref)
708
def fetch(self, from_branch, last_revision=None, limit=None):
709
return branch.InterBranch.get(from_branch, self).fetch(
710
stop_revision=last_revision, limit=limit)
712
def _gen_revision_history(self):
713
if self.head is None:
715
last_revid = self.last_revision()
716
graph = self.repository.get_graph()
718
ret = list(graph.iter_lefthand_ancestry(
719
last_revid, (revision.NULL_REVISION, )))
720
except errors.RevisionNotPresent as e:
721
raise errors.GhostRevisionsHaveNoRevno(last_revid, e.revision_id)
727
return self.repository._git.refs[self.ref]
731
def _read_last_revision_info(self):
732
last_revid = self.last_revision()
733
graph = self.repository.get_graph()
735
revno = graph.find_distance_to_null(
736
last_revid, [(revision.NULL_REVISION, 0)])
737
except errors.GhostRevisionsHaveNoRevno:
739
return revno, last_revid
741
def set_last_revision_info(self, revno, revision_id):
742
self.set_last_revision(revision_id)
743
self._last_revision_info_cache = revno, revision_id
745
def set_last_revision(self, revid):
746
if not revid or not isinstance(revid, bytes):
747
raise errors.InvalidRevisionId(revision_id=revid, branch=self)
748
if revid == NULL_REVISION:
751
(newhead, self.mapping) = self.repository.lookup_bzr_revision_id(
753
if self.mapping is None:
755
self._set_head(newhead)
757
def _set_head(self, value):
758
if value == ZERO_SHA:
759
raise ValueError(value)
762
del self.repository._git.refs[self.ref]
764
self.repository._git.refs[self.ref] = self._head
765
self._clear_cached_state()
767
head = property(_get_head, _set_head)
769
def get_push_location(self):
770
"""See Branch.get_push_location."""
771
push_loc = self.get_config_stack().get('push_location')
772
if push_loc is not None:
774
cs = self.repository._git.get_config_stack()
775
return self._get_related_push_branch(cs)
777
def set_push_location(self, location):
778
"""See Branch.set_push_location."""
779
self.get_config().set_user_option('push_location', location,
780
store=config.STORE_LOCATION)
782
def supports_tags(self):
785
def store_uncommitted(self, creator):
786
raise errors.StoringUncommittedNotSupported(self)
788
def _iter_tag_refs(self):
789
"""Iterate over the tag refs.
791
:param refs: Refs dictionary (name -> git sha1)
792
:return: iterator over (ref_name, tag_name, peeled_sha1, unpeeled_sha1)
794
refs = self.repository.controldir.get_refs_container()
795
for ref_name, unpeeled in viewitems(refs.as_dict()):
797
tag_name = ref_to_tag_name(ref_name)
798
except (ValueError, UnicodeDecodeError):
800
peeled = refs.get_peeled(ref_name)
803
if not isinstance(tag_name, text_type):
804
raise TypeError(tag_name)
805
yield (ref_name, tag_name, peeled, unpeeled)
807
def create_memorytree(self):
808
from .memorytree import GitMemoryTree
809
return GitMemoryTree(self, self.repository._git.object_store,
812
def reference_parent(self, path, possible_transports=None):
813
"""Return the parent branch for a tree-reference.
815
:param path: The path of the nested tree in the tree
816
:return: A branch associated with the nested tree
818
# FIXME should provide multiple branches, based on config
819
url = urlutils.join(self.user_url, path)
820
return branch.Branch.open(
822
possible_transports=possible_transports)
825
def _quick_lookup_revno(local_branch, remote_branch, revid):
826
if not isinstance(revid, bytes):
827
raise TypeError(revid)
828
# Try in source branch first, it'll be faster
829
with local_branch.lock_read():
830
if not _calculate_revnos(local_branch):
833
return local_branch.revision_id_to_revno(revid)
834
except errors.NoSuchRevision:
835
graph = local_branch.repository.get_graph()
837
return graph.find_distance_to_null(
838
revid, [(revision.NULL_REVISION, 0)])
839
except errors.GhostRevisionsHaveNoRevno:
840
if not _calculate_revnos(remote_branch):
842
# FIXME: Check using graph.find_distance_to_null() ?
843
with remote_branch.lock_read():
844
return remote_branch.revision_id_to_revno(revid)
847
class GitBranchPullResult(branch.PullResult):
850
super(GitBranchPullResult, self).__init__()
851
self.new_git_head = None
852
self._old_revno = None
853
self._new_revno = None
855
def report(self, to_file):
857
if self.old_revid == self.new_revid:
858
to_file.write('No revisions to pull.\n')
859
elif self.new_git_head is not None:
860
to_file.write('Now on revision %d (git sha: %s).\n' %
861
(self.new_revno, self.new_git_head))
863
to_file.write('Now on revision %d.\n' % (self.new_revno,))
864
self._show_tag_conficts(to_file)
866
def _lookup_revno(self, revid):
867
return _quick_lookup_revno(self.target_branch, self.source_branch,
870
def _get_old_revno(self):
871
if self._old_revno is not None:
872
return self._old_revno
873
return self._lookup_revno(self.old_revid)
875
def _set_old_revno(self, revno):
876
self._old_revno = revno
878
old_revno = property(_get_old_revno, _set_old_revno)
880
def _get_new_revno(self):
881
if self._new_revno is not None:
882
return self._new_revno
883
return self._lookup_revno(self.new_revid)
885
def _set_new_revno(self, revno):
886
self._new_revno = revno
888
new_revno = property(_get_new_revno, _set_new_revno)
891
class GitBranchPushResult(branch.BranchPushResult):
893
def _lookup_revno(self, revid):
894
return _quick_lookup_revno(self.source_branch, self.target_branch,
899
return self._lookup_revno(self.old_revid)
903
new_original_revno = getattr(self, "new_original_revno", None)
904
if new_original_revno:
905
return new_original_revno
906
if getattr(self, "new_original_revid", None) is not None:
907
return self._lookup_revno(self.new_original_revid)
908
return self._lookup_revno(self.new_revid)
911
class InterFromGitBranch(branch.GenericInterBranch):
912
"""InterBranch implementation that pulls from Git into bzr."""
915
def _get_branch_formats_to_test():
917
default_format = branch.format_registry.get_default()
918
except AttributeError:
919
default_format = branch.BranchFormat._default_format
920
from .remote import RemoteGitBranchFormat
922
(RemoteGitBranchFormat(), default_format),
923
(LocalGitBranchFormat(), default_format)]
926
def _get_interrepo(self, source, target):
927
return _mod_repository.InterRepository.get(
928
source.repository, target.repository)
931
def is_compatible(cls, source, target):
932
if not isinstance(source, GitBranch):
934
if isinstance(target, GitBranch):
935
# InterLocalGitRemoteGitBranch or InterToGitBranch should be used
937
if (getattr(cls._get_interrepo(source, target), "fetch_objects", None)
939
# fetch_objects is necessary for this to work
943
def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
944
self.fetch_objects(stop_revision, fetch_tags=fetch_tags, limit=limit)
946
def fetch_objects(self, stop_revision, fetch_tags, limit=None):
947
interrepo = self._get_interrepo(self.source, self.target)
948
if fetch_tags is None:
949
c = self.source.get_config_stack()
950
fetch_tags = c.get('branch.fetch_tags')
952
def determine_wants(heads):
953
if stop_revision is None:
955
head = heads[self.source.ref]
957
self._last_revid = revision.NULL_REVISION
959
self._last_revid = self.source.lookup_foreign_revision_id(
962
self._last_revid = stop_revision
963
real = interrepo.get_determine_wants_revids(
964
[self._last_revid], include_tags=fetch_tags)
966
pack_hint, head, refs = interrepo.fetch_objects(
967
determine_wants, self.source.mapping, limit=limit)
968
if (pack_hint is not None and
969
self.target.repository._format.pack_compresses):
970
self.target.repository.pack(hint=pack_hint)
973
def _update_revisions(self, stop_revision=None, overwrite=False):
974
head, refs = self.fetch_objects(stop_revision, fetch_tags=None)
976
prev_last_revid = None
978
prev_last_revid = self.target.last_revision()
979
self.target.generate_revision_history(
980
self._last_revid, last_rev=prev_last_revid,
981
other_branch=self.source)
984
def _basic_pull(self, stop_revision, overwrite, run_hooks,
985
_override_hook_target, _hook_master):
986
if overwrite is True:
987
overwrite = set(["history", "tags"])
990
result = GitBranchPullResult()
991
result.source_branch = self.source
992
if _override_hook_target is None:
993
result.target_branch = self.target
995
result.target_branch = _override_hook_target
996
with self.target.lock_write(), self.source.lock_read():
997
# We assume that during 'pull' the target repository is closer than
999
(result.old_revno, result.old_revid) = \
1000
self.target.last_revision_info()
1001
result.new_git_head, remote_refs = self._update_revisions(
1002
stop_revision, overwrite=("history" in overwrite))
1003
tags_ret = self.source.tags.merge_to(
1004
self.target.tags, ("tags" in overwrite), ignore_master=True)
1005
if isinstance(tags_ret, tuple):
1006
result.tag_updates, result.tag_conflicts = tags_ret
1008
result.tag_conflicts = tags_ret
1009
(result.new_revno, result.new_revid) = \
1010
self.target.last_revision_info()
1012
result.master_branch = _hook_master
1013
result.local_branch = result.target_branch
1015
result.master_branch = result.target_branch
1016
result.local_branch = None
1018
for hook in branch.Branch.hooks['post_pull']:
1022
def pull(self, overwrite=False, stop_revision=None,
1023
possible_transports=None, _hook_master=None, run_hooks=True,
1024
_override_hook_target=None, local=False):
1027
:param _hook_master: Private parameter - set the branch to
1028
be supplied as the master to pull hooks.
1029
:param run_hooks: Private parameter - if false, this branch
1030
is being called because it's the master of the primary branch,
1031
so it should not run its hooks.
1032
:param _override_hook_target: Private parameter - set the branch to be
1033
supplied as the target_branch to pull hooks.
1035
# This type of branch can't be bound.
1036
bound_location = self.target.get_bound_location()
1037
if local and not bound_location:
1038
raise errors.LocalRequiresBoundBranch()
1039
source_is_master = False
1040
with cleanup.ExitStack() as es:
1041
es.enter_context(self.source.lock_read())
1043
# bound_location comes from a config file, some care has to be
1044
# taken to relate it to source.user_url
1045
normalized = urlutils.normalize_url(bound_location)
1047
relpath = self.source.user_transport.relpath(normalized)
1048
source_is_master = (relpath == '')
1049
except (errors.PathNotChild, urlutils.InvalidURL):
1050
source_is_master = False
1051
if not local and bound_location and not source_is_master:
1052
# not pulling from master, so we need to update master.
1053
master_branch = self.target.get_master_branch(possible_transports)
1054
es.enter_context(master_branch.lock_write())
1055
# pull from source into master.
1056
master_branch.pull(self.source, overwrite, stop_revision,
1059
master_branch = None
1060
return self._basic_pull(stop_revision, overwrite, run_hooks,
1061
_override_hook_target,
1062
_hook_master=master_branch)
1064
def _basic_push(self, overwrite, stop_revision):
1065
if overwrite is True:
1066
overwrite = set(["history", "tags"])
1069
result = branch.BranchPushResult()
1070
result.source_branch = self.source
1071
result.target_branch = self.target
1072
result.old_revno, result.old_revid = self.target.last_revision_info()
1073
result.new_git_head, remote_refs = self._update_revisions(
1074
stop_revision, overwrite=("history" in overwrite))
1075
tags_ret = self.source.tags.merge_to(
1076
self.target.tags, "tags" in overwrite, ignore_master=True)
1077
(result.tag_updates, result.tag_conflicts) = tags_ret
1078
result.new_revno, result.new_revid = self.target.last_revision_info()
1082
class InterGitBranch(branch.GenericInterBranch):
1083
"""InterBranch implementation that pulls between Git branches."""
1085
def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
1086
raise NotImplementedError(self.fetch)
1089
class InterLocalGitRemoteGitBranch(InterGitBranch):
1090
"""InterBranch that copies from a local to a remote git branch."""
1093
def _get_branch_formats_to_test():
1094
from .remote import RemoteGitBranchFormat
1096
(LocalGitBranchFormat(), RemoteGitBranchFormat())]
1099
def is_compatible(self, source, target):
1100
from .remote import RemoteGitBranch
1101
return (isinstance(source, LocalGitBranch) and
1102
isinstance(target, RemoteGitBranch))
1104
def _basic_push(self, overwrite, stop_revision):
1105
result = GitBranchPushResult()
1106
result.source_branch = self.source
1107
result.target_branch = self.target
1108
if stop_revision is None:
1109
stop_revision = self.source.last_revision()
1111
def get_changed_refs(old_refs):
1112
old_ref = old_refs.get(self.target.ref, None)
1114
result.old_revid = revision.NULL_REVISION
1116
result.old_revid = self.target.lookup_foreign_revision_id(
1118
new_ref = self.source.repository.lookup_bzr_revision_id(
1121
if remote_divergence(
1123
self.source.repository._git.object_store):
1124
raise errors.DivergedBranches(self.source, self.target)
1125
refs = {self.target.ref: new_ref}
1126
result.new_revid = stop_revision
1127
for name, sha in viewitems(
1128
self.source.repository._git.refs.as_dict(b"refs/tags")):
1129
if sha not in self.source.repository._git:
1130
trace.mutter('Ignoring missing SHA: %s', sha)
1132
refs[tag_name_to_ref(name)] = sha
1134
self.target.repository.send_pack(
1136
self.source.repository._git.object_store.generate_pack_data)
1140
class InterGitLocalGitBranch(InterGitBranch):
1141
"""InterBranch that copies from a remote to a local git branch."""
1144
def _get_branch_formats_to_test():
1145
from .remote import RemoteGitBranchFormat
1147
(RemoteGitBranchFormat(), LocalGitBranchFormat()),
1148
(LocalGitBranchFormat(), LocalGitBranchFormat())]
1151
def is_compatible(self, source, target):
1152
return (isinstance(source, GitBranch) and
1153
isinstance(target, LocalGitBranch))
1155
def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
1156
interrepo = _mod_repository.InterRepository.get(self.source.repository,
1157
self.target.repository)
1158
if stop_revision is None:
1159
stop_revision = self.source.last_revision()
1160
if fetch_tags is None:
1161
c = self.source.get_config_stack()
1162
fetch_tags = c.get('branch.fetch_tags')
1163
determine_wants = interrepo.get_determine_wants_revids(
1164
[stop_revision], include_tags=fetch_tags)
1165
interrepo.fetch_objects(determine_wants, limit=limit)
1167
def _basic_push(self, overwrite=False, stop_revision=None):
1168
if overwrite is True:
1169
overwrite = set(["history", "tags"])
1172
result = GitBranchPushResult()
1173
result.source_branch = self.source
1174
result.target_branch = self.target
1175
result.old_revid = self.target.last_revision()
1176
refs, stop_revision = self.update_refs(stop_revision)
1177
self.target.generate_revision_history(
1179
(result.old_revid if ("history" not in overwrite) else None),
1180
other_branch=self.source)
1181
tags_ret = self.source.tags.merge_to(
1183
source_tag_refs=remote_refs_dict_to_tag_refs(refs),
1184
overwrite=("tags" in overwrite))
1185
if isinstance(tags_ret, tuple):
1186
(result.tag_updates, result.tag_conflicts) = tags_ret
1188
result.tag_conflicts = tags_ret
1189
result.new_revid = self.target.last_revision()
1192
def update_refs(self, stop_revision=None):
1193
interrepo = _mod_repository.InterRepository.get(
1194
self.source.repository, self.target.repository)
1195
c = self.source.get_config_stack()
1196
fetch_tags = c.get('branch.fetch_tags')
1198
if stop_revision is None:
1199
refs = interrepo.fetch(branches=[self.source.ref], include_tags=fetch_tags)
1201
head = refs[self.source.ref]
1203
stop_revision = revision.NULL_REVISION
1205
stop_revision = self.target.lookup_foreign_revision_id(head)
1207
refs = interrepo.fetch(
1208
revision_id=stop_revision, include_tags=fetch_tags)
1209
return refs, stop_revision
1211
def pull(self, stop_revision=None, overwrite=False,
1212
possible_transports=None, run_hooks=True, local=False):
1213
# This type of branch can't be bound.
1215
raise errors.LocalRequiresBoundBranch()
1216
if overwrite is True:
1217
overwrite = set(["history", "tags"])
1221
result = GitPullResult()
1222
result.source_branch = self.source
1223
result.target_branch = self.target
1224
with self.target.lock_write(), self.source.lock_read():
1225
result.old_revid = self.target.last_revision()
1226
refs, stop_revision = self.update_refs(stop_revision)
1227
self.target.generate_revision_history(
1229
(result.old_revid if ("history" not in overwrite) else None),
1230
other_branch=self.source)
1231
tags_ret = self.source.tags.merge_to(
1232
self.target.tags, overwrite=("tags" in overwrite),
1233
source_tag_refs=remote_refs_dict_to_tag_refs(refs))
1234
if isinstance(tags_ret, tuple):
1235
(result.tag_updates, result.tag_conflicts) = tags_ret
1237
result.tag_conflicts = tags_ret
1238
result.new_revid = self.target.last_revision()
1239
result.local_branch = None
1240
result.master_branch = result.target_branch
1242
for hook in branch.Branch.hooks['post_pull']:
1247
class InterToGitBranch(branch.GenericInterBranch):
1248
"""InterBranch implementation that pulls into a Git branch."""
1250
def __init__(self, source, target):
1251
super(InterToGitBranch, self).__init__(source, target)
1252
self.interrepo = _mod_repository.InterRepository.get(source.repository,
1256
def _get_branch_formats_to_test():
1258
default_format = branch.format_registry.get_default()
1259
except AttributeError:
1260
default_format = branch.BranchFormat._default_format
1261
from .remote import RemoteGitBranchFormat
1263
(default_format, LocalGitBranchFormat()),
1264
(default_format, RemoteGitBranchFormat())]
1267
def is_compatible(self, source, target):
1268
return (not isinstance(source, GitBranch) and
1269
isinstance(target, GitBranch))
1271
def _get_new_refs(self, stop_revision=None, fetch_tags=None,
1273
if not self.source.is_locked():
1274
raise errors.ObjectNotLocked(self.source)
1275
if stop_revision is None:
1276
(stop_revno, stop_revision) = self.source.last_revision_info()
1277
elif stop_revno is None:
1279
stop_revno = self.source.revision_id_to_revno(stop_revision)
1280
except errors.NoSuchRevision:
1282
if not isinstance(stop_revision, bytes):
1283
raise TypeError(stop_revision)
1284
main_ref = self.target.ref
1285
refs = {main_ref: (None, stop_revision)}
1286
if fetch_tags is None:
1287
c = self.source.get_config_stack()
1288
fetch_tags = c.get('branch.fetch_tags')
1289
for name, revid in viewitems(self.source.tags.get_tag_dict()):
1290
if self.source.repository.has_revision(revid):
1291
ref = tag_name_to_ref(name)
1292
if not check_ref_format(ref):
1293
warning("skipping tag with invalid characters %s (%s)",
1297
# FIXME: Skip tags that are not in the ancestry
1298
refs[ref] = (None, revid)
1299
return refs, main_ref, (stop_revno, stop_revision)
1301
def _update_refs(self, result, old_refs, new_refs, overwrite):
1302
mutter("updating refs. old refs: %r, new refs: %r",
1304
result.tag_updates = {}
1305
result.tag_conflicts = []
1306
ret = dict(old_refs)
1308
def ref_equals(refs, ref, git_sha, revid):
1313
if (value[0] is not None and
1314
git_sha is not None and
1315
value[0] == git_sha):
1317
if (value[1] is not None and
1318
revid is not None and
1321
# FIXME: If one side only has the git sha available and the other
1322
# only has the bzr revid, then this will cause us to show a tag as
1323
# updated that hasn't actually been updated.
1325
# FIXME: Check for diverged branches
1326
for ref, (git_sha, revid) in viewitems(new_refs):
1327
if ref_equals(ret, ref, git_sha, revid):
1328
# Already up to date
1330
git_sha = old_refs[ref][0]
1332
revid = old_refs[ref][1]
1333
ret[ref] = new_refs[ref] = (git_sha, revid)
1334
elif ref not in ret or overwrite:
1336
tag_name = ref_to_tag_name(ref)
1340
result.tag_updates[tag_name] = revid
1341
ret[ref] = (git_sha, revid)
1343
# FIXME: Check diverged
1347
name = ref_to_tag_name(ref)
1351
result.tag_conflicts.append(
1352
(name, revid, ret[name][1]))
1354
ret[ref] = (git_sha, revid)
1357
def fetch(self, stop_revision=None, fetch_tags=None, lossy=False,
1359
if stop_revision is None:
1360
stop_revision = self.source.last_revision()
1363
for k, v in viewitems(self.source.tags.get_tag_dict()):
1364
ret.append((None, v))
1365
ret.append((None, stop_revision))
1367
self.interrepo.fetch_objects(ret, lossy=lossy, limit=limit)
1368
except NoPushSupport:
1369
raise errors.NoRoundtrippingSupport(self.source, self.target)
1371
def pull(self, overwrite=False, stop_revision=None, local=False,
1372
possible_transports=None, run_hooks=True, _stop_revno=None):
1373
result = GitBranchPullResult()
1374
result.source_branch = self.source
1375
result.target_branch = self.target
1376
with self.source.lock_read(), self.target.lock_write():
1377
new_refs, main_ref, stop_revinfo = self._get_new_refs(
1378
stop_revision, stop_revno=_stop_revno)
1380
def update_refs(old_refs):
1381
return self._update_refs(result, old_refs, new_refs, overwrite)
1383
result.revidmap, old_refs, new_refs = (
1384
self.interrepo.fetch_refs(update_refs, lossy=False))
1385
except NoPushSupport:
1386
raise errors.NoRoundtrippingSupport(self.source, self.target)
1387
(old_sha1, result.old_revid) = old_refs.get(
1388
main_ref, (ZERO_SHA, NULL_REVISION))
1389
if result.old_revid is None:
1390
result.old_revid = self.target.lookup_foreign_revision_id(
1392
result.new_revid = new_refs[main_ref][1]
1393
result.local_branch = None
1394
result.master_branch = self.target
1396
for hook in branch.Branch.hooks['post_pull']:
1400
def push(self, overwrite=False, stop_revision=None, lossy=False,
1401
_override_hook_source_branch=None, _stop_revno=None):
1402
result = GitBranchPushResult()
1403
result.source_branch = self.source
1404
result.target_branch = self.target
1405
result.local_branch = None
1406
result.master_branch = result.target_branch
1407
with self.source.lock_read(), self.target.lock_write():
1408
new_refs, main_ref, stop_revinfo = self._get_new_refs(
1409
stop_revision, stop_revno=_stop_revno)
1411
def update_refs(old_refs):
1412
return self._update_refs(result, old_refs, new_refs, overwrite)
1414
result.revidmap, old_refs, new_refs = (
1415
self.interrepo.fetch_refs(
1416
update_refs, lossy=lossy, overwrite=overwrite))
1417
except NoPushSupport:
1418
raise errors.NoRoundtrippingSupport(self.source, self.target)
1419
(old_sha1, result.old_revid) = old_refs.get(
1420
main_ref, (ZERO_SHA, NULL_REVISION))
1421
if result.old_revid is None:
1422
result.old_revid = self.target.lookup_foreign_revision_id(
1424
result.new_revid = new_refs[main_ref][1]
1425
(result.new_original_revno,
1426
result.new_original_revid) = stop_revinfo
1427
for hook in branch.Branch.hooks['post_push']:
1432
branch.InterBranch.register_optimiser(InterGitLocalGitBranch)
1433
branch.InterBranch.register_optimiser(InterFromGitBranch)
1434
branch.InterBranch.register_optimiser(InterToGitBranch)
1435
branch.InterBranch.register_optimiser(InterLocalGitRemoteGitBranch)