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 (
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
last_revid = self.last_revision()
618
graph = self.repository.get_graph()
620
ret = list(graph.iter_lefthand_ancestry(last_revid,
621
(revision.NULL_REVISION, )))
622
except errors.RevisionNotPresent as e:
623
raise errors.GhostRevisionsHaveNoRevno(last_revid, e.revision_id)
629
return self.repository._git.refs[self.ref]
633
def _read_last_revision_info(self):
634
last_revid = self.last_revision()
635
graph = self.repository.get_graph()
636
revno = graph.find_distance_to_null(last_revid,
637
[(revision.NULL_REVISION, 0)])
638
return revno, last_revid
640
def set_last_revision_info(self, revno, revision_id):
641
self.set_last_revision(revision_id)
642
self._last_revision_info_cache = revno, revision_id
644
def set_last_revision(self, revid):
645
if not revid or not isinstance(revid, bytes):
646
raise errors.InvalidRevisionId(revision_id=revid, branch=self)
647
if revid == NULL_REVISION:
650
(newhead, self.mapping) = self.repository.lookup_bzr_revision_id(revid)
651
if self.mapping is None:
653
self._set_head(newhead)
655
def _set_head(self, value):
656
if value == ZERO_SHA:
657
raise ValueError(value)
660
del self.repository._git.refs[self.ref]
662
self.repository._git.refs[self.ref] = self._head
663
self._clear_cached_state()
665
head = property(_get_head, _set_head)
667
def get_push_location(self):
668
"""See Branch.get_push_location."""
669
push_loc = self.get_config_stack().get('push_location')
672
def set_push_location(self, location):
673
"""See Branch.set_push_location."""
674
self.get_config().set_user_option('push_location', location,
675
store=config.STORE_LOCATION)
677
def supports_tags(self):
680
def store_uncommitted(self, creator):
681
raise errors.StoringUncommittedNotSupported(self)
683
def _iter_tag_refs(self):
684
"""Iterate over the tag refs.
686
:param refs: Refs dictionary (name -> git sha1)
687
:return: iterator over (ref_name, tag_name, peeled_sha1, unpeeled_sha1)
689
refs = self.repository._git.refs
690
for ref_name, unpeeled in refs.as_dict().iteritems():
692
tag_name = ref_to_tag_name(ref_name)
693
except (ValueError, UnicodeDecodeError):
695
peeled = refs.get_peeled(ref_name)
698
if type(tag_name) is not unicode:
699
raise TypeError(tag_name)
700
yield (ref_name, tag_name, peeled, unpeeled)
702
def create_memorytree(self):
703
from .memorytree import GitMemoryTree
704
return GitMemoryTree(self, self.repository._git.object_store, self.head)
706
def reference_parent(self, path, file_id=None, possible_transports=None):
707
"""Return the parent branch for a tree-reference file_id
709
:param path: The path of the file_id in the tree
710
:param file_id: Optional file_id of the tree reference
711
:return: A branch associated with the file_id
713
# FIXME should provide multiple branches, based on config
714
url = urlutils.join(self.user_url, path)
715
return branch.Branch.open(
717
possible_transports=possible_transports)
721
def _quick_lookup_revno(local_branch, remote_branch, revid):
722
if type(revid) is not str:
723
raise TypeError(revid)
724
# Try in source branch first, it'll be faster
725
with local_branch.lock_read():
727
return local_branch.revision_id_to_revno(revid)
728
except errors.NoSuchRevision:
729
graph = local_branch.repository.get_graph()
731
return graph.find_distance_to_null(revid,
732
[(revision.NULL_REVISION, 0)])
733
except errors.GhostRevisionsHaveNoRevno:
734
# FIXME: Check using graph.find_distance_to_null() ?
735
with remote_branch.lock_read():
736
return remote_branch.revision_id_to_revno(revid)
739
class GitBranchPullResult(branch.PullResult):
742
super(GitBranchPullResult, self).__init__()
743
self.new_git_head = None
744
self._old_revno = None
745
self._new_revno = None
747
def report(self, to_file):
749
if self.old_revid == self.new_revid:
750
to_file.write('No revisions to pull.\n')
751
elif self.new_git_head is not None:
752
to_file.write('Now on revision %d (git sha: %s).\n' %
753
(self.new_revno, self.new_git_head))
755
to_file.write('Now on revision %d.\n' % (self.new_revno,))
756
self._show_tag_conficts(to_file)
758
def _lookup_revno(self, revid):
759
return _quick_lookup_revno(self.target_branch, self.source_branch,
762
def _get_old_revno(self):
763
if self._old_revno is not None:
764
return self._old_revno
765
return self._lookup_revno(self.old_revid)
767
def _set_old_revno(self, revno):
768
self._old_revno = revno
770
old_revno = property(_get_old_revno, _set_old_revno)
772
def _get_new_revno(self):
773
if self._new_revno is not None:
774
return self._new_revno
775
return self._lookup_revno(self.new_revid)
777
def _set_new_revno(self, revno):
778
self._new_revno = revno
780
new_revno = property(_get_new_revno, _set_new_revno)
783
class GitBranchPushResult(branch.BranchPushResult):
785
def _lookup_revno(self, revid):
786
return _quick_lookup_revno(self.source_branch, self.target_branch,
791
return self._lookup_revno(self.old_revid)
795
new_original_revno = getattr(self, "new_original_revno", None)
796
if new_original_revno:
797
return new_original_revno
798
if getattr(self, "new_original_revid", None) is not None:
799
return self._lookup_revno(self.new_original_revid)
800
return self._lookup_revno(self.new_revid)
803
class InterFromGitBranch(branch.GenericInterBranch):
804
"""InterBranch implementation that pulls from Git into bzr."""
807
def _get_branch_formats_to_test():
809
default_format = branch.format_registry.get_default()
810
except AttributeError:
811
default_format = branch.BranchFormat._default_format
812
from .remote import RemoteGitBranchFormat
814
(RemoteGitBranchFormat(), default_format),
815
(LocalGitBranchFormat(), default_format)]
818
def _get_interrepo(self, source, target):
819
return _mod_repository.InterRepository.get(source.repository, target.repository)
822
def is_compatible(cls, source, target):
823
if not isinstance(source, GitBranch):
825
if isinstance(target, GitBranch):
826
# InterLocalGitRemoteGitBranch or InterToGitBranch should be used
828
if getattr(cls._get_interrepo(source, target), "fetch_objects", None) is None:
829
# fetch_objects is necessary for this to work
833
def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
834
self.fetch_objects(stop_revision, fetch_tags=fetch_tags, limit=limit)
836
def fetch_objects(self, stop_revision, fetch_tags, limit=None):
837
interrepo = self._get_interrepo(self.source, self.target)
838
if fetch_tags is None:
839
c = self.source.get_config_stack()
840
fetch_tags = c.get('branch.fetch_tags')
841
def determine_wants(heads):
842
if stop_revision is None:
844
head = heads[self.source.ref]
846
self._last_revid = revision.NULL_REVISION
848
self._last_revid = self.source.lookup_foreign_revision_id(head)
850
self._last_revid = stop_revision
851
real = interrepo.get_determine_wants_revids(
852
[self._last_revid], include_tags=fetch_tags)
854
pack_hint, head, refs = interrepo.fetch_objects(
855
determine_wants, self.source.mapping, limit=limit)
856
if (pack_hint is not None and
857
self.target.repository._format.pack_compresses):
858
self.target.repository.pack(hint=pack_hint)
861
def _update_revisions(self, stop_revision=None, overwrite=False):
862
head, refs = self.fetch_objects(stop_revision, fetch_tags=None)
864
prev_last_revid = None
866
prev_last_revid = self.target.last_revision()
867
self.target.generate_revision_history(self._last_revid,
868
last_rev=prev_last_revid, other_branch=self.source)
871
def _basic_pull(self, stop_revision, overwrite, run_hooks,
872
_override_hook_target, _hook_master):
873
if overwrite is True:
874
overwrite = set(["history", "tags"])
877
result = GitBranchPullResult()
878
result.source_branch = self.source
879
if _override_hook_target is None:
880
result.target_branch = self.target
882
result.target_branch = _override_hook_target
883
with self.target.lock_write(), self.source.lock_read():
884
# We assume that during 'pull' the target repository is closer than
886
(result.old_revno, result.old_revid) = \
887
self.target.last_revision_info()
888
result.new_git_head, remote_refs = self._update_revisions(
889
stop_revision, overwrite=("history" in overwrite))
890
tags_ret = self.source.tags.merge_to(
891
self.target.tags, ("tags" in overwrite), ignore_master=True)
892
if isinstance(tags_ret, tuple):
893
result.tag_updates, result.tag_conflicts = tags_ret
895
result.tag_conflicts = tags_ret
896
(result.new_revno, result.new_revid) = \
897
self.target.last_revision_info()
899
result.master_branch = _hook_master
900
result.local_branch = result.target_branch
902
result.master_branch = result.target_branch
903
result.local_branch = None
905
for hook in branch.Branch.hooks['post_pull']:
909
def pull(self, overwrite=False, stop_revision=None,
910
possible_transports=None, _hook_master=None, run_hooks=True,
911
_override_hook_target=None, local=False):
914
:param _hook_master: Private parameter - set the branch to
915
be supplied as the master to pull hooks.
916
:param run_hooks: Private parameter - if false, this branch
917
is being called because it's the master of the primary branch,
918
so it should not run its hooks.
919
:param _override_hook_target: Private parameter - set the branch to be
920
supplied as the target_branch to pull hooks.
922
# This type of branch can't be bound.
923
bound_location = self.target.get_bound_location()
924
if local and not bound_location:
925
raise errors.LocalRequiresBoundBranch()
927
source_is_master = False
928
self.source.lock_read()
930
# bound_location comes from a config file, some care has to be
931
# taken to relate it to source.user_url
932
normalized = urlutils.normalize_url(bound_location)
934
relpath = self.source.user_transport.relpath(normalized)
935
source_is_master = (relpath == '')
936
except (errors.PathNotChild, urlutils.InvalidURL):
937
source_is_master = False
938
if not local and bound_location and not source_is_master:
939
# not pulling from master, so we need to update master.
940
master_branch = self.target.get_master_branch(possible_transports)
941
master_branch.lock_write()
945
# pull from source into master.
946
master_branch.pull(self.source, overwrite, stop_revision,
948
result = self._basic_pull(stop_revision, overwrite, run_hooks,
949
_override_hook_target, _hook_master=master_branch)
954
master_branch.unlock()
957
def _basic_push(self, overwrite, stop_revision):
958
if overwrite is True:
959
overwrite = set(["history", "tags"])
962
result = branch.BranchPushResult()
963
result.source_branch = self.source
964
result.target_branch = self.target
965
result.old_revno, result.old_revid = self.target.last_revision_info()
966
result.new_git_head, remote_refs = self._update_revisions(
967
stop_revision, overwrite=("history" in overwrite))
968
tags_ret = self.source.tags.merge_to(self.target.tags,
969
"tags" in overwrite, ignore_master=True)
970
(result.tag_updates, result.tag_conflicts) = tags_ret
971
result.new_revno, result.new_revid = self.target.last_revision_info()
975
class InterGitBranch(branch.GenericInterBranch):
976
"""InterBranch implementation that pulls between Git branches."""
978
def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
979
raise NotImplementedError(self.fetch)
982
class InterLocalGitRemoteGitBranch(InterGitBranch):
983
"""InterBranch that copies from a local to a remote git branch."""
986
def _get_branch_formats_to_test():
987
from .remote import RemoteGitBranchFormat
989
(LocalGitBranchFormat(), RemoteGitBranchFormat())]
992
def is_compatible(self, source, target):
993
from .remote import RemoteGitBranch
994
return (isinstance(source, LocalGitBranch) and
995
isinstance(target, RemoteGitBranch))
997
def _basic_push(self, overwrite, stop_revision):
998
result = GitBranchPushResult()
999
result.source_branch = self.source
1000
result.target_branch = self.target
1001
if stop_revision is None:
1002
stop_revision = self.source.last_revision()
1003
def get_changed_refs(old_refs):
1004
old_ref = old_refs.get(self.target.ref, None)
1006
result.old_revid = revision.NULL_REVISION
1008
result.old_revid = self.target.lookup_foreign_revision_id(old_ref)
1009
new_ref = self.source.repository.lookup_bzr_revision_id(stop_revision)[0]
1011
if remote_divergence(old_ref, new_ref, self.source.repository._git.object_store):
1012
raise errors.DivergedBranches(self.source, self.target)
1013
refs = { self.target.ref: new_ref }
1014
result.new_revid = stop_revision
1015
for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
1016
refs[tag_name_to_ref(name)] = sha
1018
self.target.repository.send_pack(get_changed_refs,
1019
self.source.repository._git.object_store.generate_pack_data)
1023
class InterGitLocalGitBranch(InterGitBranch):
1024
"""InterBranch that copies from a remote to a local git branch."""
1027
def _get_branch_formats_to_test():
1028
from .remote import RemoteGitBranchFormat
1030
(RemoteGitBranchFormat(), LocalGitBranchFormat()),
1031
(LocalGitBranchFormat(), LocalGitBranchFormat())]
1034
def is_compatible(self, source, target):
1035
return (isinstance(source, GitBranch) and
1036
isinstance(target, LocalGitBranch))
1038
def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
1039
interrepo = _mod_repository.InterRepository.get(self.source.repository,
1040
self.target.repository)
1041
if stop_revision is None:
1042
stop_revision = self.source.last_revision()
1043
determine_wants = interrepo.get_determine_wants_revids(
1044
[stop_revision], include_tags=fetch_tags)
1045
interrepo.fetch_objects(determine_wants, limit=limit)
1047
def _basic_push(self, overwrite=False, stop_revision=None):
1048
if overwrite is True:
1049
overwrite = set(["history", "tags"])
1052
result = GitBranchPushResult()
1053
result.source_branch = self.source
1054
result.target_branch = self.target
1055
result.old_revid = self.target.last_revision()
1056
refs, stop_revision = self.update_refs(stop_revision)
1057
self.target.generate_revision_history(stop_revision,
1058
(result.old_revid if ("history" not in overwrite) else None),
1059
other_branch=self.source)
1060
tags_ret = self.source.tags.merge_to(self.target.tags,
1061
source_tag_refs=remote_refs_dict_to_tag_refs(refs),
1062
overwrite=("tags" in overwrite))
1063
if isinstance(tags_ret, tuple):
1064
(result.tag_updates, result.tag_conflicts) = tags_ret
1066
result.tag_conflicts = tags_ret
1067
result.new_revid = self.target.last_revision()
1070
def update_refs(self, stop_revision=None):
1071
interrepo = _mod_repository.InterRepository.get(
1072
self.source.repository, self.target.repository)
1073
c = self.source.get_config_stack()
1074
fetch_tags = c.get('branch.fetch_tags')
1076
if stop_revision is None:
1077
refs = interrepo.fetch(branches=["HEAD"], include_tags=fetch_tags)
1081
stop_revision = revision.NULL_REVISION
1083
stop_revision = self.target.lookup_foreign_revision_id(head)
1085
refs = interrepo.fetch(revision_id=stop_revision, include_tags=fetch_tags)
1086
return refs, stop_revision
1088
def pull(self, stop_revision=None, overwrite=False,
1089
possible_transports=None, run_hooks=True, local=False):
1090
# This type of branch can't be bound.
1092
raise errors.LocalRequiresBoundBranch()
1093
if overwrite is True:
1094
overwrite = set(["history", "tags"])
1098
result = GitPullResult()
1099
result.source_branch = self.source
1100
result.target_branch = self.target
1101
with self.target.lock_write(), self.source.lock_read():
1102
result.old_revid = self.target.last_revision()
1103
refs, stop_revision = self.update_refs(stop_revision)
1104
self.target.generate_revision_history(stop_revision,
1105
(result.old_revid if ("history" not in overwrite) else None),
1106
other_branch=self.source)
1107
tags_ret = self.source.tags.merge_to(self.target.tags,
1108
overwrite=("tags" in overwrite),
1109
source_tag_refs=remote_refs_dict_to_tag_refs(refs))
1110
if isinstance(tags_ret, tuple):
1111
(result.tag_updates, result.tag_conflicts) = tags_ret
1113
result.tag_conflicts = tags_ret
1114
result.new_revid = self.target.last_revision()
1115
result.local_branch = None
1116
result.master_branch = result.target_branch
1118
for hook in branch.Branch.hooks['post_pull']:
1123
class InterToGitBranch(branch.GenericInterBranch):
1124
"""InterBranch implementation that pulls from a non-bzr into a Git branch."""
1126
def __init__(self, source, target):
1127
super(InterToGitBranch, self).__init__(source, target)
1128
self.interrepo = _mod_repository.InterRepository.get(source.repository,
1132
def _get_branch_formats_to_test():
1134
default_format = branch.format_registry.get_default()
1135
except AttributeError:
1136
default_format = branch.BranchFormat._default_format
1137
from .remote import RemoteGitBranchFormat
1139
(default_format, LocalGitBranchFormat()),
1140
(default_format, RemoteGitBranchFormat())]
1143
def is_compatible(self, source, target):
1144
return (not isinstance(source, GitBranch) and
1145
isinstance(target, GitBranch))
1147
def _get_new_refs(self, stop_revision=None, fetch_tags=None):
1148
if not self.source.is_locked():
1149
raise errors.ObjectNotLocked(self.source)
1150
if stop_revision is None:
1151
(stop_revno, stop_revision) = self.source.last_revision_info()
1153
stop_revno = self.source.revision_id_to_revno(stop_revision)
1154
if type(stop_revision) is not str:
1155
raise TypeError(stop_revision)
1156
main_ref = self.target.ref
1157
refs = { main_ref: (None, stop_revision) }
1158
if fetch_tags is None:
1159
c = self.source.get_config_stack()
1160
fetch_tags = c.get('branch.fetch_tags')
1161
for name, revid in self.source.tags.get_tag_dict().iteritems():
1162
if self.source.repository.has_revision(revid):
1163
ref = tag_name_to_ref(name)
1164
if not check_ref_format(ref):
1165
warning("skipping tag with invalid characters %s (%s)",
1169
# FIXME: Skip tags that are not in the ancestry
1170
refs[ref] = (None, revid)
1171
return refs, main_ref, (stop_revno, stop_revision)
1173
def _update_refs(self, result, old_refs, new_refs, overwrite):
1174
mutter("updating refs. old refs: %r, new refs: %r",
1176
result.tag_updates = {}
1177
result.tag_conflicts = []
1178
ret = dict(old_refs)
1179
def ref_equals(refs, ref, git_sha, revid):
1184
if (value[0] is not None and
1185
git_sha is not None and
1186
value[0] == git_sha):
1188
if (value[1] is not None and
1189
revid is not None and
1192
# FIXME: If one side only has the git sha available and the other only
1193
# has the bzr revid, then this will cause us to show a tag as updated
1194
# that hasn't actually been updated.
1196
# FIXME: Check for diverged branches
1197
for ref, (git_sha, revid) in new_refs.iteritems():
1198
if ref_equals(ret, ref, git_sha, revid):
1199
# Already up to date
1201
git_sha = old_refs[ref][0]
1203
revid = old_refs[ref][1]
1204
ret[ref] = new_refs[ref] = (git_sha, revid)
1205
elif ref not in ret or overwrite:
1207
tag_name = ref_to_tag_name(ref)
1211
result.tag_updates[tag_name] = revid
1212
ret[ref] = (git_sha, revid)
1214
# FIXME: Check diverged
1218
name = ref_to_tag_name(ref)
1222
result.tag_conflicts.append((name, revid, ret[name][1]))
1224
ret[ref] = (git_sha, revid)
1227
def fetch(self, stop_revision=None, fetch_tags=None, lossy=False, limit=None):
1228
if stop_revision is None:
1229
stop_revision = self.source.last_revision()
1232
for k, v in self.source.tags.get_tag_dict().iteritems():
1233
ret.append((None, v))
1234
ret.append((None, stop_revision))
1236
self.interrepo.fetch_objects(ret, lossy=lossy, limit=limit)
1237
except NoPushSupport:
1238
raise errors.NoRoundtrippingSupport(self.source, self.target)
1240
def pull(self, overwrite=False, stop_revision=None, local=False,
1241
possible_transports=None, run_hooks=True):
1242
result = GitBranchPullResult()
1243
result.source_branch = self.source
1244
result.target_branch = self.target
1245
with self.source.lock_read(), self.target.lock_write():
1246
new_refs, main_ref, stop_revinfo = self._get_new_refs(
1248
def update_refs(old_refs):
1249
return self._update_refs(result, old_refs, new_refs, overwrite)
1251
result.revidmap, old_refs, new_refs = self.interrepo.fetch_refs(
1252
update_refs, lossy=False)
1253
except NoPushSupport:
1254
raise errors.NoRoundtrippingSupport(self.source, self.target)
1255
(old_sha1, result.old_revid) = old_refs.get(main_ref, (ZERO_SHA, NULL_REVISION))
1256
if result.old_revid is None:
1257
result.old_revid = self.target.lookup_foreign_revision_id(old_sha1)
1258
result.new_revid = new_refs[main_ref][1]
1259
result.local_branch = None
1260
result.master_branch = self.target
1262
for hook in branch.Branch.hooks['post_pull']:
1266
def push(self, overwrite=False, stop_revision=None, lossy=False,
1267
_override_hook_source_branch=None):
1268
result = GitBranchPushResult()
1269
result.source_branch = self.source
1270
result.target_branch = self.target
1271
result.local_branch = None
1272
result.master_branch = result.target_branch
1273
with self.source.lock_read(), self.target.lock_write():
1274
new_refs, main_ref, stop_revinfo = self._get_new_refs(stop_revision)
1275
def update_refs(old_refs):
1276
return self._update_refs(result, old_refs, new_refs, overwrite)
1278
result.revidmap, old_refs, new_refs = self.interrepo.fetch_refs(
1279
update_refs, lossy=lossy, overwrite=overwrite)
1280
except NoPushSupport:
1281
raise errors.NoRoundtrippingSupport(self.source, self.target)
1282
(old_sha1, result.old_revid) = old_refs.get(main_ref, (ZERO_SHA, NULL_REVISION))
1283
if result.old_revid is None:
1284
result.old_revid = self.target.lookup_foreign_revision_id(old_sha1)
1285
result.new_revid = new_refs[main_ref][1]
1286
(result.new_original_revno, result.new_original_revid) = stop_revinfo
1287
for hook in branch.Branch.hooks['post_push']:
1292
branch.InterBranch.register_optimiser(InterGitLocalGitBranch)
1293
branch.InterBranch.register_optimiser(InterFromGitBranch)
1294
branch.InterBranch.register_optimiser(InterToGitBranch)
1295
branch.InterBranch.register_optimiser(InterLocalGitRemoteGitBranch)