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 cStringIO import StringIO
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 ...revision import (
47
from ...trace import (
65
remote_refs_dict_to_tag_refs,
68
from .unpeel_map import (
72
from ...foreign import ForeignBranch
75
class GitPullResult(branch.PullResult):
76
"""Result of a pull from a Git branch."""
78
def _lookup_revno(self, revid):
79
if type(revid) is not str:
80
raise TypeError(revid)
81
# Try in source branch first, it'll be faster
82
with self.target_branch.lock_read():
83
return self.target_branch.revision_id_to_revno(revid)
87
return self._lookup_revno(self.old_revid)
91
return self._lookup_revno(self.new_revid)
94
class GitTags(tag.BasicTags):
95
"""Ref-based tag dictionary."""
97
def __init__(self, branch):
99
self.repository = branch.repository
101
def _merge_to_remote_git(self, target_repo, source_tag_refs, overwrite=False):
104
def get_changed_refs(old_refs):
106
for ref_name, tag_name, peeled, unpeeled in source_tag_refs.iteritems():
107
if old_refs.get(ref_name) == unpeeled:
109
elif overwrite or not ref_name in old_refs:
110
ret[ref_name] = unpeeled
111
updates[tag_name] = target_repo.lookup_foreign_revision_id(peeled)
115
self.repository.lookup_foreign_revision_id(peeled),
116
target_repo.lookup_foreign_revision_id(old_refs[ref_name])))
118
target_repo.controldir.send_pack(get_changed_refs, lambda have, want: [])
119
return updates, conflicts
121
def _merge_to_local_git(self, target_repo, source_tag_refs, overwrite=False):
124
for ref_name, tag_name, peeled, unpeeled in source_tag_refs:
125
if target_repo._git.refs.get(ref_name) == unpeeled:
127
elif overwrite or not ref_name in target_repo._git.refs:
128
target_repo._git.refs[ref_name] = unpeeled or peeled
129
updates[tag_name] = self.repository.lookup_foreign_revision_id(peeled)
131
source_revid = self.repository.lookup_foreign_revision_id(peeled)
133
target_revid = target_repo.lookup_foreign_revision_id(
134
target_repo._git.refs[ref_name])
136
trace.warning('%s does not point to a valid object',
139
conflicts.append((tag_name, source_revid, target_revid))
140
return updates, conflicts
142
def _merge_to_git(self, to_tags, source_tag_refs, overwrite=False):
143
target_repo = to_tags.repository
144
if self.repository.has_same_location(target_repo):
147
if getattr(target_repo, "_git", None):
148
return self._merge_to_local_git(target_repo, source_tag_refs, overwrite)
150
return self._merge_to_remote_git(target_repo, source_tag_refs, overwrite)
152
to_tags.branch._tag_refs = None
154
def _merge_to_non_git(self, to_tags, source_tag_refs, overwrite=False):
155
unpeeled_map = defaultdict(set)
158
result = dict(to_tags.get_tag_dict())
159
for ref_name, tag_name, peeled, unpeeled in source_tag_refs:
160
if unpeeled is not None:
161
unpeeled_map[peeled].add(unpeeled)
163
bzr_revid = self.branch.lookup_foreign_revision_id(peeled)
164
except NotCommitError:
166
if result.get(tag_name) == bzr_revid:
168
elif tag_name not in result or overwrite:
169
result[tag_name] = bzr_revid
170
updates[tag_name] = bzr_revid
172
conflicts.append((tag_name, bzr_revid, result[n]))
173
to_tags._set_tag_dict(result)
174
if len(unpeeled_map) > 0:
175
map_file = UnpeelMap.from_repository(to_tags.branch.repository)
176
map_file.update(unpeeled_map)
177
map_file.save_in_repository(to_tags.branch.repository)
178
return updates, conflicts
180
def merge_to(self, to_tags, overwrite=False, ignore_master=False,
181
source_tag_refs=None):
182
"""See Tags.merge_to."""
183
if source_tag_refs is None:
184
source_tag_refs = self.branch.get_tag_refs()
187
if isinstance(to_tags, GitTags):
188
return self._merge_to_git(to_tags, source_tag_refs,
194
master = to_tags.branch.get_master_branch()
195
if master is not None:
198
updates, conflicts = self._merge_to_non_git(to_tags, source_tag_refs,
200
if master is not None:
201
extra_updates, extra_conflicts = self.merge_to(
202
master.tags, overwrite=overwrite,
203
source_tag_refs=source_tag_refs,
204
ignore_master=ignore_master)
205
updates.update(extra_updates)
206
conflicts += extra_conflicts
207
return updates, conflicts
209
if master is not None:
212
def get_tag_dict(self):
214
for (ref_name, tag_name, peeled, unpeeled) in self.branch.get_tag_refs():
216
bzr_revid = self.branch.lookup_foreign_revision_id(peeled)
217
except NotCommitError:
220
ret[tag_name] = bzr_revid
224
class LocalGitTagDict(GitTags):
225
"""Dictionary with tags in a local repository."""
227
def __init__(self, branch):
228
super(LocalGitTagDict, self).__init__(branch)
229
self.refs = self.repository.controldir._git.refs
231
def _set_tag_dict(self, to_dict):
232
extra = set(self.refs.allkeys())
233
for k, revid in to_dict.iteritems():
234
name = tag_name_to_ref(k)
237
self.set_tag(k, revid)
240
del self.repository._git[name]
242
def set_tag(self, name, revid):
244
git_sha, mapping = self.branch.lookup_bzr_revision_id(revid)
245
except errors.NoSuchRevision:
246
raise errors.GhostTagsNotSupported(self)
247
self.refs[tag_name_to_ref(name)] = git_sha
248
self.branch._tag_refs = None
250
def delete_tag(self, name):
251
ref = tag_name_to_ref(name)
252
if not ref in self.refs:
253
raise errors.NoSuchTag(name)
255
self.branch._tag_refs = None
258
class GitBranchFormat(branch.BranchFormat):
260
def network_name(self):
263
def supports_tags(self):
266
def supports_leaving_lock(self):
269
def supports_tags_referencing_ghosts(self):
272
def tags_are_versioned(self):
275
def get_foreign_tests_branch_factory(self):
276
from .tests.test_branch import ForeignTestsBranchFactory
277
return ForeignTestsBranchFactory()
279
def make_tags(self, branch):
282
except AttributeError:
284
if getattr(branch.repository, "_git", None) is None:
285
from .remote import RemoteGitTagDict
286
return RemoteGitTagDict(branch)
288
return LocalGitTagDict(branch)
290
def initialize(self, a_controldir, name=None, repository=None,
291
append_revisions_only=None):
292
raise NotImplementedError(self.initialize)
294
def get_reference(self, controldir, name=None):
295
return controldir.get_branch_reference(name)
297
def set_reference(self, controldir, name, target):
298
return controldir.set_branch_reference(target, name)
301
class LocalGitBranchFormat(GitBranchFormat):
303
def get_format_description(self):
304
return 'Local Git Branch'
307
def _matchingcontroldir(self):
308
from .dir import LocalGitControlDirFormat
309
return LocalGitControlDirFormat()
311
def initialize(self, a_controldir, name=None, repository=None,
312
append_revisions_only=None):
313
from .dir import LocalGitDir
314
if not isinstance(a_controldir, LocalGitDir):
315
raise errors.IncompatibleFormat(self, a_controldir._format)
316
return a_controldir.create_branch(repository=repository, name=name,
317
append_revisions_only=append_revisions_only)
320
class GitBranch(ForeignBranch):
321
"""An adapter to git repositories for bzr Branch objects."""
324
def control_transport(self):
325
return self._control_transport
328
def user_transport(self):
329
return self._user_transport
331
def __init__(self, controldir, repository, ref, format):
332
self.repository = repository
333
self._format = format
334
self.controldir = controldir
335
self._lock_mode = None
337
super(GitBranch, self).__init__(repository.get_mapping())
340
self._user_transport = controldir.user_transport.clone('.')
341
self._control_transport = controldir.control_transport.clone('.')
342
self._tag_refs = None
345
self.name = ref_to_branch_name(ref)
348
if self.ref is not None:
349
params = {"ref": urlutils.escape(self.ref)}
352
params = {"branch": urlutils.escape(self.name)}
353
for k, v in params.items():
354
self._user_transport.set_segment_parameter(k, v)
355
self._control_transport.set_segment_parameter(k, v)
356
self.base = controldir.user_transport.base
358
def _get_checkout_format(self, lightweight=False):
359
"""Return the most suitable metadir for a checkout of this branch.
360
Weaves are used if this branch's repository uses weaves.
363
return controldir.format_registry.make_controldir("git")
365
return controldir.format_registry.make_controldir("default")
367
def get_child_submit_format(self):
368
"""Return the preferred format of submissions to this branch."""
369
ret = self.get_config_stack().get("child_submit_format")
374
def get_config(self):
375
return GitBranchConfig(self)
377
def get_config_stack(self):
378
return GitBranchStack(self)
380
def _get_nick(self, local=False, possible_master_transports=None):
381
"""Find the nick name for this branch.
385
cs = self.repository._git.get_config_stack()
387
return cs.get((b"branch", self.name.encode('utf-8')), b"nick").decode("utf-8")
390
return self.name or u"HEAD"
392
def _set_nick(self, nick):
393
cf = self.repository._git.get_config()
394
cf.set((b"branch", self.name.encode('utf-8')), b"nick", nick.encode("utf-8"))
397
self.repository._git._put_named_file('config', f.getvalue())
399
nick = property(_get_nick, _set_nick)
402
return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
405
def generate_revision_history(self, revid, last_rev=None, other_branch=None):
406
if last_rev is not None:
407
graph = self.repository.get_graph()
408
if not graph.is_ancestor(last_rev, revid):
409
# our previous tip is not merged into stop_revision
410
raise errors.DivergedBranches(self, other_branch)
412
self.set_last_revision(revid)
414
def lock_write(self, token=None):
415
if token is not None:
416
raise errors.TokenLockingNotSupported(self)
418
if self._lock_mode == 'r':
419
raise errors.ReadOnlyError(self)
420
self._lock_count += 1
423
self._lock_mode = 'w'
425
self.repository.lock_write()
426
return lock.LogicalLockResult(self.unlock)
428
def leave_lock_in_place(self):
429
raise NotImplementedError(self.leave_lock_in_place)
431
def dont_leave_lock_in_place(self):
432
raise NotImplementedError(self.dont_leave_lock_in_place)
434
def get_stacked_on_url(self):
435
# Git doesn't do stacking (yet...)
436
raise branch.UnstackableBranchFormat(self._format, self.base)
438
def _get_parent_location(self):
439
"""See Branch.get_parent()."""
440
# FIXME: Set "origin" url from .git/config ?
441
cs = self.repository._git.get_config_stack()
443
return cs.get((b"remote", b'origin'), b"url").decode("utf-8")
447
def set_parent(self, location):
448
# FIXME: Set "origin" url in .git/config ?
449
cs = self.repository._git.get_config()
450
location = urlutils.relative_url(self.base, location)
451
cs.set((b"remote", b"origin"), b"url", location)
454
self.repository._git._put_named_file('config', f.getvalue())
456
def break_lock(self):
457
raise NotImplementedError(self.break_lock)
461
if self._lock_mode not in ('r', 'w'):
462
raise ValueError(self._lock_mode)
463
self._lock_count += 1
465
self._lock_mode = 'r'
467
self.repository.lock_read()
468
return lock.LogicalLockResult(self.unlock)
470
def peek_lock_mode(self):
471
return self._lock_mode
474
return (self._lock_mode is not None)
479
def _unlock_ref(self):
483
"""See Branch.unlock()."""
484
if self._lock_count == 0:
485
raise errors.LockNotHeld(self)
487
self._lock_count -= 1
488
if self._lock_count == 0:
489
if self._lock_mode == 'w':
491
self._lock_mode = None
492
self._clear_cached_state()
494
self.repository.unlock()
496
def get_physical_lock_status(self):
499
def last_revision(self):
500
with self.lock_read():
501
# perhaps should escape this ?
502
if self.head is None:
503
return revision.NULL_REVISION
504
return self.lookup_foreign_revision_id(self.head)
506
def _basic_push(self, target, overwrite=False, stop_revision=None):
507
return branch.InterBranch.get(self, target)._basic_push(
508
overwrite, stop_revision)
510
def lookup_foreign_revision_id(self, foreign_revid):
512
return self.repository.lookup_foreign_revision_id(foreign_revid,
516
return self.mapping.revision_id_foreign_to_bzr(foreign_revid)
518
def lookup_bzr_revision_id(self, revid):
519
return self.repository.lookup_bzr_revision_id(
520
revid, mapping=self.mapping)
522
def get_unshelver(self, tree):
523
raise errors.StoringUncommittedNotSupported(self)
525
def _clear_cached_state(self):
526
super(GitBranch, self)._clear_cached_state()
527
self._tag_refs = None
529
def _iter_tag_refs(self, refs):
530
"""Iterate over the tag refs.
532
:param refs: Refs dictionary (name -> git sha1)
533
:return: iterator over (ref_name, tag_name, peeled_sha1, unpeeled_sha1)
535
raise NotImplementedError(self._iter_tag_refs)
537
def get_tag_refs(self):
538
with self.lock_read():
539
if self._tag_refs is None:
540
self._tag_refs = list(self._iter_tag_refs())
541
return self._tag_refs
544
class LocalGitBranch(GitBranch):
545
"""A local Git branch."""
547
def __init__(self, controldir, repository, ref):
548
super(LocalGitBranch, self).__init__(controldir, repository, ref,
549
LocalGitBranchFormat())
551
def create_checkout(self, to_location, revision_id=None, lightweight=False,
552
accelerator_tree=None, hardlink=False):
553
t = transport.get_transport(to_location)
555
format = self._get_checkout_format(lightweight=lightweight)
556
checkout = format.initialize_on_transport(t)
558
from_branch = checkout.set_branch_reference(target_branch=self)
560
policy = checkout.determine_repository_policy()
561
repo = policy.acquire_repository()[0]
563
checkout_branch = checkout.create_branch()
564
checkout_branch.bind(self)
565
checkout_branch.pull(self, stop_revision=revision_id)
567
return checkout.create_workingtree(revision_id,
568
from_branch=from_branch, hardlink=hardlink)
571
self._ref_lock = self.repository._git.refs.lock_ref(self.ref)
573
def _unlock_ref(self):
574
self._ref_lock.unlock()
576
def fetch(self, from_branch, last_revision=None, limit=None):
577
return branch.InterBranch.get(from_branch, self).fetch(
578
stop_revision=last_revision, limit=limit)
580
def _gen_revision_history(self):
581
if self.head is None:
583
graph = self.repository.get_graph()
584
ret = list(graph.iter_lefthand_ancestry(self.last_revision(),
585
(revision.NULL_REVISION, )))
591
return self.repository._git.refs[self.ref]
595
def _read_last_revision_info(self):
596
last_revid = self.last_revision()
597
graph = self.repository.get_graph()
598
revno = graph.find_distance_to_null(last_revid,
599
[(revision.NULL_REVISION, 0)])
600
return revno, last_revid
602
def set_last_revision_info(self, revno, revision_id):
603
self.set_last_revision(revision_id)
604
self._last_revision_info_cache = revno, revision_id
606
def set_last_revision(self, revid):
607
if not revid or not isinstance(revid, basestring):
608
raise errors.InvalidRevisionId(revision_id=revid, branch=self)
609
if revid == NULL_REVISION:
612
(newhead, self.mapping) = self.repository.lookup_bzr_revision_id(revid)
613
if self.mapping is None:
615
self._set_head(newhead)
617
def _set_head(self, value):
618
if value == ZERO_SHA:
619
raise ValueError(value)
622
del self.repository._git.refs[self.ref]
624
self.repository._git.refs[self.ref] = self._head
625
self._clear_cached_state()
627
head = property(_get_head, _set_head)
629
def get_push_location(self):
630
"""See Branch.get_push_location."""
631
push_loc = self.get_config_stack().get('push_location')
634
def set_push_location(self, location):
635
"""See Branch.set_push_location."""
636
self.get_config().set_user_option('push_location', location,
637
store=config.STORE_LOCATION)
639
def supports_tags(self):
642
def store_uncommitted(self, creator):
643
raise errors.StoringUncommittedNotSupported(self)
645
def _iter_tag_refs(self):
646
"""Iterate over the tag refs.
648
:param refs: Refs dictionary (name -> git sha1)
649
:return: iterator over (ref_name, tag_name, peeled_sha1, unpeeled_sha1)
651
refs = self.repository._git.refs
652
for ref_name, unpeeled in refs.as_dict().iteritems():
654
tag_name = ref_to_tag_name(ref_name)
655
except (ValueError, UnicodeDecodeError):
657
peeled = refs.get_peeled(ref_name)
660
if type(tag_name) is not unicode:
661
raise TypeError(tag_name)
662
yield (ref_name, tag_name, peeled, unpeeled)
664
def create_memorytree(self):
665
from .memorytree import GitMemoryTree
666
return GitMemoryTree(self, self.repository._git.object_store, self.head)
669
def _quick_lookup_revno(local_branch, remote_branch, revid):
670
if type(revid) is not str:
671
raise TypeError(revid)
672
# Try in source branch first, it'll be faster
673
with local_branch.lock_read():
675
return local_branch.revision_id_to_revno(revid)
676
except errors.NoSuchRevision:
677
graph = local_branch.repository.get_graph()
679
return graph.find_distance_to_null(revid,
680
[(revision.NULL_REVISION, 0)])
681
except errors.GhostRevisionsHaveNoRevno:
682
# FIXME: Check using graph.find_distance_to_null() ?
683
with remote_branch.lock_read():
684
return remote_branch.revision_id_to_revno(revid)
687
class GitBranchPullResult(branch.PullResult):
690
super(GitBranchPullResult, self).__init__()
691
self.new_git_head = None
692
self._old_revno = None
693
self._new_revno = None
695
def report(self, to_file):
697
if self.old_revid == self.new_revid:
698
to_file.write('No revisions to pull.\n')
699
elif self.new_git_head is not None:
700
to_file.write('Now on revision %d (git sha: %s).\n' %
701
(self.new_revno, self.new_git_head))
703
to_file.write('Now on revision %d.\n' % (self.new_revno,))
704
self._show_tag_conficts(to_file)
706
def _lookup_revno(self, revid):
707
return _quick_lookup_revno(self.target_branch, self.source_branch,
710
def _get_old_revno(self):
711
if self._old_revno is not None:
712
return self._old_revno
713
return self._lookup_revno(self.old_revid)
715
def _set_old_revno(self, revno):
716
self._old_revno = revno
718
old_revno = property(_get_old_revno, _set_old_revno)
720
def _get_new_revno(self):
721
if self._new_revno is not None:
722
return self._new_revno
723
return self._lookup_revno(self.new_revid)
725
def _set_new_revno(self, revno):
726
self._new_revno = revno
728
new_revno = property(_get_new_revno, _set_new_revno)
731
class GitBranchPushResult(branch.BranchPushResult):
733
def _lookup_revno(self, revid):
734
return _quick_lookup_revno(self.source_branch, self.target_branch,
739
return self._lookup_revno(self.old_revid)
743
new_original_revno = getattr(self, "new_original_revno", None)
744
if new_original_revno:
745
return new_original_revno
746
if getattr(self, "new_original_revid", None) is not None:
747
return self._lookup_revno(self.new_original_revid)
748
return self._lookup_revno(self.new_revid)
751
class InterFromGitBranch(branch.GenericInterBranch):
752
"""InterBranch implementation that pulls from Git into bzr."""
755
def _get_branch_formats_to_test():
757
default_format = branch.format_registry.get_default()
758
except AttributeError:
759
default_format = branch.BranchFormat._default_format
760
from .remote import RemoteGitBranchFormat
762
(RemoteGitBranchFormat(), default_format),
763
(LocalGitBranchFormat(), default_format)]
766
def _get_interrepo(self, source, target):
767
return _mod_repository.InterRepository.get(source.repository, target.repository)
770
def is_compatible(cls, source, target):
771
if not isinstance(source, GitBranch):
773
if isinstance(target, GitBranch):
774
# InterLocalGitRemoteGitBranch or InterToGitBranch should be used
776
if getattr(cls._get_interrepo(source, target), "fetch_objects", None) is None:
777
# fetch_objects is necessary for this to work
781
def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
782
self.fetch_objects(stop_revision, fetch_tags=fetch_tags, limit=limit)
784
def fetch_objects(self, stop_revision, fetch_tags, limit=None):
785
interrepo = self._get_interrepo(self.source, self.target)
786
if fetch_tags is None:
787
c = self.source.get_config_stack()
788
fetch_tags = c.get('branch.fetch_tags')
789
def determine_wants(heads):
790
if stop_revision is None:
792
head = heads[self.source.ref]
794
self._last_revid = revision.NULL_REVISION
796
self._last_revid = self.source.lookup_foreign_revision_id(head)
798
self._last_revid = stop_revision
799
real = interrepo.get_determine_wants_revids(
800
[self._last_revid], include_tags=fetch_tags)
802
pack_hint, head, refs = interrepo.fetch_objects(
803
determine_wants, self.source.mapping, limit=limit)
804
if (pack_hint is not None and
805
self.target.repository._format.pack_compresses):
806
self.target.repository.pack(hint=pack_hint)
809
def _update_revisions(self, stop_revision=None, overwrite=False):
810
head, refs = self.fetch_objects(stop_revision, fetch_tags=None)
812
prev_last_revid = None
814
prev_last_revid = self.target.last_revision()
815
self.target.generate_revision_history(self._last_revid,
816
last_rev=prev_last_revid, other_branch=self.source)
819
def _basic_pull(self, stop_revision, overwrite, run_hooks,
820
_override_hook_target, _hook_master):
821
if overwrite is True:
822
overwrite = set(["history", "tags"])
825
result = GitBranchPullResult()
826
result.source_branch = self.source
827
if _override_hook_target is None:
828
result.target_branch = self.target
830
result.target_branch = _override_hook_target
831
with self.target.lock_write(), self.source.lock_read():
832
# We assume that during 'pull' the target repository is closer than
834
(result.old_revno, result.old_revid) = \
835
self.target.last_revision_info()
836
result.new_git_head, remote_refs = self._update_revisions(
837
stop_revision, overwrite=("history" in overwrite))
838
tags_ret = self.source.tags.merge_to(
839
self.target.tags, ("tags" in overwrite), ignore_master=True)
840
if isinstance(tags_ret, tuple):
841
result.tag_updates, result.tag_conflicts = tags_ret
843
result.tag_conflicts = tags_ret
844
(result.new_revno, result.new_revid) = \
845
self.target.last_revision_info()
847
result.master_branch = _hook_master
848
result.local_branch = result.target_branch
850
result.master_branch = result.target_branch
851
result.local_branch = None
853
for hook in branch.Branch.hooks['post_pull']:
857
def pull(self, overwrite=False, stop_revision=None,
858
possible_transports=None, _hook_master=None, run_hooks=True,
859
_override_hook_target=None, local=False):
862
:param _hook_master: Private parameter - set the branch to
863
be supplied as the master to pull hooks.
864
:param run_hooks: Private parameter - if false, this branch
865
is being called because it's the master of the primary branch,
866
so it should not run its hooks.
867
:param _override_hook_target: Private parameter - set the branch to be
868
supplied as the target_branch to pull hooks.
870
# This type of branch can't be bound.
871
bound_location = self.target.get_bound_location()
872
if local and not bound_location:
873
raise errors.LocalRequiresBoundBranch()
875
source_is_master = False
876
self.source.lock_read()
878
# bound_location comes from a config file, some care has to be
879
# taken to relate it to source.user_url
880
normalized = urlutils.normalize_url(bound_location)
882
relpath = self.source.user_transport.relpath(normalized)
883
source_is_master = (relpath == '')
884
except (errors.PathNotChild, urlutils.InvalidURL):
885
source_is_master = False
886
if not local and bound_location and not source_is_master:
887
# not pulling from master, so we need to update master.
888
master_branch = self.target.get_master_branch(possible_transports)
889
master_branch.lock_write()
893
# pull from source into master.
894
master_branch.pull(self.source, overwrite, stop_revision,
896
result = self._basic_pull(stop_revision, overwrite, run_hooks,
897
_override_hook_target, _hook_master=master_branch)
902
master_branch.unlock()
905
def _basic_push(self, overwrite, stop_revision):
906
if overwrite is True:
907
overwrite = set(["history", "tags"])
910
result = branch.BranchPushResult()
911
result.source_branch = self.source
912
result.target_branch = self.target
913
result.old_revno, result.old_revid = self.target.last_revision_info()
914
result.new_git_head, remote_refs = self._update_revisions(
915
stop_revision, overwrite=("history" in overwrite))
916
tags_ret = self.source.tags.merge_to(self.target.tags,
917
"tags" in overwrite, ignore_master=True)
918
(result.tag_updates, result.tag_conflicts) = tags_ret
919
result.new_revno, result.new_revid = self.target.last_revision_info()
923
class InterGitBranch(branch.GenericInterBranch):
924
"""InterBranch implementation that pulls between Git branches."""
926
def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
927
raise NotImplementedError(self.fetch)
930
class InterLocalGitRemoteGitBranch(InterGitBranch):
931
"""InterBranch that copies from a local to a remote git branch."""
934
def _get_branch_formats_to_test():
935
from .remote import RemoteGitBranchFormat
937
(LocalGitBranchFormat(), RemoteGitBranchFormat())]
940
def is_compatible(self, source, target):
941
from .remote import RemoteGitBranch
942
return (isinstance(source, LocalGitBranch) and
943
isinstance(target, RemoteGitBranch))
945
def _basic_push(self, overwrite, stop_revision):
946
# TODO(jelmer): Support overwrite
947
result = GitBranchPushResult()
948
result.source_branch = self.source
949
result.target_branch = self.target
950
if stop_revision is None:
951
stop_revision = self.source.last_revision()
952
# TODO(jelmer): Check for diverged branches
953
def get_changed_refs(old_refs):
954
old_ref = old_refs.get(self.target.ref, ZERO_SHA)
955
result.old_revid = self.target.lookup_foreign_revision_id(old_ref)
956
refs = { self.target.ref: self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
957
result.new_revid = stop_revision
958
for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
959
refs[tag_name_to_ref(name)] = sha
961
self.target.repository.send_pack(get_changed_refs,
962
self.source.repository._git.object_store.generate_pack_data)
966
class InterGitLocalGitBranch(InterGitBranch):
967
"""InterBranch that copies from a remote to a local git branch."""
970
def _get_branch_formats_to_test():
971
from .remote import RemoteGitBranchFormat
973
(RemoteGitBranchFormat(), LocalGitBranchFormat()),
974
(LocalGitBranchFormat(), LocalGitBranchFormat())]
977
def is_compatible(self, source, target):
978
return (isinstance(source, GitBranch) and
979
isinstance(target, LocalGitBranch))
981
def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
982
interrepo = _mod_repository.InterRepository.get(self.source.repository,
983
self.target.repository)
984
if stop_revision is None:
985
stop_revision = self.source.last_revision()
986
determine_wants = interrepo.get_determine_wants_revids(
987
[stop_revision], include_tags=fetch_tags)
988
interrepo.fetch_objects(determine_wants, limit=limit)
990
def _basic_push(self, overwrite=False, stop_revision=None):
991
if overwrite is True:
992
overwrite = set(["history", "tags"])
995
result = GitBranchPushResult()
996
result.source_branch = self.source
997
result.target_branch = self.target
998
result.old_revid = self.target.last_revision()
999
refs, stop_revision = self.update_refs(stop_revision)
1000
self.target.generate_revision_history(stop_revision,
1001
(result.old_revid if ("history" not in overwrite) else None),
1002
other_branch=self.source)
1003
tags_ret = self.source.tags.merge_to(self.target.tags,
1004
source_tag_refs=remote_refs_dict_to_tag_refs(refs),
1005
overwrite=("tags" in overwrite))
1006
if isinstance(tags_ret, tuple):
1007
(result.tag_updates, result.tag_conflicts) = tags_ret
1009
result.tag_conflicts = tags_ret
1010
result.new_revid = self.target.last_revision()
1013
def update_refs(self, stop_revision=None):
1014
interrepo = _mod_repository.InterRepository.get(
1015
self.source.repository, self.target.repository)
1016
c = self.source.get_config_stack()
1017
fetch_tags = c.get('branch.fetch_tags')
1019
if stop_revision is None:
1020
refs = interrepo.fetch(branches=["HEAD"], include_tags=fetch_tags)
1024
stop_revision = revision.NULL_REVISION
1026
stop_revision = self.target.lookup_foreign_revision_id(head)
1028
refs = interrepo.fetch(revision_id=stop_revision, include_tags=fetch_tags)
1029
return refs, stop_revision
1031
def pull(self, stop_revision=None, overwrite=False,
1032
possible_transports=None, run_hooks=True, local=False):
1033
# This type of branch can't be bound.
1035
raise errors.LocalRequiresBoundBranch()
1036
if overwrite is True:
1037
overwrite = set(["history", "tags"])
1041
result = GitPullResult()
1042
result.source_branch = self.source
1043
result.target_branch = self.target
1044
with self.target.lock_write(), self.source.lock_read():
1045
result.old_revid = self.target.last_revision()
1046
refs, stop_revision = self.update_refs(stop_revision)
1047
self.target.generate_revision_history(stop_revision,
1048
(result.old_revid if ("history" not in overwrite) else None),
1049
other_branch=self.source)
1050
tags_ret = self.source.tags.merge_to(self.target.tags,
1051
overwrite=("tags" in overwrite),
1052
source_tag_refs=remote_refs_dict_to_tag_refs(refs))
1053
if isinstance(tags_ret, tuple):
1054
(result.tag_updates, result.tag_conflicts) = tags_ret
1056
result.tag_conflicts = tags_ret
1057
result.new_revid = self.target.last_revision()
1058
result.local_branch = None
1059
result.master_branch = result.target_branch
1061
for hook in branch.Branch.hooks['post_pull']:
1066
class InterToGitBranch(branch.GenericInterBranch):
1067
"""InterBranch implementation that pulls from a non-bzr into a Git branch."""
1069
def __init__(self, source, target):
1070
super(InterToGitBranch, self).__init__(source, target)
1071
self.interrepo = _mod_repository.InterRepository.get(source.repository,
1075
def _get_branch_formats_to_test():
1077
default_format = branch.format_registry.get_default()
1078
except AttributeError:
1079
default_format = branch.BranchFormat._default_format
1080
from .remote import RemoteGitBranchFormat
1082
(default_format, LocalGitBranchFormat()),
1083
(default_format, RemoteGitBranchFormat())]
1086
def is_compatible(self, source, target):
1087
return (not isinstance(source, GitBranch) and
1088
isinstance(target, GitBranch))
1090
def _get_new_refs(self, stop_revision=None, fetch_tags=None):
1091
if not self.source.is_locked():
1092
raise errors.ObjectNotLocked(self.source)
1093
if stop_revision is None:
1094
(stop_revno, stop_revision) = self.source.last_revision_info()
1096
stop_revno = self.source.revision_id_to_revno(stop_revision)
1097
if type(stop_revision) is not str:
1098
raise TypeError(stop_revision)
1099
main_ref = self.target.ref
1100
refs = { main_ref: (None, stop_revision) }
1101
if fetch_tags is None:
1102
c = self.source.get_config_stack()
1103
fetch_tags = c.get('branch.fetch_tags')
1104
for name, revid in self.source.tags.get_tag_dict().iteritems():
1105
if self.source.repository.has_revision(revid):
1106
ref = tag_name_to_ref(name)
1107
if not check_ref_format(ref):
1108
warning("skipping tag with invalid characters %s (%s)",
1112
# FIXME: Skip tags that are not in the ancestry
1113
refs[ref] = (None, revid)
1114
return refs, main_ref, (stop_revno, stop_revision)
1116
def _update_refs(self, result, old_refs, new_refs, overwrite):
1117
mutter("updating refs. old refs: %r, new refs: %r",
1119
result.tag_updates = {}
1120
result.tag_conflicts = []
1121
ret = dict(old_refs)
1122
def ref_equals(refs, ref, git_sha, revid):
1127
if (value[0] is not None and
1128
git_sha is not None and
1129
value[0] == git_sha):
1131
if (value[1] is not None and
1132
revid is not None and
1135
# FIXME: If one side only has the git sha available and the other only
1136
# has the bzr revid, then this will cause us to show a tag as updated
1137
# that hasn't actually been updated.
1139
# FIXME: Check for diverged branches
1140
for ref, (git_sha, revid) in new_refs.iteritems():
1141
if ref_equals(ret, ref, git_sha, revid):
1142
# Already up to date
1144
git_sha = old_refs[ref][0]
1146
revid = old_refs[ref][1]
1147
ret[ref] = new_refs[ref] = (git_sha, revid)
1148
elif ref not in ret or overwrite:
1150
tag_name = ref_to_tag_name(ref)
1154
result.tag_updates[tag_name] = revid
1155
ret[ref] = (git_sha, revid)
1157
# FIXME: Check diverged
1161
name = ref_to_tag_name(ref)
1165
result.tag_conflicts.append((name, revid, ret[name][1]))
1167
ret[ref] = (git_sha, revid)
1170
def fetch(self, stop_revision=None, fetch_tags=None, lossy=False, limit=None):
1171
if stop_revision is None:
1172
stop_revision = self.source.last_revision()
1175
for k, v in self.source.tags.get_tag_dict().iteritems():
1176
ret.append((None, v))
1177
ret.append((None, stop_revision))
1179
self.interrepo.fetch_objects(ret, lossy=lossy, limit=limit)
1180
except NoPushSupport:
1181
raise errors.NoRoundtrippingSupport(self.source, self.target)
1183
def pull(self, overwrite=False, stop_revision=None, local=False,
1184
possible_transports=None, run_hooks=True):
1185
result = GitBranchPullResult()
1186
result.source_branch = self.source
1187
result.target_branch = self.target
1188
with self.source.lock_read(), self.target.lock_write():
1189
new_refs, main_ref, stop_revinfo = self._get_new_refs(
1191
def update_refs(old_refs):
1192
return self._update_refs(result, old_refs, new_refs, overwrite)
1194
result.revidmap, old_refs, new_refs = self.interrepo.fetch_refs(
1195
update_refs, lossy=False)
1196
except NoPushSupport:
1197
raise errors.NoRoundtrippingSupport(self.source, self.target)
1198
(old_sha1, result.old_revid) = old_refs.get(main_ref, (ZERO_SHA, NULL_REVISION))
1199
if result.old_revid is None:
1200
result.old_revid = self.target.lookup_foreign_revision_id(old_sha1)
1201
result.new_revid = new_refs[main_ref][1]
1202
result.local_branch = None
1203
result.master_branch = self.target
1205
for hook in branch.Branch.hooks['post_pull']:
1209
def push(self, overwrite=False, stop_revision=None, lossy=False,
1210
_override_hook_source_branch=None):
1211
result = GitBranchPushResult()
1212
result.source_branch = self.source
1213
result.target_branch = self.target
1214
result.local_branch = None
1215
result.master_branch = result.target_branch
1216
with self.source.lock_read(), self.target.lock_write():
1217
new_refs, main_ref, stop_revinfo = self._get_new_refs(stop_revision)
1218
def update_refs(old_refs):
1219
return self._update_refs(result, old_refs, new_refs, overwrite)
1221
result.revidmap, old_refs, new_refs = self.interrepo.fetch_refs(
1222
update_refs, lossy=lossy)
1223
except NoPushSupport:
1224
raise errors.NoRoundtrippingSupport(self.source, self.target)
1225
(old_sha1, result.old_revid) = old_refs.get(main_ref, (ZERO_SHA, NULL_REVISION))
1226
if result.old_revid is None:
1227
result.old_revid = self.target.lookup_foreign_revision_id(old_sha1)
1228
result.new_revid = new_refs[main_ref][1]
1229
(result.new_original_revno, result.new_original_revid) = stop_revinfo
1230
for hook in branch.Branch.hooks['post_push']:
1235
branch.InterBranch.register_optimiser(InterGitLocalGitBranch)
1236
branch.InterBranch.register_optimiser(InterFromGitBranch)
1237
branch.InterBranch.register_optimiser(InterToGitBranch)
1238
branch.InterBranch.register_optimiser(InterLocalGitRemoteGitBranch)