1
# Copyright (C) 2007,2012 Canonical Ltd
2
# Copyright (C) 2009-2018 Jelmer Vernooij <jelmer@jelmer.uk>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
"""An adapter between a Git Branch and a Bazaar Branch"""
20
from __future__ import absolute_import
22
from io import BytesIO
23
from collections import defaultdict
25
from dulwich.objects import (
29
from dulwich.repo import check_ref_format
37
repository as _mod_repository,
44
from ...revision import (
47
from ...trace import (
69
remote_refs_dict_to_tag_refs,
72
from .unpeel_map import (
75
from .urls import git_url_to_bzr_url
77
from ...foreign import ForeignBranch
80
class GitPullResult(branch.PullResult):
81
"""Result of a pull from a Git branch."""
83
def _lookup_revno(self, revid):
84
if type(revid) is not str:
85
raise TypeError(revid)
86
# Try in source branch first, it'll be faster
87
with self.target_branch.lock_read():
88
return self.target_branch.revision_id_to_revno(revid)
92
return self._lookup_revno(self.old_revid)
96
return self._lookup_revno(self.new_revid)
99
class GitTags(tag.BasicTags):
100
"""Ref-based tag dictionary."""
102
def __init__(self, branch):
104
self.repository = branch.repository
106
def _merge_to_remote_git(self, target_repo, source_tag_refs, overwrite=False):
109
def get_changed_refs(old_refs):
111
for ref_name, tag_name, peeled, unpeeled in source_tag_refs.iteritems():
112
if old_refs.get(ref_name) == unpeeled:
114
elif overwrite or not ref_name in old_refs:
115
ret[ref_name] = unpeeled
116
updates[tag_name] = target_repo.lookup_foreign_revision_id(peeled)
120
self.repository.lookup_foreign_revision_id(peeled),
121
target_repo.lookup_foreign_revision_id(old_refs[ref_name])))
123
target_repo.controldir.send_pack(get_changed_refs, lambda have, want: [])
124
return updates, conflicts
126
def _merge_to_local_git(self, target_repo, source_tag_refs, overwrite=False):
129
for ref_name, tag_name, peeled, unpeeled in source_tag_refs:
130
if target_repo._git.refs.get(ref_name) == unpeeled:
132
elif overwrite or not ref_name in target_repo._git.refs:
133
target_repo._git.refs[ref_name] = unpeeled or peeled
134
updates[tag_name] = self.repository.lookup_foreign_revision_id(peeled)
136
source_revid = self.repository.lookup_foreign_revision_id(peeled)
138
target_revid = target_repo.lookup_foreign_revision_id(
139
target_repo._git.refs[ref_name])
141
trace.warning('%s does not point to a valid object',
144
conflicts.append((tag_name, source_revid, target_revid))
145
return updates, conflicts
147
def _merge_to_git(self, to_tags, source_tag_refs, overwrite=False):
148
target_repo = to_tags.repository
149
if self.repository.has_same_location(target_repo):
152
if getattr(target_repo, "_git", None):
153
return self._merge_to_local_git(target_repo, source_tag_refs, overwrite)
155
return self._merge_to_remote_git(target_repo, source_tag_refs, overwrite)
157
to_tags.branch._tag_refs = None
159
def _merge_to_non_git(self, to_tags, source_tag_refs, overwrite=False):
160
unpeeled_map = defaultdict(set)
163
result = dict(to_tags.get_tag_dict())
164
for ref_name, tag_name, peeled, unpeeled in source_tag_refs:
165
if unpeeled is not None:
166
unpeeled_map[peeled].add(unpeeled)
168
bzr_revid = self.branch.lookup_foreign_revision_id(peeled)
169
except NotCommitError:
171
if result.get(tag_name) == bzr_revid:
173
elif tag_name not in result or overwrite:
174
result[tag_name] = bzr_revid
175
updates[tag_name] = bzr_revid
177
conflicts.append((tag_name, bzr_revid, result[n]))
178
to_tags._set_tag_dict(result)
179
if len(unpeeled_map) > 0:
180
map_file = UnpeelMap.from_repository(to_tags.branch.repository)
181
map_file.update(unpeeled_map)
182
map_file.save_in_repository(to_tags.branch.repository)
183
return updates, conflicts
185
def merge_to(self, to_tags, overwrite=False, ignore_master=False,
186
source_tag_refs=None):
187
"""See Tags.merge_to."""
188
if source_tag_refs is None:
189
source_tag_refs = self.branch.get_tag_refs()
192
if isinstance(to_tags, GitTags):
193
return self._merge_to_git(to_tags, source_tag_refs,
199
master = to_tags.branch.get_master_branch()
200
if master is not None:
203
updates, conflicts = self._merge_to_non_git(to_tags, source_tag_refs,
205
if master is not None:
206
extra_updates, extra_conflicts = self.merge_to(
207
master.tags, overwrite=overwrite,
208
source_tag_refs=source_tag_refs,
209
ignore_master=ignore_master)
210
updates.update(extra_updates)
211
conflicts += extra_conflicts
212
return updates, conflicts
214
if master is not None:
217
def get_tag_dict(self):
219
for (ref_name, tag_name, peeled, unpeeled) in self.branch.get_tag_refs():
221
bzr_revid = self.branch.lookup_foreign_revision_id(peeled)
222
except NotCommitError:
225
ret[tag_name] = bzr_revid
229
class LocalGitTagDict(GitTags):
230
"""Dictionary with tags in a local repository."""
232
def __init__(self, branch):
233
super(LocalGitTagDict, self).__init__(branch)
234
self.refs = self.repository.controldir._git.refs
236
def _set_tag_dict(self, to_dict):
237
extra = set(self.refs.allkeys())
238
for k, revid in to_dict.iteritems():
239
name = tag_name_to_ref(k)
242
self.set_tag(k, revid)
245
del self.repository._git[name]
247
def set_tag(self, name, revid):
249
git_sha, mapping = self.branch.lookup_bzr_revision_id(revid)
250
except errors.NoSuchRevision:
251
raise errors.GhostTagsNotSupported(self)
252
self.refs[tag_name_to_ref(name)] = git_sha
253
self.branch._tag_refs = None
255
def delete_tag(self, name):
256
ref = tag_name_to_ref(name)
257
if not ref in self.refs:
258
raise errors.NoSuchTag(name)
260
self.branch._tag_refs = None
263
class GitBranchFormat(branch.BranchFormat):
265
def network_name(self):
268
def supports_tags(self):
271
def supports_leaving_lock(self):
274
def supports_tags_referencing_ghosts(self):
277
def tags_are_versioned(self):
280
def get_foreign_tests_branch_factory(self):
281
from .tests.test_branch import ForeignTestsBranchFactory
282
return ForeignTestsBranchFactory()
284
def make_tags(self, branch):
287
except AttributeError:
289
if getattr(branch.repository, "_git", None) is None:
290
from .remote import RemoteGitTagDict
291
return RemoteGitTagDict(branch)
293
return LocalGitTagDict(branch)
295
def initialize(self, a_controldir, name=None, repository=None,
296
append_revisions_only=None):
297
raise NotImplementedError(self.initialize)
299
def get_reference(self, controldir, name=None):
300
return controldir.get_branch_reference(name)
302
def set_reference(self, controldir, name, target):
303
return controldir.set_branch_reference(target, name)
306
class LocalGitBranchFormat(GitBranchFormat):
308
def get_format_description(self):
309
return 'Local Git Branch'
312
def _matchingcontroldir(self):
313
from .dir import LocalGitControlDirFormat
314
return LocalGitControlDirFormat()
316
def initialize(self, a_controldir, name=None, repository=None,
317
append_revisions_only=None):
318
from .dir import LocalGitDir
319
if not isinstance(a_controldir, LocalGitDir):
320
raise errors.IncompatibleFormat(self, a_controldir._format)
321
return a_controldir.create_branch(repository=repository, name=name,
322
append_revisions_only=append_revisions_only)
325
class GitBranch(ForeignBranch):
326
"""An adapter to git repositories for bzr Branch objects."""
329
def control_transport(self):
330
return self._control_transport
333
def user_transport(self):
334
return self._user_transport
336
def __init__(self, controldir, repository, ref, format):
337
self.repository = repository
338
self._format = format
339
self.controldir = controldir
340
self._lock_mode = None
342
super(GitBranch, self).__init__(repository.get_mapping())
345
self._user_transport = controldir.user_transport.clone('.')
346
self._control_transport = controldir.control_transport.clone('.')
347
self._tag_refs = None
350
self.name = ref_to_branch_name(ref)
353
if self.ref is not None:
354
params = {"ref": urlutils.escape(self.ref)}
357
params = {"branch": urlutils.escape(self.name)}
358
for k, v in params.items():
359
self._user_transport.set_segment_parameter(k, v)
360
self._control_transport.set_segment_parameter(k, v)
361
self.base = controldir.user_transport.base
363
def _get_checkout_format(self, lightweight=False):
364
"""Return the most suitable metadir for a checkout of this branch.
365
Weaves are used if this branch's repository uses weaves.
368
return controldir.format_registry.make_controldir("git")
370
return controldir.format_registry.make_controldir("default")
372
def get_child_submit_format(self):
373
"""Return the preferred format of submissions to this branch."""
374
ret = self.get_config_stack().get("child_submit_format")
379
def get_config(self):
380
return GitBranchConfig(self)
382
def get_config_stack(self):
383
return GitBranchStack(self)
385
def _get_nick(self, local=False, possible_master_transports=None):
386
"""Find the nick name for this branch.
390
cs = self.repository._git.get_config_stack()
392
return cs.get((b"branch", self.name.encode('utf-8')), b"nick").decode("utf-8")
395
return self.name or u"HEAD"
397
def _set_nick(self, nick):
398
cf = self.repository._git.get_config()
399
cf.set((b"branch", self.name.encode('utf-8')), b"nick", nick.encode("utf-8"))
402
self.repository._git._put_named_file('config', f.getvalue())
404
nick = property(_get_nick, _set_nick)
407
return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
410
def generate_revision_history(self, revid, last_rev=None, other_branch=None):
411
if last_rev is not None:
412
graph = self.repository.get_graph()
413
if not graph.is_ancestor(last_rev, revid):
414
# our previous tip is not merged into stop_revision
415
raise errors.DivergedBranches(self, other_branch)
417
self.set_last_revision(revid)
419
def lock_write(self, token=None):
420
if token is not None:
421
raise errors.TokenLockingNotSupported(self)
423
if self._lock_mode == 'r':
424
raise errors.ReadOnlyError(self)
425
self._lock_count += 1
428
self._lock_mode = 'w'
430
self.repository.lock_write()
431
return lock.LogicalLockResult(self.unlock)
433
def leave_lock_in_place(self):
434
raise NotImplementedError(self.leave_lock_in_place)
436
def dont_leave_lock_in_place(self):
437
raise NotImplementedError(self.dont_leave_lock_in_place)
439
def get_stacked_on_url(self):
440
# Git doesn't do stacking (yet...)
441
raise branch.UnstackableBranchFormat(self._format, self.base)
443
def _get_parent_location(self):
444
"""See Branch.get_parent()."""
445
# FIXME: Set "origin" url from .git/config ?
446
cs = self.repository._git.get_config_stack()
448
location = cs.get((b"remote", b'origin'), b"url")
454
ref = cs.get((b"remote", b"origin"), b"merge")
460
params['branch'] = ref_to_branch_name(ref).encode('utf-8')
462
params['ref'] = ref.encode('utf-8')
464
url = git_url_to_bzr_url(location)
465
return urlutils.join_segment_parameters(url, params)
467
def set_parent(self, location):
468
# FIXME: Set "origin" url in .git/config ?
469
cs = self.repository._git.get_config()
470
this_url = urlutils.split_segment_parameters(self.user_url)[0]
471
target_url, target_params = urlutils.split_segment_parameters(location)
472
location = urlutils.relative_url(this_url, target_url)
473
cs.set((b"remote", b"origin"), b"url", location)
474
if 'branch' in target_params:
475
cs.set((b"remote", b"origin"), b"merge",
476
branch_name_to_ref(target_params['branch']))
477
elif 'ref' in target_params:
478
cs.set((b"remote", b"origin"), b"merge",
479
target_params['ref'])
481
# TODO(jelmer): Maybe unset rather than setting to HEAD?
482
cs.set((b"remote", b"origin"), b"merge", 'HEAD')
485
self.repository._git._put_named_file('config', f.getvalue())
487
def break_lock(self):
488
raise NotImplementedError(self.break_lock)
492
if self._lock_mode not in ('r', 'w'):
493
raise ValueError(self._lock_mode)
494
self._lock_count += 1
496
self._lock_mode = 'r'
498
self.repository.lock_read()
499
return lock.LogicalLockResult(self.unlock)
501
def peek_lock_mode(self):
502
return self._lock_mode
505
return (self._lock_mode is not None)
510
def _unlock_ref(self):
514
"""See Branch.unlock()."""
515
if self._lock_count == 0:
516
raise errors.LockNotHeld(self)
518
self._lock_count -= 1
519
if self._lock_count == 0:
520
if self._lock_mode == 'w':
522
self._lock_mode = None
523
self._clear_cached_state()
525
self.repository.unlock()
527
def get_physical_lock_status(self):
530
def last_revision(self):
531
with self.lock_read():
532
# perhaps should escape this ?
533
if self.head is None:
534
return revision.NULL_REVISION
535
return self.lookup_foreign_revision_id(self.head)
537
def _basic_push(self, target, overwrite=False, stop_revision=None):
538
return branch.InterBranch.get(self, target)._basic_push(
539
overwrite, stop_revision)
541
def lookup_foreign_revision_id(self, foreign_revid):
543
return self.repository.lookup_foreign_revision_id(foreign_revid,
547
return self.mapping.revision_id_foreign_to_bzr(foreign_revid)
549
def lookup_bzr_revision_id(self, revid):
550
return self.repository.lookup_bzr_revision_id(
551
revid, mapping=self.mapping)
553
def get_unshelver(self, tree):
554
raise errors.StoringUncommittedNotSupported(self)
556
def _clear_cached_state(self):
557
super(GitBranch, self)._clear_cached_state()
558
self._tag_refs = None
560
def _iter_tag_refs(self, refs):
561
"""Iterate over the tag refs.
563
:param refs: Refs dictionary (name -> git sha1)
564
:return: iterator over (ref_name, tag_name, peeled_sha1, unpeeled_sha1)
566
raise NotImplementedError(self._iter_tag_refs)
568
def get_tag_refs(self):
569
with self.lock_read():
570
if self._tag_refs is None:
571
self._tag_refs = list(self._iter_tag_refs())
572
return self._tag_refs
575
class LocalGitBranch(GitBranch):
576
"""A local Git branch."""
578
def __init__(self, controldir, repository, ref):
579
super(LocalGitBranch, self).__init__(controldir, repository, ref,
580
LocalGitBranchFormat())
582
def create_checkout(self, to_location, revision_id=None, lightweight=False,
583
accelerator_tree=None, hardlink=False):
584
t = transport.get_transport(to_location)
586
format = self._get_checkout_format(lightweight=lightweight)
587
checkout = format.initialize_on_transport(t)
589
from_branch = checkout.set_branch_reference(target_branch=self)
591
policy = checkout.determine_repository_policy()
592
repo = policy.acquire_repository()[0]
594
checkout_branch = checkout.create_branch()
595
checkout_branch.bind(self)
596
checkout_branch.pull(self, stop_revision=revision_id)
598
return checkout.create_workingtree(revision_id,
599
from_branch=from_branch, hardlink=hardlink)
602
self._ref_lock = self.repository._git.refs.lock_ref(self.ref)
604
def _unlock_ref(self):
605
self._ref_lock.unlock()
607
def break_lock(self):
608
self.repository._git.refs.unlock_ref(self.ref)
610
def fetch(self, from_branch, last_revision=None, limit=None):
611
return branch.InterBranch.get(from_branch, self).fetch(
612
stop_revision=last_revision, limit=limit)
614
def _gen_revision_history(self):
615
if self.head is None:
617
graph = self.repository.get_graph()
618
ret = list(graph.iter_lefthand_ancestry(self.last_revision(),
619
(revision.NULL_REVISION, )))
625
return self.repository._git.refs[self.ref]
629
def _read_last_revision_info(self):
630
last_revid = self.last_revision()
631
graph = self.repository.get_graph()
632
revno = graph.find_distance_to_null(last_revid,
633
[(revision.NULL_REVISION, 0)])
634
return revno, last_revid
636
def set_last_revision_info(self, revno, revision_id):
637
self.set_last_revision(revision_id)
638
self._last_revision_info_cache = revno, revision_id
640
def set_last_revision(self, revid):
641
if not revid or not isinstance(revid, bytes):
642
raise errors.InvalidRevisionId(revision_id=revid, branch=self)
643
if revid == NULL_REVISION:
646
(newhead, self.mapping) = self.repository.lookup_bzr_revision_id(revid)
647
if self.mapping is None:
649
self._set_head(newhead)
651
def _set_head(self, value):
652
if value == ZERO_SHA:
653
raise ValueError(value)
656
del self.repository._git.refs[self.ref]
658
self.repository._git.refs[self.ref] = self._head
659
self._clear_cached_state()
661
head = property(_get_head, _set_head)
663
def get_push_location(self):
664
"""See Branch.get_push_location."""
665
push_loc = self.get_config_stack().get('push_location')
668
def set_push_location(self, location):
669
"""See Branch.set_push_location."""
670
self.get_config().set_user_option('push_location', location,
671
store=config.STORE_LOCATION)
673
def supports_tags(self):
676
def store_uncommitted(self, creator):
677
raise errors.StoringUncommittedNotSupported(self)
679
def _iter_tag_refs(self):
680
"""Iterate over the tag refs.
682
:param refs: Refs dictionary (name -> git sha1)
683
:return: iterator over (ref_name, tag_name, peeled_sha1, unpeeled_sha1)
685
refs = self.repository._git.refs
686
for ref_name, unpeeled in refs.as_dict().iteritems():
688
tag_name = ref_to_tag_name(ref_name)
689
except (ValueError, UnicodeDecodeError):
691
peeled = refs.get_peeled(ref_name)
694
if type(tag_name) is not unicode:
695
raise TypeError(tag_name)
696
yield (ref_name, tag_name, peeled, unpeeled)
698
def create_memorytree(self):
699
from .memorytree import GitMemoryTree
700
return GitMemoryTree(self, self.repository._git.object_store, self.head)
702
def reference_parent(self, path, file_id=None, possible_transports=None):
703
"""Return the parent branch for a tree-reference file_id
705
:param path: The path of the file_id in the tree
706
:param file_id: Optional file_id of the tree reference
707
:return: A branch associated with the file_id
709
# FIXME should provide multiple branches, based on config
710
url = urlutils.join(self.user_url, path)
711
return branch.Branch.open(
713
possible_transports=possible_transports)
717
def _quick_lookup_revno(local_branch, remote_branch, revid):
718
if type(revid) is not str:
719
raise TypeError(revid)
720
# Try in source branch first, it'll be faster
721
with local_branch.lock_read():
723
return local_branch.revision_id_to_revno(revid)
724
except errors.NoSuchRevision:
725
graph = local_branch.repository.get_graph()
727
return graph.find_distance_to_null(revid,
728
[(revision.NULL_REVISION, 0)])
729
except errors.GhostRevisionsHaveNoRevno:
730
# FIXME: Check using graph.find_distance_to_null() ?
731
with remote_branch.lock_read():
732
return remote_branch.revision_id_to_revno(revid)
735
class GitBranchPullResult(branch.PullResult):
738
super(GitBranchPullResult, self).__init__()
739
self.new_git_head = None
740
self._old_revno = None
741
self._new_revno = None
743
def report(self, to_file):
745
if self.old_revid == self.new_revid:
746
to_file.write('No revisions to pull.\n')
747
elif self.new_git_head is not None:
748
to_file.write('Now on revision %d (git sha: %s).\n' %
749
(self.new_revno, self.new_git_head))
751
to_file.write('Now on revision %d.\n' % (self.new_revno,))
752
self._show_tag_conficts(to_file)
754
def _lookup_revno(self, revid):
755
return _quick_lookup_revno(self.target_branch, self.source_branch,
758
def _get_old_revno(self):
759
if self._old_revno is not None:
760
return self._old_revno
761
return self._lookup_revno(self.old_revid)
763
def _set_old_revno(self, revno):
764
self._old_revno = revno
766
old_revno = property(_get_old_revno, _set_old_revno)
768
def _get_new_revno(self):
769
if self._new_revno is not None:
770
return self._new_revno
771
return self._lookup_revno(self.new_revid)
773
def _set_new_revno(self, revno):
774
self._new_revno = revno
776
new_revno = property(_get_new_revno, _set_new_revno)
779
class GitBranchPushResult(branch.BranchPushResult):
781
def _lookup_revno(self, revid):
782
return _quick_lookup_revno(self.source_branch, self.target_branch,
787
return self._lookup_revno(self.old_revid)
791
new_original_revno = getattr(self, "new_original_revno", None)
792
if new_original_revno:
793
return new_original_revno
794
if getattr(self, "new_original_revid", None) is not None:
795
return self._lookup_revno(self.new_original_revid)
796
return self._lookup_revno(self.new_revid)
799
class InterFromGitBranch(branch.GenericInterBranch):
800
"""InterBranch implementation that pulls from Git into bzr."""
803
def _get_branch_formats_to_test():
805
default_format = branch.format_registry.get_default()
806
except AttributeError:
807
default_format = branch.BranchFormat._default_format
808
from .remote import RemoteGitBranchFormat
810
(RemoteGitBranchFormat(), default_format),
811
(LocalGitBranchFormat(), default_format)]
814
def _get_interrepo(self, source, target):
815
return _mod_repository.InterRepository.get(source.repository, target.repository)
818
def is_compatible(cls, source, target):
819
if not isinstance(source, GitBranch):
821
if isinstance(target, GitBranch):
822
# InterLocalGitRemoteGitBranch or InterToGitBranch should be used
824
if getattr(cls._get_interrepo(source, target), "fetch_objects", None) is None:
825
# fetch_objects is necessary for this to work
829
def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
830
self.fetch_objects(stop_revision, fetch_tags=fetch_tags, limit=limit)
832
def fetch_objects(self, stop_revision, fetch_tags, limit=None):
833
interrepo = self._get_interrepo(self.source, self.target)
834
if fetch_tags is None:
835
c = self.source.get_config_stack()
836
fetch_tags = c.get('branch.fetch_tags')
837
def determine_wants(heads):
838
if stop_revision is None:
840
head = heads[self.source.ref]
842
self._last_revid = revision.NULL_REVISION
844
self._last_revid = self.source.lookup_foreign_revision_id(head)
846
self._last_revid = stop_revision
847
real = interrepo.get_determine_wants_revids(
848
[self._last_revid], include_tags=fetch_tags)
850
pack_hint, head, refs = interrepo.fetch_objects(
851
determine_wants, self.source.mapping, limit=limit)
852
if (pack_hint is not None and
853
self.target.repository._format.pack_compresses):
854
self.target.repository.pack(hint=pack_hint)
857
def _update_revisions(self, stop_revision=None, overwrite=False):
858
head, refs = self.fetch_objects(stop_revision, fetch_tags=None)
860
prev_last_revid = None
862
prev_last_revid = self.target.last_revision()
863
self.target.generate_revision_history(self._last_revid,
864
last_rev=prev_last_revid, other_branch=self.source)
867
def _basic_pull(self, stop_revision, overwrite, run_hooks,
868
_override_hook_target, _hook_master):
869
if overwrite is True:
870
overwrite = set(["history", "tags"])
873
result = GitBranchPullResult()
874
result.source_branch = self.source
875
if _override_hook_target is None:
876
result.target_branch = self.target
878
result.target_branch = _override_hook_target
879
with self.target.lock_write(), self.source.lock_read():
880
# We assume that during 'pull' the target repository is closer than
882
(result.old_revno, result.old_revid) = \
883
self.target.last_revision_info()
884
result.new_git_head, remote_refs = self._update_revisions(
885
stop_revision, overwrite=("history" in overwrite))
886
tags_ret = self.source.tags.merge_to(
887
self.target.tags, ("tags" in overwrite), ignore_master=True)
888
if isinstance(tags_ret, tuple):
889
result.tag_updates, result.tag_conflicts = tags_ret
891
result.tag_conflicts = tags_ret
892
(result.new_revno, result.new_revid) = \
893
self.target.last_revision_info()
895
result.master_branch = _hook_master
896
result.local_branch = result.target_branch
898
result.master_branch = result.target_branch
899
result.local_branch = None
901
for hook in branch.Branch.hooks['post_pull']:
905
def pull(self, overwrite=False, stop_revision=None,
906
possible_transports=None, _hook_master=None, run_hooks=True,
907
_override_hook_target=None, local=False):
910
:param _hook_master: Private parameter - set the branch to
911
be supplied as the master to pull hooks.
912
:param run_hooks: Private parameter - if false, this branch
913
is being called because it's the master of the primary branch,
914
so it should not run its hooks.
915
:param _override_hook_target: Private parameter - set the branch to be
916
supplied as the target_branch to pull hooks.
918
# This type of branch can't be bound.
919
bound_location = self.target.get_bound_location()
920
if local and not bound_location:
921
raise errors.LocalRequiresBoundBranch()
923
source_is_master = False
924
self.source.lock_read()
926
# bound_location comes from a config file, some care has to be
927
# taken to relate it to source.user_url
928
normalized = urlutils.normalize_url(bound_location)
930
relpath = self.source.user_transport.relpath(normalized)
931
source_is_master = (relpath == '')
932
except (errors.PathNotChild, urlutils.InvalidURL):
933
source_is_master = False
934
if not local and bound_location and not source_is_master:
935
# not pulling from master, so we need to update master.
936
master_branch = self.target.get_master_branch(possible_transports)
937
master_branch.lock_write()
941
# pull from source into master.
942
master_branch.pull(self.source, overwrite, stop_revision,
944
result = self._basic_pull(stop_revision, overwrite, run_hooks,
945
_override_hook_target, _hook_master=master_branch)
950
master_branch.unlock()
953
def _basic_push(self, overwrite, stop_revision):
954
if overwrite is True:
955
overwrite = set(["history", "tags"])
958
result = branch.BranchPushResult()
959
result.source_branch = self.source
960
result.target_branch = self.target
961
result.old_revno, result.old_revid = self.target.last_revision_info()
962
result.new_git_head, remote_refs = self._update_revisions(
963
stop_revision, overwrite=("history" in overwrite))
964
tags_ret = self.source.tags.merge_to(self.target.tags,
965
"tags" in overwrite, ignore_master=True)
966
(result.tag_updates, result.tag_conflicts) = tags_ret
967
result.new_revno, result.new_revid = self.target.last_revision_info()
971
class InterGitBranch(branch.GenericInterBranch):
972
"""InterBranch implementation that pulls between Git branches."""
974
def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
975
raise NotImplementedError(self.fetch)
978
class InterLocalGitRemoteGitBranch(InterGitBranch):
979
"""InterBranch that copies from a local to a remote git branch."""
982
def _get_branch_formats_to_test():
983
from .remote import RemoteGitBranchFormat
985
(LocalGitBranchFormat(), RemoteGitBranchFormat())]
988
def is_compatible(self, source, target):
989
from .remote import RemoteGitBranch
990
return (isinstance(source, LocalGitBranch) and
991
isinstance(target, RemoteGitBranch))
993
def _basic_push(self, overwrite, stop_revision):
994
result = GitBranchPushResult()
995
result.source_branch = self.source
996
result.target_branch = self.target
997
if stop_revision is None:
998
stop_revision = self.source.last_revision()
999
def get_changed_refs(old_refs):
1000
old_ref = old_refs.get(self.target.ref, None)
1002
result.old_revid = revision.NULL_REVISION
1004
result.old_revid = self.target.lookup_foreign_revision_id(old_ref)
1005
new_ref = self.source.repository.lookup_bzr_revision_id(stop_revision)[0]
1007
if remote_divergence(old_ref, new_ref, self.source.repository._git.object_store):
1008
raise errors.DivergedBranches(self.source, self.target)
1009
refs = { self.target.ref: new_ref }
1010
result.new_revid = stop_revision
1011
for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
1012
refs[tag_name_to_ref(name)] = sha
1014
self.target.repository.send_pack(get_changed_refs,
1015
self.source.repository._git.object_store.generate_pack_data)
1019
class InterGitLocalGitBranch(InterGitBranch):
1020
"""InterBranch that copies from a remote to a local git branch."""
1023
def _get_branch_formats_to_test():
1024
from .remote import RemoteGitBranchFormat
1026
(RemoteGitBranchFormat(), LocalGitBranchFormat()),
1027
(LocalGitBranchFormat(), LocalGitBranchFormat())]
1030
def is_compatible(self, source, target):
1031
return (isinstance(source, GitBranch) and
1032
isinstance(target, LocalGitBranch))
1034
def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
1035
interrepo = _mod_repository.InterRepository.get(self.source.repository,
1036
self.target.repository)
1037
if stop_revision is None:
1038
stop_revision = self.source.last_revision()
1039
determine_wants = interrepo.get_determine_wants_revids(
1040
[stop_revision], include_tags=fetch_tags)
1041
interrepo.fetch_objects(determine_wants, limit=limit)
1043
def _basic_push(self, overwrite=False, stop_revision=None):
1044
if overwrite is True:
1045
overwrite = set(["history", "tags"])
1048
result = GitBranchPushResult()
1049
result.source_branch = self.source
1050
result.target_branch = self.target
1051
result.old_revid = self.target.last_revision()
1052
refs, stop_revision = self.update_refs(stop_revision)
1053
self.target.generate_revision_history(stop_revision,
1054
(result.old_revid if ("history" not in overwrite) else None),
1055
other_branch=self.source)
1056
tags_ret = self.source.tags.merge_to(self.target.tags,
1057
source_tag_refs=remote_refs_dict_to_tag_refs(refs),
1058
overwrite=("tags" in overwrite))
1059
if isinstance(tags_ret, tuple):
1060
(result.tag_updates, result.tag_conflicts) = tags_ret
1062
result.tag_conflicts = tags_ret
1063
result.new_revid = self.target.last_revision()
1066
def update_refs(self, stop_revision=None):
1067
interrepo = _mod_repository.InterRepository.get(
1068
self.source.repository, self.target.repository)
1069
c = self.source.get_config_stack()
1070
fetch_tags = c.get('branch.fetch_tags')
1072
if stop_revision is None:
1073
refs = interrepo.fetch(branches=["HEAD"], include_tags=fetch_tags)
1077
stop_revision = revision.NULL_REVISION
1079
stop_revision = self.target.lookup_foreign_revision_id(head)
1081
refs = interrepo.fetch(revision_id=stop_revision, include_tags=fetch_tags)
1082
return refs, stop_revision
1084
def pull(self, stop_revision=None, overwrite=False,
1085
possible_transports=None, run_hooks=True, local=False):
1086
# This type of branch can't be bound.
1088
raise errors.LocalRequiresBoundBranch()
1089
if overwrite is True:
1090
overwrite = set(["history", "tags"])
1094
result = GitPullResult()
1095
result.source_branch = self.source
1096
result.target_branch = self.target
1097
with self.target.lock_write(), self.source.lock_read():
1098
result.old_revid = self.target.last_revision()
1099
refs, stop_revision = self.update_refs(stop_revision)
1100
self.target.generate_revision_history(stop_revision,
1101
(result.old_revid if ("history" not in overwrite) else None),
1102
other_branch=self.source)
1103
tags_ret = self.source.tags.merge_to(self.target.tags,
1104
overwrite=("tags" in overwrite),
1105
source_tag_refs=remote_refs_dict_to_tag_refs(refs))
1106
if isinstance(tags_ret, tuple):
1107
(result.tag_updates, result.tag_conflicts) = tags_ret
1109
result.tag_conflicts = tags_ret
1110
result.new_revid = self.target.last_revision()
1111
result.local_branch = None
1112
result.master_branch = result.target_branch
1114
for hook in branch.Branch.hooks['post_pull']:
1119
class InterToGitBranch(branch.GenericInterBranch):
1120
"""InterBranch implementation that pulls from a non-bzr into a Git branch."""
1122
def __init__(self, source, target):
1123
super(InterToGitBranch, self).__init__(source, target)
1124
self.interrepo = _mod_repository.InterRepository.get(source.repository,
1128
def _get_branch_formats_to_test():
1130
default_format = branch.format_registry.get_default()
1131
except AttributeError:
1132
default_format = branch.BranchFormat._default_format
1133
from .remote import RemoteGitBranchFormat
1135
(default_format, LocalGitBranchFormat()),
1136
(default_format, RemoteGitBranchFormat())]
1139
def is_compatible(self, source, target):
1140
return (not isinstance(source, GitBranch) and
1141
isinstance(target, GitBranch))
1143
def _get_new_refs(self, stop_revision=None, fetch_tags=None):
1144
if not self.source.is_locked():
1145
raise errors.ObjectNotLocked(self.source)
1146
if stop_revision is None:
1147
(stop_revno, stop_revision) = self.source.last_revision_info()
1149
stop_revno = self.source.revision_id_to_revno(stop_revision)
1150
if type(stop_revision) is not str:
1151
raise TypeError(stop_revision)
1152
main_ref = self.target.ref
1153
refs = { main_ref: (None, stop_revision) }
1154
if fetch_tags is None:
1155
c = self.source.get_config_stack()
1156
fetch_tags = c.get('branch.fetch_tags')
1157
for name, revid in self.source.tags.get_tag_dict().iteritems():
1158
if self.source.repository.has_revision(revid):
1159
ref = tag_name_to_ref(name)
1160
if not check_ref_format(ref):
1161
warning("skipping tag with invalid characters %s (%s)",
1165
# FIXME: Skip tags that are not in the ancestry
1166
refs[ref] = (None, revid)
1167
return refs, main_ref, (stop_revno, stop_revision)
1169
def _update_refs(self, result, old_refs, new_refs, overwrite):
1170
mutter("updating refs. old refs: %r, new refs: %r",
1172
result.tag_updates = {}
1173
result.tag_conflicts = []
1174
ret = dict(old_refs)
1175
def ref_equals(refs, ref, git_sha, revid):
1180
if (value[0] is not None and
1181
git_sha is not None and
1182
value[0] == git_sha):
1184
if (value[1] is not None and
1185
revid is not None and
1188
# FIXME: If one side only has the git sha available and the other only
1189
# has the bzr revid, then this will cause us to show a tag as updated
1190
# that hasn't actually been updated.
1192
# FIXME: Check for diverged branches
1193
for ref, (git_sha, revid) in new_refs.iteritems():
1194
if ref_equals(ret, ref, git_sha, revid):
1195
# Already up to date
1197
git_sha = old_refs[ref][0]
1199
revid = old_refs[ref][1]
1200
ret[ref] = new_refs[ref] = (git_sha, revid)
1201
elif ref not in ret or overwrite:
1203
tag_name = ref_to_tag_name(ref)
1207
result.tag_updates[tag_name] = revid
1208
ret[ref] = (git_sha, revid)
1210
# FIXME: Check diverged
1214
name = ref_to_tag_name(ref)
1218
result.tag_conflicts.append((name, revid, ret[name][1]))
1220
ret[ref] = (git_sha, revid)
1223
def fetch(self, stop_revision=None, fetch_tags=None, lossy=False, limit=None):
1224
if stop_revision is None:
1225
stop_revision = self.source.last_revision()
1228
for k, v in self.source.tags.get_tag_dict().iteritems():
1229
ret.append((None, v))
1230
ret.append((None, stop_revision))
1232
self.interrepo.fetch_objects(ret, lossy=lossy, limit=limit)
1233
except NoPushSupport:
1234
raise errors.NoRoundtrippingSupport(self.source, self.target)
1236
def pull(self, overwrite=False, stop_revision=None, local=False,
1237
possible_transports=None, run_hooks=True):
1238
result = GitBranchPullResult()
1239
result.source_branch = self.source
1240
result.target_branch = self.target
1241
with self.source.lock_read(), self.target.lock_write():
1242
new_refs, main_ref, stop_revinfo = self._get_new_refs(
1244
def update_refs(old_refs):
1245
return self._update_refs(result, old_refs, new_refs, overwrite)
1247
result.revidmap, old_refs, new_refs = self.interrepo.fetch_refs(
1248
update_refs, lossy=False)
1249
except NoPushSupport:
1250
raise errors.NoRoundtrippingSupport(self.source, self.target)
1251
(old_sha1, result.old_revid) = old_refs.get(main_ref, (ZERO_SHA, NULL_REVISION))
1252
if result.old_revid is None:
1253
result.old_revid = self.target.lookup_foreign_revision_id(old_sha1)
1254
result.new_revid = new_refs[main_ref][1]
1255
result.local_branch = None
1256
result.master_branch = self.target
1258
for hook in branch.Branch.hooks['post_pull']:
1262
def push(self, overwrite=False, stop_revision=None, lossy=False,
1263
_override_hook_source_branch=None):
1264
result = GitBranchPushResult()
1265
result.source_branch = self.source
1266
result.target_branch = self.target
1267
result.local_branch = None
1268
result.master_branch = result.target_branch
1269
with self.source.lock_read(), self.target.lock_write():
1270
new_refs, main_ref, stop_revinfo = self._get_new_refs(stop_revision)
1271
def update_refs(old_refs):
1272
return self._update_refs(result, old_refs, new_refs, overwrite)
1274
result.revidmap, old_refs, new_refs = self.interrepo.fetch_refs(
1275
update_refs, lossy=lossy, overwrite=overwrite)
1276
except NoPushSupport:
1277
raise errors.NoRoundtrippingSupport(self.source, self.target)
1278
(old_sha1, result.old_revid) = old_refs.get(main_ref, (ZERO_SHA, NULL_REVISION))
1279
if result.old_revid is None:
1280
result.old_revid = self.target.lookup_foreign_revision_id(old_sha1)
1281
result.new_revid = new_refs[main_ref][1]
1282
(result.new_original_revno, result.new_original_revid) = stop_revinfo
1283
for hook in branch.Branch.hooks['post_push']:
1288
branch.InterBranch.register_optimiser(InterGitLocalGitBranch)
1289
branch.InterBranch.register_optimiser(InterFromGitBranch)
1290
branch.InterBranch.register_optimiser(InterToGitBranch)
1291
branch.InterBranch.register_optimiser(InterLocalGitRemoteGitBranch)