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.config import (
26
ConfigFile as GitConfigFile,
30
from dulwich.objects import (
34
from dulwich.repo import check_ref_format
43
repository as _mod_repository,
49
from ..foreign import ForeignBranch
50
from ..revision import (
53
from ..sixish import (
70
from .mapping import (
82
remote_refs_dict_to_tag_refs,
85
from .unpeel_map import (
94
def _calculate_revnos(branch):
95
if branch._format.stores_revno():
97
config = branch.get_config_stack()
98
return config.get('calculate_revnos')
101
class GitPullResult(branch.PullResult):
102
"""Result of a pull from a Git branch."""
104
def _lookup_revno(self, revid):
105
if not isinstance(revid, bytes):
106
raise TypeError(revid)
107
if not _calculate_revnos(self.target_branch):
109
# Try in source branch first, it'll be faster
110
with self.target_branch.lock_read():
111
return self.target_branch.revision_id_to_revno(revid)
115
return self._lookup_revno(self.old_revid)
119
return self._lookup_revno(self.new_revid)
122
class InterTagsFromGitToRemoteGit(InterTags):
125
def is_compatible(klass, source, target):
126
if not isinstance(source, GitTags):
128
if not isinstance(target, GitTags):
130
if getattr(target.branch.repository, "_git", None) is not None:
134
def merge(self, overwrite=False, ignore_master=False, selector=None):
135
if self.source.branch.repository.has_same_location(self.target.branch.repository):
139
source_tag_refs = self.source.branch.get_tag_refs()
142
def get_changed_refs(old_refs):
144
for ref_name, tag_name, peeled, unpeeled in (
145
source_tag_refs.iteritems()):
146
if selector and not selector(tag_name):
148
if old_refs.get(ref_name) == unpeeled:
150
elif overwrite or ref_name not in old_refs:
151
ret[ref_name] = unpeeled
152
updates[tag_name] = self.target.branch.repository.lookup_foreign_revision_id(
154
ref_to_tag_map[ref_name] = tag_name
155
self.target.branch._tag_refs = None
159
self.repository.lookup_foreign_revision_id(peeled),
160
self.target.branch.repository.lookup_foreign_revision_id(
161
old_refs[ref_name])))
163
result = self.target.branch.repository.controldir.send_pack(
164
get_changed_refs, lambda have, want: [])
165
if result is not None and not isinstance(result, dict):
166
for ref, error in result.ref_status.items():
168
warning('unable to update ref %s: %s',
170
del updates[ref_to_tag_map[ref]]
171
return updates, set(conflicts)
174
class InterTagsFromGitToLocalGit(InterTags):
177
def is_compatible(klass, source, target):
178
if not isinstance(source, GitTags):
180
if not isinstance(target, GitTags):
182
if getattr(target.branch.repository, "_git", None) is None:
186
def merge(self, overwrite=False, ignore_master=False, selector=None):
187
if self.source.branch.repository.has_same_location(self.target.branch.repository):
192
source_tag_refs = self.source.branch.get_tag_refs()
194
target_repo = self.target.branch.repository
196
for ref_name, tag_name, peeled, unpeeled in source_tag_refs:
197
if selector and not selector(tag_name):
199
if target_repo._git.refs.get(ref_name) == unpeeled:
201
elif overwrite or ref_name not in target_repo._git.refs:
203
updates[tag_name] = (
204
target_repo.lookup_foreign_revision_id(peeled))
206
trace.warning('%s does not point to a valid object',
209
except NotCommitError:
210
trace.warning('%s points to a non-commit object',
213
target_repo._git.refs[ref_name] = unpeeled or peeled
214
self.target.branch._tag_refs = None
217
source_revid = self.source.branch.repository.lookup_foreign_revision_id(
219
target_revid = target_repo.lookup_foreign_revision_id(
220
target_repo._git.refs[ref_name])
222
trace.warning('%s does not point to a valid object',
225
except NotCommitError:
226
trace.warning('%s points to a non-commit object',
229
conflicts.append((tag_name, source_revid, target_revid))
230
return updates, set(conflicts)
233
class InterTagsFromGitToNonGit(InterTags):
236
def is_compatible(klass, source, target):
237
if not isinstance(source, GitTags):
239
if isinstance(target, GitTags):
243
def merge(self, overwrite=False, ignore_master=False, selector=None):
244
"""See Tags.merge_to."""
245
source_tag_refs = self.source.branch.get_tag_refs()
249
master = self.target.branch.get_master_branch()
250
with cleanup.ExitStack() as es:
251
if master is not None:
252
es.enter_context(master.lock_write())
253
updates, conflicts = self._merge_to(
254
self.target, source_tag_refs, overwrite=overwrite,
256
if master is not None:
257
extra_updates, extra_conflicts = self._merge_to(
258
master.tags, overwrite=overwrite,
259
source_tag_refs=source_tag_refs,
260
ignore_master=ignore_master, selector=selector)
261
updates.update(extra_updates)
262
conflicts.update(extra_conflicts)
263
return updates, conflicts
265
def _merge_to(self, to_tags, source_tag_refs, overwrite=False,
267
unpeeled_map = defaultdict(set)
270
result = dict(to_tags.get_tag_dict())
271
for ref_name, tag_name, peeled, unpeeled in source_tag_refs:
272
if selector and not selector(tag_name):
274
if unpeeled is not None:
275
unpeeled_map[peeled].add(unpeeled)
277
bzr_revid = self.source.branch.lookup_foreign_revision_id(peeled)
278
except NotCommitError:
280
if result.get(tag_name) == bzr_revid:
282
elif tag_name not in result or overwrite:
283
result[tag_name] = bzr_revid
284
updates[tag_name] = bzr_revid
286
conflicts.append((tag_name, bzr_revid, result[tag_name]))
287
to_tags._set_tag_dict(result)
288
if len(unpeeled_map) > 0:
289
map_file = UnpeelMap.from_repository(to_tags.branch.repository)
290
map_file.update(unpeeled_map)
291
map_file.save_in_repository(to_tags.branch.repository)
292
return updates, set(conflicts)
295
InterTags.register_optimiser(InterTagsFromGitToRemoteGit)
296
InterTags.register_optimiser(InterTagsFromGitToLocalGit)
297
InterTags.register_optimiser(InterTagsFromGitToNonGit)
301
"""Ref-based tag dictionary."""
303
def __init__(self, branch):
305
self.repository = branch.repository
307
def get_tag_dict(self):
309
for (ref_name, tag_name, peeled, unpeeled) in (
310
self.branch.get_tag_refs()):
312
bzr_revid = self.branch.lookup_foreign_revision_id(peeled)
313
except NotCommitError:
316
ret[tag_name] = bzr_revid
319
def lookup_tag(self, tag_name):
320
"""Return the referent string of a tag"""
321
# TODO(jelmer): Replace with something more efficient for local tags.
322
td = self.get_tag_dict()
326
raise errors.NoSuchTag(tag_name)
329
class LocalGitTagDict(GitTags):
330
"""Dictionary with tags in a local repository."""
332
def __init__(self, branch):
333
super(LocalGitTagDict, self).__init__(branch)
334
self.refs = self.repository.controldir._git.refs
336
def _set_tag_dict(self, to_dict):
337
extra = set(self.refs.allkeys())
338
for k, revid in viewitems(to_dict):
339
name = tag_name_to_ref(k)
343
self.set_tag(k, revid)
344
except errors.GhostTagsNotSupported:
348
del self.repository._git[name]
350
def set_tag(self, name, revid):
352
git_sha, mapping = self.branch.lookup_bzr_revision_id(revid)
353
except errors.NoSuchRevision:
354
raise errors.GhostTagsNotSupported(self)
355
self.refs[tag_name_to_ref(name)] = git_sha
356
self.branch._tag_refs = None
358
def delete_tag(self, name):
359
ref = tag_name_to_ref(name)
360
if ref not in self.refs:
361
raise errors.NoSuchTag(name)
363
self.branch._tag_refs = None
366
class GitBranchFormat(branch.BranchFormat):
368
def network_name(self):
371
def supports_tags(self):
374
def supports_leaving_lock(self):
377
def supports_tags_referencing_ghosts(self):
380
def tags_are_versioned(self):
383
def get_foreign_tests_branch_factory(self):
384
from .tests.test_branch import ForeignTestsBranchFactory
385
return ForeignTestsBranchFactory()
387
def make_tags(self, branch):
390
except AttributeError:
392
if getattr(branch.repository, "_git", None) is None:
393
from .remote import RemoteGitTagDict
394
return RemoteGitTagDict(branch)
396
return LocalGitTagDict(branch)
398
def initialize(self, a_controldir, name=None, repository=None,
399
append_revisions_only=None):
400
raise NotImplementedError(self.initialize)
402
def get_reference(self, controldir, name=None):
403
return controldir.get_branch_reference(name=name)
405
def set_reference(self, controldir, name, target):
406
return controldir.set_branch_reference(target, name)
408
def stores_revno(self):
409
"""True if this branch format store revision numbers."""
412
supports_reference_locations = False
415
class LocalGitBranchFormat(GitBranchFormat):
417
def get_format_description(self):
418
return 'Local Git Branch'
421
def _matchingcontroldir(self):
422
from .dir import LocalGitControlDirFormat
423
return LocalGitControlDirFormat()
425
def initialize(self, a_controldir, name=None, repository=None,
426
append_revisions_only=None):
427
from .dir import LocalGitDir
428
if not isinstance(a_controldir, LocalGitDir):
429
raise errors.IncompatibleFormat(self, a_controldir._format)
430
return a_controldir.create_branch(
431
repository=repository, name=name,
432
append_revisions_only=append_revisions_only)
435
class GitBranch(ForeignBranch):
436
"""An adapter to git repositories for bzr Branch objects."""
439
def control_transport(self):
440
return self._control_transport
443
def user_transport(self):
444
return self._user_transport
446
def __init__(self, controldir, repository, ref, format):
447
self.repository = repository
448
self._format = format
449
self.controldir = controldir
450
self._lock_mode = None
452
super(GitBranch, self).__init__(repository.get_mapping())
455
self._user_transport = controldir.user_transport.clone('.')
456
self._control_transport = controldir.control_transport.clone('.')
457
self._tag_refs = None
460
self.name = ref_to_branch_name(ref)
463
if self.ref is not None:
464
params = {"ref": urlutils.escape(self.ref)}
467
params = {"branch": urlutils.escape(self.name)}
468
for k, v in params.items():
469
self._user_transport.set_segment_parameter(k, v)
470
self._control_transport.set_segment_parameter(k, v)
471
self.base = controldir.user_transport.base
473
def _get_checkout_format(self, lightweight=False):
474
"""Return the most suitable metadir for a checkout of this branch.
475
Weaves are used if this branch's repository uses weaves.
478
return controldir.format_registry.make_controldir("git")
480
return controldir.format_registry.make_controldir("default")
482
def get_child_submit_format(self):
483
"""Return the preferred format of submissions to this branch."""
484
ret = self.get_config_stack().get("child_submit_format")
489
def get_config(self):
490
from .config import GitBranchConfig
491
return GitBranchConfig(self)
493
def get_config_stack(self):
494
from .config import GitBranchStack
495
return GitBranchStack(self)
497
def _get_nick(self, local=False, possible_master_transports=None):
498
"""Find the nick name for this branch.
502
if getattr(self.repository, '_git', None):
503
cs = self.repository._git.get_config_stack()
505
return cs.get((b"branch", self.name.encode('utf-8')),
506
b"nick").decode("utf-8")
509
return self.name or u"HEAD"
511
def _set_nick(self, nick):
512
cf = self.repository._git.get_config()
513
cf.set((b"branch", self.name.encode('utf-8')),
514
b"nick", nick.encode("utf-8"))
517
self.repository._git._put_named_file('config', f.getvalue())
519
nick = property(_get_nick, _set_nick)
522
return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
525
def generate_revision_history(self, revid, last_rev=None,
527
if last_rev is not None:
528
graph = self.repository.get_graph()
529
if not graph.is_ancestor(last_rev, revid):
530
# our previous tip is not merged into stop_revision
531
raise errors.DivergedBranches(self, other_branch)
533
self.set_last_revision(revid)
535
def lock_write(self, token=None):
536
if token is not None:
537
raise errors.TokenLockingNotSupported(self)
539
if self._lock_mode == 'r':
540
raise errors.ReadOnlyError(self)
541
self._lock_count += 1
544
self._lock_mode = 'w'
546
self.repository.lock_write()
547
return lock.LogicalLockResult(self.unlock)
549
def leave_lock_in_place(self):
550
raise NotImplementedError(self.leave_lock_in_place)
552
def dont_leave_lock_in_place(self):
553
raise NotImplementedError(self.dont_leave_lock_in_place)
555
def get_stacked_on_url(self):
556
# Git doesn't do stacking (yet...)
557
raise branch.UnstackableBranchFormat(self._format, self.base)
559
def _get_push_origin(self, cs):
560
"""Get the name for the push origin.
562
The exact behaviour is documented in the git-config(1) manpage.
565
return cs.get((b'branch', self.name.encode('utf-8')), b'pushRemote')
568
return cs.get((b'branch', ), b'remote')
571
return cs.get((b'branch', self.name.encode('utf-8')), b'remote')
575
def _get_origin(self, cs):
577
return cs.get((b'branch', self.name.encode('utf-8')), b'remote')
581
def _get_related_push_branch(self, cs):
582
remote = self._get_push_origin(cs)
584
location = cs.get((b"remote", remote), b"url")
588
return git_url_to_bzr_url(location.decode('utf-8'), ref=self.ref)
590
def _get_related_merge_branch(self, cs):
591
remote = self._get_origin(cs)
593
location = cs.get((b"remote", remote), b"url")
598
ref = cs.get((b"branch", remote), b"merge")
602
return git_url_to_bzr_url(location.decode('utf-8'), ref=ref)
604
def _get_parent_location(self):
605
"""See Branch.get_parent()."""
606
cs = self.repository._git.get_config_stack()
607
return self._get_related_merge_branch(cs)
609
def _write_git_config(self, cs):
612
self.repository._git._put_named_file('config', f.getvalue())
614
def set_parent(self, location):
615
cs = self.repository._git.get_config()
616
remote = self._get_origin(cs)
617
this_url = urlutils.strip_segment_parameters(self.user_url)
618
target_url, branch, ref = bzr_url_to_git_url(location)
619
location = urlutils.relative_url(this_url, target_url)
620
cs.set((b"remote", remote), b"url", location)
622
cs.set((b"branch", remote), b"merge", branch_name_to_ref(branch))
624
cs.set((b"branch", remote), b"merge", ref)
626
# TODO(jelmer): Maybe unset rather than setting to HEAD?
627
cs.set((b"branch", remote), b"merge", b'HEAD')
628
self._write_git_config(cs)
630
def break_lock(self):
631
raise NotImplementedError(self.break_lock)
635
if self._lock_mode not in ('r', 'w'):
636
raise ValueError(self._lock_mode)
637
self._lock_count += 1
639
self._lock_mode = 'r'
641
self.repository.lock_read()
642
return lock.LogicalLockResult(self.unlock)
644
def peek_lock_mode(self):
645
return self._lock_mode
648
return (self._lock_mode is not None)
653
def _unlock_ref(self):
657
"""See Branch.unlock()."""
658
if self._lock_count == 0:
659
raise errors.LockNotHeld(self)
661
self._lock_count -= 1
662
if self._lock_count == 0:
663
if self._lock_mode == 'w':
665
self._lock_mode = None
666
self._clear_cached_state()
668
self.repository.unlock()
670
def get_physical_lock_status(self):
673
def last_revision(self):
674
with self.lock_read():
675
# perhaps should escape this ?
676
if self.head is None:
677
return revision.NULL_REVISION
678
return self.lookup_foreign_revision_id(self.head)
680
def _basic_push(self, target, overwrite=False, stop_revision=None,
682
return branch.InterBranch.get(self, target)._basic_push(
683
overwrite, stop_revision, tag_selector=tag_selector)
685
def lookup_foreign_revision_id(self, foreign_revid):
687
return self.repository.lookup_foreign_revision_id(foreign_revid,
691
return self.mapping.revision_id_foreign_to_bzr(foreign_revid)
693
def lookup_bzr_revision_id(self, revid):
694
return self.repository.lookup_bzr_revision_id(
695
revid, mapping=self.mapping)
697
def get_unshelver(self, tree):
698
raise errors.StoringUncommittedNotSupported(self)
700
def _clear_cached_state(self):
701
super(GitBranch, self)._clear_cached_state()
702
self._tag_refs = None
704
def _iter_tag_refs(self, refs):
705
"""Iterate over the tag refs.
707
:param refs: Refs dictionary (name -> git sha1)
708
:return: iterator over (ref_name, tag_name, peeled_sha1, unpeeled_sha1)
710
raise NotImplementedError(self._iter_tag_refs)
712
def get_tag_refs(self):
713
with self.lock_read():
714
if self._tag_refs is None:
715
self._tag_refs = list(self._iter_tag_refs())
716
return self._tag_refs
718
def import_last_revision_info_and_tags(self, source, revno, revid,
720
"""Set the last revision info, importing from another repo if necessary.
722
This is used by the bound branch code to upload a revision to
723
the master branch first before updating the tip of the local branch.
724
Revisions referenced by source's tags are also transferred.
726
:param source: Source branch to optionally fetch from
727
:param revno: Revision number of the new tip
728
:param revid: Revision id of the new tip
729
:param lossy: Whether to discard metadata that can not be
731
:return: Tuple with the new revision number and revision id
732
(should only be different from the arguments when lossy=True)
734
push_result = source.push(
735
self, stop_revision=revid, lossy=lossy, _stop_revno=revno)
736
return (push_result.new_revno, push_result.new_revid)
738
def reconcile(self, thorough=True):
739
"""Make sure the data stored in this branch is consistent."""
740
from ..reconcile import ReconcileResult
742
return ReconcileResult()
745
class LocalGitBranch(GitBranch):
746
"""A local Git branch."""
748
def __init__(self, controldir, repository, ref):
749
super(LocalGitBranch, self).__init__(controldir, repository, ref,
750
LocalGitBranchFormat())
752
def create_checkout(self, to_location, revision_id=None, lightweight=False,
753
accelerator_tree=None, hardlink=False):
754
t = transport.get_transport(to_location)
756
format = self._get_checkout_format(lightweight=lightweight)
757
checkout = format.initialize_on_transport(t)
759
from_branch = checkout.set_branch_reference(target_branch=self)
761
policy = checkout.determine_repository_policy()
762
policy.acquire_repository()
763
checkout_branch = checkout.create_branch()
764
checkout_branch.bind(self)
765
checkout_branch.pull(self, stop_revision=revision_id)
767
return checkout.create_workingtree(
768
revision_id, from_branch=from_branch, hardlink=hardlink)
771
self._ref_lock = self.repository._git.refs.lock_ref(self.ref)
773
def _unlock_ref(self):
774
self._ref_lock.unlock()
776
def break_lock(self):
777
self.repository._git.refs.unlock_ref(self.ref)
779
def _gen_revision_history(self):
780
if self.head is None:
782
last_revid = self.last_revision()
783
graph = self.repository.get_graph()
785
ret = list(graph.iter_lefthand_ancestry(
786
last_revid, (revision.NULL_REVISION, )))
787
except errors.RevisionNotPresent as e:
788
raise errors.GhostRevisionsHaveNoRevno(last_revid, e.revision_id)
794
return self.repository._git.refs[self.ref]
798
def _read_last_revision_info(self):
799
last_revid = self.last_revision()
800
graph = self.repository.get_graph()
802
revno = graph.find_distance_to_null(
803
last_revid, [(revision.NULL_REVISION, 0)])
804
except errors.GhostRevisionsHaveNoRevno:
806
return revno, last_revid
808
def set_last_revision_info(self, revno, revision_id):
809
self.set_last_revision(revision_id)
810
self._last_revision_info_cache = revno, revision_id
812
def set_last_revision(self, revid):
813
if not revid or not isinstance(revid, bytes):
814
raise errors.InvalidRevisionId(revision_id=revid, branch=self)
815
if revid == NULL_REVISION:
818
(newhead, self.mapping) = self.repository.lookup_bzr_revision_id(
820
if self.mapping is None:
822
self._set_head(newhead)
824
def _set_head(self, value):
825
if value == ZERO_SHA:
826
raise ValueError(value)
829
del self.repository._git.refs[self.ref]
831
self.repository._git.refs[self.ref] = self._head
832
self._clear_cached_state()
834
head = property(_get_head, _set_head)
836
def get_push_location(self):
837
"""See Branch.get_push_location."""
838
push_loc = self.get_config_stack().get('push_location')
839
if push_loc is not None:
841
cs = self.repository._git.get_config_stack()
842
return self._get_related_push_branch(cs)
844
def set_push_location(self, location):
845
"""See Branch.set_push_location."""
846
self.get_config().set_user_option('push_location', location,
847
store=config.STORE_LOCATION)
849
def supports_tags(self):
852
def store_uncommitted(self, creator):
853
raise errors.StoringUncommittedNotSupported(self)
855
def _iter_tag_refs(self):
856
"""Iterate over the tag refs.
858
:param refs: Refs dictionary (name -> git sha1)
859
:return: iterator over (ref_name, tag_name, peeled_sha1, unpeeled_sha1)
861
refs = self.repository.controldir.get_refs_container()
862
for ref_name, unpeeled in viewitems(refs.as_dict()):
864
tag_name = ref_to_tag_name(ref_name)
865
except (ValueError, UnicodeDecodeError):
867
peeled = refs.get_peeled(ref_name)
870
if not isinstance(tag_name, text_type):
871
raise TypeError(tag_name)
872
yield (ref_name, tag_name, peeled, unpeeled)
874
def create_memorytree(self):
875
from .memorytree import GitMemoryTree
876
return GitMemoryTree(self, self.repository._git.object_store,
880
def _quick_lookup_revno(local_branch, remote_branch, revid):
881
if not isinstance(revid, bytes):
882
raise TypeError(revid)
883
# Try in source branch first, it'll be faster
884
with local_branch.lock_read():
885
if not _calculate_revnos(local_branch):
888
return local_branch.revision_id_to_revno(revid)
889
except errors.NoSuchRevision:
890
graph = local_branch.repository.get_graph()
892
return graph.find_distance_to_null(
893
revid, [(revision.NULL_REVISION, 0)])
894
except errors.GhostRevisionsHaveNoRevno:
895
if not _calculate_revnos(remote_branch):
897
# FIXME: Check using graph.find_distance_to_null() ?
898
with remote_branch.lock_read():
899
return remote_branch.revision_id_to_revno(revid)
902
class GitBranchPullResult(branch.PullResult):
905
super(GitBranchPullResult, self).__init__()
906
self.new_git_head = None
907
self._old_revno = None
908
self._new_revno = None
910
def report(self, to_file):
912
if self.old_revid == self.new_revid:
913
to_file.write('No revisions to pull.\n')
914
elif self.new_git_head is not None:
915
to_file.write('Now on revision %d (git sha: %s).\n' %
916
(self.new_revno, self.new_git_head))
918
to_file.write('Now on revision %d.\n' % (self.new_revno,))
919
self._show_tag_conficts(to_file)
921
def _lookup_revno(self, revid):
922
return _quick_lookup_revno(self.target_branch, self.source_branch,
925
def _get_old_revno(self):
926
if self._old_revno is not None:
927
return self._old_revno
928
return self._lookup_revno(self.old_revid)
930
def _set_old_revno(self, revno):
931
self._old_revno = revno
933
old_revno = property(_get_old_revno, _set_old_revno)
935
def _get_new_revno(self):
936
if self._new_revno is not None:
937
return self._new_revno
938
return self._lookup_revno(self.new_revid)
940
def _set_new_revno(self, revno):
941
self._new_revno = revno
943
new_revno = property(_get_new_revno, _set_new_revno)
946
class GitBranchPushResult(branch.BranchPushResult):
948
def _lookup_revno(self, revid):
949
return _quick_lookup_revno(self.source_branch, self.target_branch,
954
return self._lookup_revno(self.old_revid)
958
new_original_revno = getattr(self, "new_original_revno", None)
959
if new_original_revno:
960
return new_original_revno
961
if getattr(self, "new_original_revid", None) is not None:
962
return self._lookup_revno(self.new_original_revid)
963
return self._lookup_revno(self.new_revid)
966
class InterFromGitBranch(branch.GenericInterBranch):
967
"""InterBranch implementation that pulls from Git into bzr."""
970
def _get_branch_formats_to_test():
972
default_format = branch.format_registry.get_default()
973
except AttributeError:
974
default_format = branch.BranchFormat._default_format
975
from .remote import RemoteGitBranchFormat
977
(RemoteGitBranchFormat(), default_format),
978
(LocalGitBranchFormat(), default_format)]
981
def _get_interrepo(self, source, target):
982
return _mod_repository.InterRepository.get(
983
source.repository, target.repository)
986
def is_compatible(cls, source, target):
987
if not isinstance(source, GitBranch):
989
if isinstance(target, GitBranch):
990
# InterLocalGitRemoteGitBranch or InterToGitBranch should be used
992
if (getattr(cls._get_interrepo(source, target), "fetch_objects", None)
994
# fetch_objects is necessary for this to work
998
def fetch(self, stop_revision=None, fetch_tags=None, limit=None, lossy=False):
1000
stop_revision, fetch_tags=fetch_tags, limit=limit, lossy=lossy)
1001
return _mod_repository.FetchResult()
1003
def fetch_objects(self, stop_revision, fetch_tags, limit=None, lossy=False, tag_selector=None):
1004
interrepo = self._get_interrepo(self.source, self.target)
1005
if fetch_tags is None:
1006
c = self.source.get_config_stack()
1007
fetch_tags = c.get('branch.fetch_tags')
1009
def determine_wants(heads):
1010
if stop_revision is None:
1012
head = heads[self.source.ref]
1014
self._last_revid = revision.NULL_REVISION
1016
self._last_revid = self.source.lookup_foreign_revision_id(
1019
self._last_revid = stop_revision
1020
real = interrepo.get_determine_wants_revids(
1021
[self._last_revid], include_tags=fetch_tags, tag_selector=tag_selector)
1023
pack_hint, head, refs = interrepo.fetch_objects(
1024
determine_wants, self.source.mapping, limit=limit,
1026
if (pack_hint is not None and
1027
self.target.repository._format.pack_compresses):
1028
self.target.repository.pack(hint=pack_hint)
1031
def _update_revisions(self, stop_revision=None, overwrite=False, tag_selector=None):
1032
head, refs = self.fetch_objects(stop_revision, fetch_tags=None, tag_selector=tag_selector)
1034
prev_last_revid = None
1036
prev_last_revid = self.target.last_revision()
1037
self.target.generate_revision_history(
1038
self._last_revid, last_rev=prev_last_revid,
1039
other_branch=self.source)
1042
def update_references(self, revid=None):
1044
revid = self.target.last_revision()
1045
tree = self.target.repository.revision_tree(revid)
1047
with tree.get_file('.gitmodules') as f:
1048
for path, url, section in parse_submodules(
1049
GitConfigFile.from_file(f)):
1050
self.target.set_reference_info(
1051
tree.path2id(decode_git_path(path)), url.decode('utf-8'),
1052
decode_git_path(path))
1053
except errors.NoSuchFile:
1056
def _basic_pull(self, stop_revision, overwrite, run_hooks,
1057
_override_hook_target, _hook_master, tag_selector=None):
1058
if overwrite is True:
1059
overwrite = set(["history", "tags"])
1062
result = GitBranchPullResult()
1063
result.source_branch = self.source
1064
if _override_hook_target is None:
1065
result.target_branch = self.target
1067
result.target_branch = _override_hook_target
1068
with self.target.lock_write(), self.source.lock_read():
1069
# We assume that during 'pull' the target repository is closer than
1071
(result.old_revno, result.old_revid) = \
1072
self.target.last_revision_info()
1073
result.new_git_head, remote_refs = self._update_revisions(
1074
stop_revision, overwrite=("history" in overwrite),
1075
tag_selector=tag_selector)
1076
tags_ret = self.source.tags.merge_to(
1077
self.target.tags, ("tags" in overwrite), ignore_master=True)
1078
if isinstance(tags_ret, tuple):
1079
result.tag_updates, result.tag_conflicts = tags_ret
1081
result.tag_conflicts = tags_ret
1082
(result.new_revno, result.new_revid) = \
1083
self.target.last_revision_info()
1084
self.update_references(revid=result.new_revid)
1086
result.master_branch = _hook_master
1087
result.local_branch = result.target_branch
1089
result.master_branch = result.target_branch
1090
result.local_branch = None
1092
for hook in branch.Branch.hooks['post_pull']:
1096
def pull(self, overwrite=False, stop_revision=None,
1097
possible_transports=None, _hook_master=None, run_hooks=True,
1098
_override_hook_target=None, local=False, tag_selector=None):
1101
:param _hook_master: Private parameter - set the branch to
1102
be supplied as the master to pull hooks.
1103
:param run_hooks: Private parameter - if false, this branch
1104
is being called because it's the master of the primary branch,
1105
so it should not run its hooks.
1106
:param _override_hook_target: Private parameter - set the branch to be
1107
supplied as the target_branch to pull hooks.
1109
# This type of branch can't be bound.
1110
bound_location = self.target.get_bound_location()
1111
if local and not bound_location:
1112
raise errors.LocalRequiresBoundBranch()
1113
source_is_master = False
1114
with cleanup.ExitStack() as es:
1115
es.enter_context(self.source.lock_read())
1117
# bound_location comes from a config file, some care has to be
1118
# taken to relate it to source.user_url
1119
normalized = urlutils.normalize_url(bound_location)
1121
relpath = self.source.user_transport.relpath(normalized)
1122
source_is_master = (relpath == '')
1123
except (errors.PathNotChild, urlutils.InvalidURL):
1124
source_is_master = False
1125
if not local and bound_location and not source_is_master:
1126
# not pulling from master, so we need to update master.
1127
master_branch = self.target.get_master_branch(possible_transports)
1128
es.enter_context(master_branch.lock_write())
1129
# pull from source into master.
1130
master_branch.pull(self.source, overwrite, stop_revision,
1133
master_branch = None
1134
return self._basic_pull(stop_revision, overwrite, run_hooks,
1135
_override_hook_target,
1136
_hook_master=master_branch,
1137
tag_selector=tag_selector)
1139
def _basic_push(self, overwrite, stop_revision, tag_selector=None):
1140
if overwrite is True:
1141
overwrite = set(["history", "tags"])
1144
result = branch.BranchPushResult()
1145
result.source_branch = self.source
1146
result.target_branch = self.target
1147
result.old_revno, result.old_revid = self.target.last_revision_info()
1148
result.new_git_head, remote_refs = self._update_revisions(
1149
stop_revision, overwrite=("history" in overwrite),
1150
tag_selector=tag_selector)
1151
tags_ret = self.source.tags.merge_to(
1152
self.target.tags, "tags" in overwrite, ignore_master=True,
1153
selector=tag_selector)
1154
(result.tag_updates, result.tag_conflicts) = tags_ret
1155
result.new_revno, result.new_revid = self.target.last_revision_info()
1156
self.update_references(revid=result.new_revid)
1160
class InterGitBranch(branch.GenericInterBranch):
1161
"""InterBranch implementation that pulls between Git branches."""
1163
def fetch(self, stop_revision=None, fetch_tags=None, limit=None, lossy=False):
1164
raise NotImplementedError(self.fetch)
1167
class InterLocalGitRemoteGitBranch(InterGitBranch):
1168
"""InterBranch that copies from a local to a remote git branch."""
1171
def _get_branch_formats_to_test():
1172
from .remote import RemoteGitBranchFormat
1174
(LocalGitBranchFormat(), RemoteGitBranchFormat())]
1177
def is_compatible(self, source, target):
1178
from .remote import RemoteGitBranch
1179
return (isinstance(source, LocalGitBranch) and
1180
isinstance(target, RemoteGitBranch))
1182
def _basic_push(self, overwrite, stop_revision, tag_selector=None):
1183
from .remote import parse_git_error
1184
result = GitBranchPushResult()
1185
result.source_branch = self.source
1186
result.target_branch = self.target
1187
if stop_revision is None:
1188
stop_revision = self.source.last_revision()
1190
def get_changed_refs(old_refs):
1191
old_ref = old_refs.get(self.target.ref, None)
1193
result.old_revid = revision.NULL_REVISION
1195
result.old_revid = self.target.lookup_foreign_revision_id(
1197
new_ref = self.source.repository.lookup_bzr_revision_id(
1200
if remote_divergence(
1202
self.source.repository._git.object_store):
1203
raise errors.DivergedBranches(self.source, self.target)
1204
refs = {self.target.ref: new_ref}
1205
result.new_revid = stop_revision
1206
for name, sha in viewitems(
1207
self.source.repository._git.refs.as_dict(b"refs/tags")):
1208
if tag_selector and not tag_selector(name):
1210
if sha not in self.source.repository._git:
1211
trace.mutter('Ignoring missing SHA: %s', sha)
1213
refs[tag_name_to_ref(name)] = sha
1215
dw_result = self.target.repository.send_pack(
1217
self.source.repository._git.generate_pack_data)
1218
if dw_result is not None and not isinstance(dw_result, dict):
1219
error = dw_result.ref_status.get(self.target.ref)
1221
raise parse_git_error(self.target.user_url, error)
1222
for ref, error in dw_result.ref_status.items():
1224
trace.warning('unable to open ref %s: %s', ref, error)
1228
class InterGitLocalGitBranch(InterGitBranch):
1229
"""InterBranch that copies from a remote to a local git branch."""
1232
def _get_branch_formats_to_test():
1233
from .remote import RemoteGitBranchFormat
1235
(RemoteGitBranchFormat(), LocalGitBranchFormat()),
1236
(LocalGitBranchFormat(), LocalGitBranchFormat())]
1239
def is_compatible(self, source, target):
1240
return (isinstance(source, GitBranch) and
1241
isinstance(target, LocalGitBranch))
1243
def fetch(self, stop_revision=None, fetch_tags=None, limit=None, lossy=False):
1244
interrepo = _mod_repository.InterRepository.get(
1245
self.source.repository, self.target.repository)
1246
if stop_revision is None:
1247
stop_revision = self.source.last_revision()
1248
if fetch_tags is None:
1249
c = self.source.get_config_stack()
1250
fetch_tags = c.get('branch.fetch_tags')
1251
determine_wants = interrepo.get_determine_wants_revids(
1252
[stop_revision], include_tags=fetch_tags)
1253
interrepo.fetch_objects(determine_wants, limit=limit, lossy=lossy)
1254
return _mod_repository.FetchResult()
1256
def _basic_push(self, overwrite=False, stop_revision=None, tag_selector=None):
1257
if overwrite is True:
1258
overwrite = set(["history", "tags"])
1261
result = GitBranchPushResult()
1262
result.source_branch = self.source
1263
result.target_branch = self.target
1264
result.old_revid = self.target.last_revision()
1265
refs, stop_revision = self.update_refs(stop_revision)
1266
self.target.generate_revision_history(
1268
(result.old_revid if ("history" not in overwrite) else None),
1269
other_branch=self.source)
1270
tags_ret = self.source.tags.merge_to(
1272
overwrite=("tags" in overwrite),
1273
selector=tag_selector)
1274
if isinstance(tags_ret, tuple):
1275
(result.tag_updates, result.tag_conflicts) = tags_ret
1277
result.tag_conflicts = tags_ret
1278
result.new_revid = self.target.last_revision()
1281
def update_refs(self, stop_revision=None):
1282
interrepo = _mod_repository.InterRepository.get(
1283
self.source.repository, self.target.repository)
1284
c = self.source.get_config_stack()
1285
fetch_tags = c.get('branch.fetch_tags')
1287
if stop_revision is None:
1288
result = interrepo.fetch(branches=[self.source.ref], include_tags=fetch_tags)
1290
head = result.refs[self.source.ref]
1292
stop_revision = revision.NULL_REVISION
1294
stop_revision = self.target.lookup_foreign_revision_id(head)
1296
result = interrepo.fetch(
1297
revision_id=stop_revision, include_tags=fetch_tags)
1298
return result.refs, stop_revision
1300
def pull(self, stop_revision=None, overwrite=False,
1301
possible_transports=None, run_hooks=True, local=False,
1303
# This type of branch can't be bound.
1305
raise errors.LocalRequiresBoundBranch()
1306
if overwrite is True:
1307
overwrite = set(["history", "tags"])
1311
result = GitPullResult()
1312
result.source_branch = self.source
1313
result.target_branch = self.target
1314
with self.target.lock_write(), self.source.lock_read():
1315
result.old_revid = self.target.last_revision()
1316
refs, stop_revision = self.update_refs(stop_revision)
1317
self.target.generate_revision_history(
1319
(result.old_revid if ("history" not in overwrite) else None),
1320
other_branch=self.source)
1321
tags_ret = self.source.tags.merge_to(
1322
self.target.tags, overwrite=("tags" in overwrite),
1323
selector=tag_selector)
1324
if isinstance(tags_ret, tuple):
1325
(result.tag_updates, result.tag_conflicts) = tags_ret
1327
result.tag_conflicts = tags_ret
1328
result.new_revid = self.target.last_revision()
1329
result.local_branch = None
1330
result.master_branch = result.target_branch
1332
for hook in branch.Branch.hooks['post_pull']:
1337
class InterToGitBranch(branch.GenericInterBranch):
1338
"""InterBranch implementation that pulls into a Git branch."""
1340
def __init__(self, source, target):
1341
super(InterToGitBranch, self).__init__(source, target)
1342
self.interrepo = _mod_repository.InterRepository.get(source.repository,
1346
def _get_branch_formats_to_test():
1348
default_format = branch.format_registry.get_default()
1349
except AttributeError:
1350
default_format = branch.BranchFormat._default_format
1351
from .remote import RemoteGitBranchFormat
1353
(default_format, LocalGitBranchFormat()),
1354
(default_format, RemoteGitBranchFormat())]
1357
def is_compatible(self, source, target):
1358
return (not isinstance(source, GitBranch) and
1359
isinstance(target, GitBranch))
1361
def _get_new_refs(self, stop_revision=None, fetch_tags=None,
1363
if not self.source.is_locked():
1364
raise errors.ObjectNotLocked(self.source)
1365
if stop_revision is None:
1366
(stop_revno, stop_revision) = self.source.last_revision_info()
1367
elif stop_revno is None:
1369
stop_revno = self.source.revision_id_to_revno(stop_revision)
1370
except errors.NoSuchRevision:
1372
if not isinstance(stop_revision, bytes):
1373
raise TypeError(stop_revision)
1374
main_ref = self.target.ref
1375
refs = {main_ref: (None, stop_revision)}
1376
if fetch_tags is None:
1377
c = self.source.get_config_stack()
1378
fetch_tags = c.get('branch.fetch_tags')
1379
for name, revid in viewitems(self.source.tags.get_tag_dict()):
1380
if self.source.repository.has_revision(revid):
1381
ref = tag_name_to_ref(name)
1382
if not check_ref_format(ref):
1383
warning("skipping tag with invalid characters %s (%s)",
1387
# FIXME: Skip tags that are not in the ancestry
1388
refs[ref] = (None, revid)
1389
return refs, main_ref, (stop_revno, stop_revision)
1391
def _update_refs(self, result, old_refs, new_refs, overwrite, tag_selector):
1392
mutter("updating refs. old refs: %r, new refs: %r",
1394
result.tag_updates = {}
1395
result.tag_conflicts = []
1398
def ref_equals(refs, ref, git_sha, revid):
1403
if (value[0] is not None and
1404
git_sha is not None and
1405
value[0] == git_sha):
1407
if (value[1] is not None and
1408
revid is not None and
1411
# FIXME: If one side only has the git sha available and the other
1412
# only has the bzr revid, then this will cause us to show a tag as
1413
# updated that hasn't actually been updated.
1415
# FIXME: Check for diverged branches
1416
for ref, (git_sha, revid) in viewitems(new_refs):
1417
if ref_equals(ret, ref, git_sha, revid):
1418
# Already up to date
1420
git_sha = old_refs[ref][0]
1422
revid = old_refs[ref][1]
1423
ret[ref] = new_refs[ref] = (git_sha, revid)
1424
elif ref not in ret or overwrite:
1426
tag_name = ref_to_tag_name(ref)
1430
if tag_selector and not tag_selector(tag_name):
1432
result.tag_updates[tag_name] = revid
1433
ret[ref] = (git_sha, revid)
1435
# FIXME: Check diverged
1439
name = ref_to_tag_name(ref)
1443
result.tag_conflicts.append(
1444
(name, revid, ret[name][1]))
1446
ret[ref] = (git_sha, revid)
1449
def fetch(self, stop_revision=None, fetch_tags=None, lossy=False,
1451
if stop_revision is None:
1452
stop_revision = self.source.last_revision()
1455
for k, v in viewitems(self.source.tags.get_tag_dict()):
1456
ret.append((None, v))
1457
ret.append((None, stop_revision))
1459
revidmap = self.interrepo.fetch_objects(ret, lossy=lossy, limit=limit)
1460
except NoPushSupport:
1461
raise errors.NoRoundtrippingSupport(self.source, self.target)
1462
return _mod_repository.FetchResult(revidmap={
1463
old_revid: new_revid
1464
for (old_revid, (new_sha, new_revid)) in revidmap.items()})
1466
def pull(self, overwrite=False, stop_revision=None, local=False,
1467
possible_transports=None, run_hooks=True, _stop_revno=None,
1469
result = GitBranchPullResult()
1470
result.source_branch = self.source
1471
result.target_branch = self.target
1472
with self.source.lock_read(), self.target.lock_write():
1473
new_refs, main_ref, stop_revinfo = self._get_new_refs(
1474
stop_revision, stop_revno=_stop_revno)
1476
def update_refs(old_refs):
1477
return self._update_refs(result, old_refs, new_refs, overwrite, tag_selector)
1479
result.revidmap, old_refs, new_refs = (
1480
self.interrepo.fetch_refs(update_refs, lossy=False))
1481
except NoPushSupport:
1482
raise errors.NoRoundtrippingSupport(self.source, self.target)
1483
(old_sha1, result.old_revid) = old_refs.get(
1484
main_ref, (ZERO_SHA, NULL_REVISION))
1485
if result.old_revid is None:
1486
result.old_revid = self.target.lookup_foreign_revision_id(
1488
result.new_revid = new_refs[main_ref][1]
1489
result.local_branch = None
1490
result.master_branch = self.target
1492
for hook in branch.Branch.hooks['post_pull']:
1496
def push(self, overwrite=False, stop_revision=None, lossy=False,
1497
_override_hook_source_branch=None, _stop_revno=None,
1499
result = GitBranchPushResult()
1500
result.source_branch = self.source
1501
result.target_branch = self.target
1502
result.local_branch = None
1503
result.master_branch = result.target_branch
1504
with self.source.lock_read(), self.target.lock_write():
1505
new_refs, main_ref, stop_revinfo = self._get_new_refs(
1506
stop_revision, stop_revno=_stop_revno)
1508
def update_refs(old_refs):
1509
return self._update_refs(result, old_refs, new_refs, overwrite, tag_selector)
1511
result.revidmap, old_refs, new_refs = (
1512
self.interrepo.fetch_refs(
1513
update_refs, lossy=lossy, overwrite=overwrite))
1514
except NoPushSupport:
1515
raise errors.NoRoundtrippingSupport(self.source, self.target)
1516
(old_sha1, result.old_revid) = old_refs.get(
1517
main_ref, (ZERO_SHA, NULL_REVISION))
1518
if lossy or result.old_revid is None:
1519
result.old_revid = self.target.lookup_foreign_revision_id(
1521
result.new_revid = new_refs[main_ref][1]
1522
(result.new_original_revno,
1523
result.new_original_revid) = stop_revinfo
1524
for hook in branch.Branch.hooks['post_push']:
1529
branch.InterBranch.register_optimiser(InterGitLocalGitBranch)
1530
branch.InterBranch.register_optimiser(InterFromGitBranch)
1531
branch.InterBranch.register_optimiser(InterToGitBranch)
1532
branch.InterBranch.register_optimiser(InterLocalGitRemoteGitBranch)