13
14
# You should have received a copy of the GNU General Public License
14
15
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
18
"""An adapter between a Git Branch and a Bazaar Branch"""
20
from __future__ import absolute_import
23
from io import BytesIO
24
from collections import defaultdict
26
from dulwich.config import (
27
ConfigFile as GitConfigFile,
31
from dulwich.objects import (
35
from dulwich.repo import check_ref_format
43
repository as _mod_repository,
26
from bzrlib.decorators import needs_read_lock
27
from bzrlib.trace import mutter
29
from bzrlib.plugins.git.foreign import ForeignBranch
30
from bzrlib.plugins.git.errors import LightWeightCheckoutsNotSupported
32
from dulwich.objects import (
37
class GitTagDict(tag.BasicTags):
50
from ..foreign import ForeignBranch
51
from ..revision import (
75
remote_refs_dict_to_tag_refs,
78
from .unpeel_map import (
87
def _calculate_revnos(branch):
88
if branch._format.stores_revno():
90
config = branch.get_config_stack()
91
return config.get('calculate_revnos')
94
class GitPullResult(branch.PullResult):
95
"""Result of a pull from a Git branch."""
97
def _lookup_revno(self, revid):
98
if not isinstance(revid, bytes):
99
raise TypeError(revid)
100
if not _calculate_revnos(self.target_branch):
102
# Try in source branch first, it'll be faster
103
with self.target_branch.lock_read():
104
return self.target_branch.revision_id_to_revno(revid)
108
return self._lookup_revno(self.old_revid)
112
return self._lookup_revno(self.new_revid)
115
class GitTags(tag.BasicTags):
116
"""Ref-based tag dictionary."""
39
118
def __init__(self, branch):
40
119
self.branch = branch
41
120
self.repository = branch.repository
122
def _merge_to_remote_git(self, target_repo, source_tag_refs,
127
def get_changed_refs(old_refs):
129
for ref_name, tag_name, peeled, unpeeled in (
130
source_tag_refs.iteritems()):
131
if old_refs.get(ref_name) == unpeeled:
133
elif overwrite or ref_name not in old_refs:
134
ret[ref_name] = unpeeled
135
updates[tag_name] = target_repo.lookup_foreign_revision_id(
140
self.repository.lookup_foreign_revision_id(peeled),
141
target_repo.lookup_foreign_revision_id(
142
old_refs[ref_name])))
144
target_repo.controldir.send_pack(
145
get_changed_refs, lambda have, want: [])
146
return updates, conflicts
148
def _merge_to_local_git(self, target_repo, source_tag_refs,
152
for ref_name, tag_name, peeled, unpeeled in source_tag_refs:
153
if target_repo._git.refs.get(ref_name) == unpeeled:
155
elif overwrite or ref_name not in target_repo._git.refs:
157
updates[tag_name] = (
158
target_repo.lookup_foreign_revision_id(peeled))
160
trace.warning('%s does not point to a valid object',
163
except NotCommitError:
164
trace.warning('%s points to a non-commit object',
167
target_repo._git.refs[ref_name] = unpeeled or peeled
170
source_revid = self.repository.lookup_foreign_revision_id(
172
target_revid = target_repo.lookup_foreign_revision_id(
173
target_repo._git.refs[ref_name])
175
trace.warning('%s does not point to a valid object',
178
except NotCommitError:
179
trace.warning('%s points to a non-commit object',
182
conflicts.append((tag_name, source_revid, target_revid))
183
return updates, conflicts
185
def _merge_to_git(self, to_tags, source_tag_refs, overwrite=False):
186
target_repo = to_tags.repository
187
if self.repository.has_same_location(target_repo):
190
if getattr(target_repo, "_git", None):
191
return self._merge_to_local_git(
192
target_repo, source_tag_refs, overwrite)
194
return self._merge_to_remote_git(
195
target_repo, source_tag_refs, overwrite)
197
to_tags.branch._tag_refs = None
199
def _merge_to_non_git(self, to_tags, source_tag_refs, overwrite=False):
200
unpeeled_map = defaultdict(set)
203
result = dict(to_tags.get_tag_dict())
204
for ref_name, tag_name, peeled, unpeeled in source_tag_refs:
205
if unpeeled is not None:
206
unpeeled_map[peeled].add(unpeeled)
208
bzr_revid = self.branch.lookup_foreign_revision_id(peeled)
209
except NotCommitError:
211
if result.get(tag_name) == bzr_revid:
213
elif tag_name not in result or overwrite:
214
result[tag_name] = bzr_revid
215
updates[tag_name] = bzr_revid
217
conflicts.append((tag_name, bzr_revid, result[tag_name]))
218
to_tags._set_tag_dict(result)
219
if len(unpeeled_map) > 0:
220
map_file = UnpeelMap.from_repository(to_tags.branch.repository)
221
map_file.update(unpeeled_map)
222
map_file.save_in_repository(to_tags.branch.repository)
223
return updates, conflicts
225
def merge_to(self, to_tags, overwrite=False, ignore_master=False,
226
source_tag_refs=None):
227
"""See Tags.merge_to."""
228
if source_tag_refs is None:
229
source_tag_refs = self.branch.get_tag_refs()
232
if isinstance(to_tags, GitTags):
233
return self._merge_to_git(to_tags, source_tag_refs,
239
master = to_tags.branch.get_master_branch()
240
with contextlib.ExitStack() as es:
241
if master is not None:
242
es.enter_context(master.lock_write())
243
updates, conflicts = self._merge_to_non_git(
244
to_tags, source_tag_refs, overwrite=overwrite)
245
if master is not None:
246
extra_updates, extra_conflicts = self.merge_to(
247
master.tags, overwrite=overwrite,
248
source_tag_refs=source_tag_refs,
249
ignore_master=ignore_master)
250
updates.update(extra_updates)
251
conflicts += extra_conflicts
252
return updates, conflicts
43
254
def get_tag_dict(self):
45
for k,v in self.repository._git.tags.iteritems():
46
obj = self.repository._git.get_object(v)
47
while isinstance(obj, Tag):
49
obj = self.repository._git.get_object(v)
50
if not isinstance(obj, Commit):
51
mutter("Tag %s points at object %r that is not a commit, ignoring", k, obj)
256
for (ref_name, tag_name, peeled, unpeeled) in (
257
self.branch.get_tag_refs()):
259
bzr_revid = self.branch.lookup_foreign_revision_id(peeled)
260
except NotCommitError:
53
ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
263
ret[tag_name] = bzr_revid
267
class LocalGitTagDict(GitTags):
268
"""Dictionary with tags in a local repository."""
270
def __init__(self, branch):
271
super(LocalGitTagDict, self).__init__(branch)
272
self.refs = self.repository.controldir._git.refs
274
def _set_tag_dict(self, to_dict):
275
extra = set(self.refs.allkeys())
276
for k, revid in to_dict.items():
277
name = tag_name_to_ref(k)
281
self.set_tag(k, revid)
282
except errors.GhostTagsNotSupported:
286
del self.repository._git[name]
56
288
def set_tag(self, name, revid):
57
self.repository._git.tags[name] = revid
60
class GitBranchConfig(config.BranchConfig):
61
"""BranchConfig that uses locations.conf in place of branch.conf"""
63
def __init__(self, branch):
64
config.BranchConfig.__init__(self, branch)
65
# do not provide a BranchDataConfig
66
self.option_sources = self.option_sources[0], self.option_sources[2]
68
def set_user_option(self, name, value, store=config.STORE_BRANCH, warn_masked=False):
69
"""Force local to True"""
70
config.BranchConfig.set_user_option(self, name, value, store=config.STORE_LOCATION, warn_masked=warn_masked)
290
git_sha, mapping = self.branch.lookup_bzr_revision_id(revid)
291
except errors.NoSuchRevision:
292
raise errors.GhostTagsNotSupported(self)
293
self.refs[tag_name_to_ref(name)] = git_sha
294
self.branch._tag_refs = None
296
def delete_tag(self, name):
297
ref = tag_name_to_ref(name)
298
if ref not in self.refs:
299
raise errors.NoSuchTag(name)
301
self.branch._tag_refs = None
73
304
class GitBranchFormat(branch.BranchFormat):
306
def network_name(self):
309
def supports_tags(self):
312
def supports_leaving_lock(self):
315
def supports_tags_referencing_ghosts(self):
318
def tags_are_versioned(self):
321
def get_foreign_tests_branch_factory(self):
322
from .tests.test_branch import ForeignTestsBranchFactory
323
return ForeignTestsBranchFactory()
325
def make_tags(self, branch):
328
except AttributeError:
330
if getattr(branch.repository, "_git", None) is None:
331
from .remote import RemoteGitTagDict
332
return RemoteGitTagDict(branch)
334
return LocalGitTagDict(branch)
336
def initialize(self, a_controldir, name=None, repository=None,
337
append_revisions_only=None):
338
raise NotImplementedError(self.initialize)
340
def get_reference(self, controldir, name=None):
341
return controldir.get_branch_reference(name=name)
343
def set_reference(self, controldir, name, target):
344
return controldir.set_branch_reference(target, name)
346
def stores_revno(self):
347
"""True if this branch format store revision numbers."""
350
supports_reference_locations = False
353
class LocalGitBranchFormat(GitBranchFormat):
75
355
def get_format_description(self):
78
def supports_tags(self):
356
return 'Local Git Branch'
359
def _matchingcontroldir(self):
360
from .dir import LocalGitControlDirFormat
361
return LocalGitControlDirFormat()
363
def initialize(self, a_controldir, name=None, repository=None,
364
append_revisions_only=None):
365
from .dir import LocalGitDir
366
if not isinstance(a_controldir, LocalGitDir):
367
raise errors.IncompatibleFormat(self, a_controldir._format)
368
return a_controldir.create_branch(
369
repository=repository, name=name,
370
append_revisions_only=append_revisions_only)
82
373
class GitBranch(ForeignBranch):
83
374
"""An adapter to git repositories for bzr Branch objects."""
85
def __init__(self, bzrdir, repository, name, head, lockfiles):
377
def control_transport(self):
378
return self._control_transport
381
def user_transport(self):
382
return self._user_transport
384
def __init__(self, controldir, repository, ref, format):
86
385
self.repository = repository
386
self._format = format
387
self.controldir = controldir
388
self._lock_mode = None
87
390
super(GitBranch, self).__init__(repository.get_mapping())
88
self.control_files = lockfiles
92
self.base = bzrdir.transport.base
93
self._format = GitBranchFormat()
95
def dpull(self, source, stop_revision=None):
96
if stop_revision is None:
97
stop_revision = source.last_revision()
98
# FIXME: Check for diverged branches
99
revidmap = self.repository.dfetch(source.repository, stop_revision)
100
self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(revidmap[stop_revision])
103
def lock_write(self):
104
self.control_files.lock_write()
393
self._user_transport = controldir.user_transport.clone('.')
394
self._control_transport = controldir.control_transport.clone('.')
395
self._tag_refs = None
398
self.name = ref_to_branch_name(ref)
401
if self.ref is not None:
402
params = {"ref": urlutils.escape(self.ref)}
405
params = {"branch": urlutils.escape(self.name)}
406
for k, v in params.items():
407
self._user_transport.set_segment_parameter(k, v)
408
self._control_transport.set_segment_parameter(k, v)
409
self.base = controldir.user_transport.base
411
def _get_checkout_format(self, lightweight=False):
412
"""Return the most suitable metadir for a checkout of this branch.
413
Weaves are used if this branch's repository uses weaves.
416
return controldir.format_registry.make_controldir("git")
418
return controldir.format_registry.make_controldir("default")
420
def get_child_submit_format(self):
421
"""Return the preferred format of submissions to this branch."""
422
ret = self.get_config_stack().get("child_submit_format")
427
def get_config(self):
428
return GitBranchConfig(self)
430
def get_config_stack(self):
431
return GitBranchStack(self)
433
def _get_nick(self, local=False, possible_master_transports=None):
434
"""Find the nick name for this branch.
438
if getattr(self.repository, '_git', None):
439
cs = self.repository._git.get_config_stack()
441
return cs.get((b"branch", self.name.encode('utf-8')),
442
b"nick").decode("utf-8")
445
return self.name or u"HEAD"
447
def _set_nick(self, nick):
448
cf = self.repository._git.get_config()
449
cf.set((b"branch", self.name.encode('utf-8')),
450
b"nick", nick.encode("utf-8"))
453
self.repository._git._put_named_file('config', f.getvalue())
455
nick = property(_get_nick, _set_nick)
458
return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
461
def generate_revision_history(self, revid, last_rev=None,
463
if last_rev is not None:
464
graph = self.repository.get_graph()
465
if not graph.is_ancestor(last_rev, revid):
466
# our previous tip is not merged into stop_revision
467
raise errors.DivergedBranches(self, other_branch)
469
self.set_last_revision(revid)
471
def lock_write(self, token=None):
472
if token is not None:
473
raise errors.TokenLockingNotSupported(self)
475
if self._lock_mode == 'r':
476
raise errors.ReadOnlyError(self)
477
self._lock_count += 1
480
self._lock_mode = 'w'
482
self.repository.lock_write()
483
return lock.LogicalLockResult(self.unlock)
485
def leave_lock_in_place(self):
486
raise NotImplementedError(self.leave_lock_in_place)
488
def dont_leave_lock_in_place(self):
489
raise NotImplementedError(self.dont_leave_lock_in_place)
106
491
def get_stacked_on_url(self):
107
492
# Git doesn't do stacking (yet...)
110
def get_parent(self):
493
raise branch.UnstackableBranchFormat(self._format, self.base)
495
def _get_push_origin(self, cs):
496
"""Get the name for the push origin.
498
The exact behaviour is documented in the git-config(1) manpage.
501
return cs.get((b'branch', self.name.encode('utf-8')), b'pushRemote')
504
return cs.get((b'branch', ), b'remote')
507
return cs.get((b'branch', self.name.encode('utf-8')), b'remote')
511
def _get_origin(self, cs):
513
return cs.get((b'branch', self.name.encode('utf-8')), b'remote')
517
def _get_related_push_branch(self, cs):
518
remote = self._get_push_origin(cs)
520
location = cs.get((b"remote", remote), b"url")
524
return git_url_to_bzr_url(location.decode('utf-8'), ref=self.ref)
526
def _get_related_merge_branch(self, cs):
527
remote = self._get_origin(cs)
529
location = cs.get((b"remote", remote), b"url")
534
ref = cs.get((b"branch", remote), b"merge")
538
return git_url_to_bzr_url(location.decode('utf-8'), ref=ref)
540
def _get_parent_location(self):
111
541
"""See Branch.get_parent()."""
114
def set_parent(self, url):
542
cs = self.repository._git.get_config_stack()
543
return self._get_related_merge_branch(cs)
545
def _write_git_config(self, cs):
548
self.repository._git._put_named_file('config', f.getvalue())
550
def set_parent(self, location):
551
cs = self.repository._git.get_config()
552
remote = self._get_origin(cs)
553
this_url = urlutils.strip_segment_parameters(self.user_url)
554
target_url, branch, ref = bzr_url_to_git_url(location)
555
location = urlutils.relative_url(this_url, target_url)
556
cs.set((b"remote", remote), b"url", location)
558
cs.set((b"branch", remote), b"merge", branch_name_to_ref(branch))
560
cs.set((b"branch", remote), b"merge", ref)
562
# TODO(jelmer): Maybe unset rather than setting to HEAD?
563
cs.set((b"branch", remote), b"merge", b'HEAD')
564
self._write_git_config(cs)
566
def break_lock(self):
567
raise NotImplementedError(self.break_lock)
117
569
def lock_read(self):
118
self.control_files.lock_read()
571
if self._lock_mode not in ('r', 'w'):
572
raise ValueError(self._lock_mode)
573
self._lock_count += 1
575
self._lock_mode = 'r'
577
self.repository.lock_read()
578
return lock.LogicalLockResult(self.unlock)
580
def peek_lock_mode(self):
581
return self._lock_mode
584
return (self._lock_mode is not None)
589
def _unlock_ref(self):
120
592
def unlock(self):
121
self.control_files.unlock()
593
"""See Branch.unlock()."""
594
if self._lock_count == 0:
595
raise errors.LockNotHeld(self)
597
self._lock_count -= 1
598
if self._lock_count == 0:
599
if self._lock_mode == 'w':
601
self._lock_mode = None
602
self._clear_cached_state()
604
self.repository.unlock()
123
606
def get_physical_lock_status(self):
609
def last_revision(self):
610
with self.lock_read():
611
# perhaps should escape this ?
612
if self.head is None:
613
return revision.NULL_REVISION
614
return self.lookup_foreign_revision_id(self.head)
616
def _basic_push(self, target, overwrite=False, stop_revision=None):
617
return branch.InterBranch.get(self, target)._basic_push(
618
overwrite, stop_revision)
620
def lookup_foreign_revision_id(self, foreign_revid):
622
return self.repository.lookup_foreign_revision_id(foreign_revid,
626
return self.mapping.revision_id_foreign_to_bzr(foreign_revid)
628
def lookup_bzr_revision_id(self, revid):
629
return self.repository.lookup_bzr_revision_id(
630
revid, mapping=self.mapping)
632
def get_unshelver(self, tree):
633
raise errors.StoringUncommittedNotSupported(self)
635
def _clear_cached_state(self):
636
super(GitBranch, self)._clear_cached_state()
637
self._tag_refs = None
639
def _iter_tag_refs(self, refs):
640
"""Iterate over the tag refs.
642
:param refs: Refs dictionary (name -> git sha1)
643
:return: iterator over (ref_name, tag_name, peeled_sha1, unpeeled_sha1)
645
raise NotImplementedError(self._iter_tag_refs)
647
def get_tag_refs(self):
648
with self.lock_read():
649
if self._tag_refs is None:
650
self._tag_refs = list(self._iter_tag_refs())
651
return self._tag_refs
653
def import_last_revision_info_and_tags(self, source, revno, revid,
655
"""Set the last revision info, importing from another repo if necessary.
657
This is used by the bound branch code to upload a revision to
658
the master branch first before updating the tip of the local branch.
659
Revisions referenced by source's tags are also transferred.
661
:param source: Source branch to optionally fetch from
662
:param revno: Revision number of the new tip
663
:param revid: Revision id of the new tip
664
:param lossy: Whether to discard metadata that can not be
666
:return: Tuple with the new revision number and revision id
667
(should only be different from the arguments when lossy=True)
669
push_result = source.push(
670
self, stop_revision=revid, lossy=lossy, _stop_revno=revno)
671
return (push_result.new_revno, push_result.new_revid)
673
def reconcile(self, thorough=True):
674
"""Make sure the data stored in this branch is consistent."""
675
from ..reconcile import ReconcileResult
677
return ReconcileResult()
127
680
class LocalGitBranch(GitBranch):
130
def last_revision(self):
131
# perhaps should escape this ?
132
if self.head is None:
133
return revision.NULL_REVISION
134
return self.mapping.revision_id_foreign_to_bzr(self.head)
136
def create_checkout(self, to_location, revision_id=None,
137
lightweight=False, accelerator_tree=None, hardlink=False):
681
"""A local Git branch."""
683
def __init__(self, controldir, repository, ref):
684
super(LocalGitBranch, self).__init__(controldir, repository, ref,
685
LocalGitBranchFormat())
687
def create_checkout(self, to_location, revision_id=None, lightweight=False,
688
accelerator_tree=None, hardlink=False):
689
t = transport.get_transport(to_location)
691
format = self._get_checkout_format(lightweight=lightweight)
692
checkout = format.initialize_on_transport(t)
139
raise LightWeightCheckoutsNotSupported()
140
return self._create_heavyweight_checkout(to_location, revision_id, hardlink)
142
def _create_heavyweight_checkout(self, to_location, revision_id=None,
144
"""Create a new heavyweight checkout of this branch.
146
:param to_location: URL of location to create the new checkout in.
147
:param revision_id: Revision that should be the tip of the checkout.
148
:param hardlink: Whether to hardlink
149
:return: WorkingTree object of checkout.
151
checkout_branch = BzrDir.create_branch_convenience(
152
to_location, force_new_tree=False, format=get_rich_root_format())
153
checkout = checkout_branch.bzrdir
154
checkout_branch.bind(self)
155
# pull up to the specified revision_id to set the initial
156
# branch tip correctly, and seed it with history.
157
checkout_branch.pull(self, stop_revision=revision_id)
158
return checkout.create_workingtree(revision_id, hardlink=hardlink)
160
def _make_tags(self):
161
return GitTagDict(self)
694
from_branch = checkout.set_branch_reference(target_branch=self)
696
policy = checkout.determine_repository_policy()
697
policy.acquire_repository()
698
checkout_branch = checkout.create_branch()
699
checkout_branch.bind(self)
700
checkout_branch.pull(self, stop_revision=revision_id)
702
return checkout.create_workingtree(
703
revision_id, from_branch=from_branch, hardlink=hardlink)
706
self._ref_lock = self.repository._git.refs.lock_ref(self.ref)
708
def _unlock_ref(self):
709
self._ref_lock.unlock()
711
def break_lock(self):
712
self.repository._git.refs.unlock_ref(self.ref)
163
714
def _gen_revision_history(self):
164
715
if self.head is None:
166
ret = list(self.repository.iter_reverse_revision_history(self.last_revision()))
717
last_revid = self.last_revision()
718
graph = self.repository.get_graph()
720
ret = list(graph.iter_lefthand_ancestry(
721
last_revid, (revision.NULL_REVISION, )))
722
except errors.RevisionNotPresent as e:
723
raise errors.GhostRevisionsHaveNoRevno(last_revid, e.revision_id)
170
def get_config(self):
171
return GitBranchConfig(self)
729
return self.repository._git.refs[self.ref]
733
def _read_last_revision_info(self):
734
last_revid = self.last_revision()
735
graph = self.repository.get_graph()
737
revno = graph.find_distance_to_null(
738
last_revid, [(revision.NULL_REVISION, 0)])
739
except errors.GhostRevisionsHaveNoRevno:
741
return revno, last_revid
743
def set_last_revision_info(self, revno, revision_id):
744
self.set_last_revision(revision_id)
745
self._last_revision_info_cache = revno, revision_id
747
def set_last_revision(self, revid):
748
if not revid or not isinstance(revid, bytes):
749
raise errors.InvalidRevisionId(revision_id=revid, branch=self)
750
if revid == NULL_REVISION:
753
(newhead, self.mapping) = self.repository.lookup_bzr_revision_id(
755
if self.mapping is None:
757
self._set_head(newhead)
759
def _set_head(self, value):
760
if value == ZERO_SHA:
761
raise ValueError(value)
764
del self.repository._git.refs[self.ref]
766
self.repository._git.refs[self.ref] = self._head
767
self._clear_cached_state()
769
head = property(_get_head, _set_head)
173
771
def get_push_location(self):
174
772
"""See Branch.get_push_location."""
175
push_loc = self.get_config().get_user_option('push_location')
773
push_loc = self.get_config_stack().get('push_location')
774
if push_loc is not None:
776
cs = self.repository._git.get_config_stack()
777
return self._get_related_push_branch(cs)
178
779
def set_push_location(self, location):
179
780
"""See Branch.set_push_location."""
183
784
def supports_tags(self):
186
def sprout(self, to_bzrdir, revision_id=None):
187
"""See Branch.sprout()."""
188
result = to_bzrdir.create_branch()
189
self.copy_content_into(result, revision_id=revision_id)
190
result.set_parent(self.bzrdir.root_transport.base)
194
class InterGitGenericBranch(branch.InterBranch):
197
def is_compatible(self, source, target):
198
return isinstance(source, GitBranch)
200
def update_revisions(self, stop_revision=None, overwrite=False,
202
"""See InterBranch.update_revisions()."""
203
# TODO: stop_revision, overwrite
204
interrepo = repository.InterRepository.get(self.source.repository, self.target.repository)
205
self._last_revid = None
787
def store_uncommitted(self, creator):
788
raise errors.StoringUncommittedNotSupported(self)
790
def _iter_tag_refs(self):
791
"""Iterate over the tag refs.
793
:param refs: Refs dictionary (name -> git sha1)
794
:return: iterator over (ref_name, tag_name, peeled_sha1, unpeeled_sha1)
796
refs = self.repository.controldir.get_refs_container()
797
for ref_name, unpeeled in refs.as_dict().items():
799
tag_name = ref_to_tag_name(ref_name)
800
except (ValueError, UnicodeDecodeError):
802
peeled = refs.get_peeled(ref_name)
805
if not isinstance(tag_name, str):
806
raise TypeError(tag_name)
807
yield (ref_name, tag_name, peeled, unpeeled)
809
def create_memorytree(self):
810
from .memorytree import GitMemoryTree
811
return GitMemoryTree(self, self.repository._git.object_store,
815
def _quick_lookup_revno(local_branch, remote_branch, revid):
816
if not isinstance(revid, bytes):
817
raise TypeError(revid)
818
# Try in source branch first, it'll be faster
819
with local_branch.lock_read():
820
if not _calculate_revnos(local_branch):
823
return local_branch.revision_id_to_revno(revid)
824
except errors.NoSuchRevision:
825
graph = local_branch.repository.get_graph()
827
return graph.find_distance_to_null(
828
revid, [(revision.NULL_REVISION, 0)])
829
except errors.GhostRevisionsHaveNoRevno:
830
if not _calculate_revnos(remote_branch):
832
# FIXME: Check using graph.find_distance_to_null() ?
833
with remote_branch.lock_read():
834
return remote_branch.revision_id_to_revno(revid)
837
class GitBranchPullResult(branch.PullResult):
840
super(GitBranchPullResult, self).__init__()
841
self.new_git_head = None
842
self._old_revno = None
843
self._new_revno = None
845
def report(self, to_file):
847
if self.old_revid == self.new_revid:
848
to_file.write('No revisions to pull.\n')
849
elif self.new_git_head is not None:
850
to_file.write('Now on revision %d (git sha: %s).\n' %
851
(self.new_revno, self.new_git_head))
853
to_file.write('Now on revision %d.\n' % (self.new_revno,))
854
self._show_tag_conficts(to_file)
856
def _lookup_revno(self, revid):
857
return _quick_lookup_revno(self.target_branch, self.source_branch,
860
def _get_old_revno(self):
861
if self._old_revno is not None:
862
return self._old_revno
863
return self._lookup_revno(self.old_revid)
865
def _set_old_revno(self, revno):
866
self._old_revno = revno
868
old_revno = property(_get_old_revno, _set_old_revno)
870
def _get_new_revno(self):
871
if self._new_revno is not None:
872
return self._new_revno
873
return self._lookup_revno(self.new_revid)
875
def _set_new_revno(self, revno):
876
self._new_revno = revno
878
new_revno = property(_get_new_revno, _set_new_revno)
881
class GitBranchPushResult(branch.BranchPushResult):
883
def _lookup_revno(self, revid):
884
return _quick_lookup_revno(self.source_branch, self.target_branch,
889
return self._lookup_revno(self.old_revid)
893
new_original_revno = getattr(self, "new_original_revno", None)
894
if new_original_revno:
895
return new_original_revno
896
if getattr(self, "new_original_revid", None) is not None:
897
return self._lookup_revno(self.new_original_revid)
898
return self._lookup_revno(self.new_revid)
901
class InterFromGitBranch(branch.GenericInterBranch):
902
"""InterBranch implementation that pulls from Git into bzr."""
905
def _get_branch_formats_to_test():
907
default_format = branch.format_registry.get_default()
908
except AttributeError:
909
default_format = branch.BranchFormat._default_format
910
from .remote import RemoteGitBranchFormat
912
(RemoteGitBranchFormat(), default_format),
913
(LocalGitBranchFormat(), default_format)]
916
def _get_interrepo(self, source, target):
917
return _mod_repository.InterRepository.get(
918
source.repository, target.repository)
921
def is_compatible(cls, source, target):
922
if not isinstance(source, GitBranch):
924
if isinstance(target, GitBranch):
925
# InterLocalGitRemoteGitBranch or InterToGitBranch should be used
927
if (getattr(cls._get_interrepo(source, target), "fetch_objects", None)
929
# fetch_objects is necessary for this to work
933
def fetch(self, stop_revision=None, fetch_tags=None, limit=None, lossy=False):
935
stop_revision, fetch_tags=fetch_tags, limit=limit, lossy=lossy)
936
return _mod_repository.FetchResult()
938
def fetch_objects(self, stop_revision, fetch_tags, limit=None, lossy=False):
939
interrepo = self._get_interrepo(self.source, self.target)
940
if fetch_tags is None:
941
c = self.source.get_config_stack()
942
fetch_tags = c.get('branch.fetch_tags')
206
944
def determine_wants(heads):
207
if not self.source.name in heads:
208
raise BzrError("No such remote branch '%s', found: %r" % (
209
self.source.name, heads.keys()))
210
head = heads[self.source.name]
211
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
212
if self.target.repository.has_revision(self._last_revid):
215
interrepo.fetch_objects(determine_wants, self.source.mapping)
216
self.target.generate_revision_history(self._last_revid)
219
branch.InterBranch.register_optimiser(InterGitGenericBranch)
945
if stop_revision is None:
947
head = heads[self.source.ref]
949
self._last_revid = revision.NULL_REVISION
951
self._last_revid = self.source.lookup_foreign_revision_id(
954
self._last_revid = stop_revision
955
real = interrepo.get_determine_wants_revids(
956
[self._last_revid], include_tags=fetch_tags)
958
pack_hint, head, refs = interrepo.fetch_objects(
959
determine_wants, self.source.mapping, limit=limit,
961
if (pack_hint is not None and
962
self.target.repository._format.pack_compresses):
963
self.target.repository.pack(hint=pack_hint)
966
def _update_revisions(self, stop_revision=None, overwrite=False):
967
head, refs = self.fetch_objects(stop_revision, fetch_tags=None)
969
prev_last_revid = None
971
prev_last_revid = self.target.last_revision()
972
self.target.generate_revision_history(
973
self._last_revid, last_rev=prev_last_revid,
974
other_branch=self.source)
977
def update_references(self, revid=None):
979
revid = self.target.last_revision()
980
tree = self.target.repository.revision_tree(revid)
982
with tree.get_file('.gitmodules') as f:
983
for path, url, section in parse_submodules(
984
GitConfigFile.from_file(f)):
985
self.target.set_reference_info(
986
tree.path2id(path.decode('utf-8')), url.decode('utf-8'),
987
path.decode('utf-8'))
988
except errors.NoSuchFile:
991
def _basic_pull(self, stop_revision, overwrite, run_hooks,
992
_override_hook_target, _hook_master):
993
if overwrite is True:
994
overwrite = set(["history", "tags"])
997
result = GitBranchPullResult()
998
result.source_branch = self.source
999
if _override_hook_target is None:
1000
result.target_branch = self.target
1002
result.target_branch = _override_hook_target
1003
with self.target.lock_write(), self.source.lock_read():
1004
# We assume that during 'pull' the target repository is closer than
1006
(result.old_revno, result.old_revid) = \
1007
self.target.last_revision_info()
1008
result.new_git_head, remote_refs = self._update_revisions(
1009
stop_revision, overwrite=("history" in overwrite))
1010
tags_ret = self.source.tags.merge_to(
1011
self.target.tags, ("tags" in overwrite), ignore_master=True)
1012
if isinstance(tags_ret, tuple):
1013
result.tag_updates, result.tag_conflicts = tags_ret
1015
result.tag_conflicts = tags_ret
1016
(result.new_revno, result.new_revid) = \
1017
self.target.last_revision_info()
1018
self.update_references(revid=result.new_revid)
1020
result.master_branch = _hook_master
1021
result.local_branch = result.target_branch
1023
result.master_branch = result.target_branch
1024
result.local_branch = None
1026
for hook in branch.Branch.hooks['post_pull']:
1030
def pull(self, overwrite=False, stop_revision=None,
1031
possible_transports=None, _hook_master=None, run_hooks=True,
1032
_override_hook_target=None, local=False):
1035
:param _hook_master: Private parameter - set the branch to
1036
be supplied as the master to pull hooks.
1037
:param run_hooks: Private parameter - if false, this branch
1038
is being called because it's the master of the primary branch,
1039
so it should not run its hooks.
1040
:param _override_hook_target: Private parameter - set the branch to be
1041
supplied as the target_branch to pull hooks.
1043
# This type of branch can't be bound.
1044
bound_location = self.target.get_bound_location()
1045
if local and not bound_location:
1046
raise errors.LocalRequiresBoundBranch()
1047
source_is_master = False
1048
with contextlib.ExitStack() as es:
1049
es.enter_context(self.source.lock_read())
1051
# bound_location comes from a config file, some care has to be
1052
# taken to relate it to source.user_url
1053
normalized = urlutils.normalize_url(bound_location)
1055
relpath = self.source.user_transport.relpath(normalized)
1056
source_is_master = (relpath == '')
1057
except (errors.PathNotChild, urlutils.InvalidURL):
1058
source_is_master = False
1059
if not local and bound_location and not source_is_master:
1060
# not pulling from master, so we need to update master.
1061
master_branch = self.target.get_master_branch(possible_transports)
1062
es.enter_context(master_branch.lock_write())
1063
# pull from source into master.
1064
master_branch.pull(self.source, overwrite, stop_revision,
1067
master_branch = None
1068
return self._basic_pull(stop_revision, overwrite, run_hooks,
1069
_override_hook_target,
1070
_hook_master=master_branch)
1072
def _basic_push(self, overwrite, stop_revision):
1073
if overwrite is True:
1074
overwrite = set(["history", "tags"])
1077
result = branch.BranchPushResult()
1078
result.source_branch = self.source
1079
result.target_branch = self.target
1080
result.old_revno, result.old_revid = self.target.last_revision_info()
1081
result.new_git_head, remote_refs = self._update_revisions(
1082
stop_revision, overwrite=("history" in overwrite))
1083
tags_ret = self.source.tags.merge_to(
1084
self.target.tags, "tags" in overwrite, ignore_master=True)
1085
(result.tag_updates, result.tag_conflicts) = tags_ret
1086
result.new_revno, result.new_revid = self.target.last_revision_info()
1087
self.update_references(revid=result.new_revid)
1091
class InterGitBranch(branch.GenericInterBranch):
1092
"""InterBranch implementation that pulls between Git branches."""
1094
def fetch(self, stop_revision=None, fetch_tags=None, limit=None, lossy=False):
1095
raise NotImplementedError(self.fetch)
1098
class InterLocalGitRemoteGitBranch(InterGitBranch):
1099
"""InterBranch that copies from a local to a remote git branch."""
1102
def _get_branch_formats_to_test():
1103
from .remote import RemoteGitBranchFormat
1105
(LocalGitBranchFormat(), RemoteGitBranchFormat())]
1108
def is_compatible(self, source, target):
1109
from .remote import RemoteGitBranch
1110
return (isinstance(source, LocalGitBranch) and
1111
isinstance(target, RemoteGitBranch))
1113
def _basic_push(self, overwrite, stop_revision):
1114
result = GitBranchPushResult()
1115
result.source_branch = self.source
1116
result.target_branch = self.target
1117
if stop_revision is None:
1118
stop_revision = self.source.last_revision()
1120
def get_changed_refs(old_refs):
1121
old_ref = old_refs.get(self.target.ref, None)
1123
result.old_revid = revision.NULL_REVISION
1125
result.old_revid = self.target.lookup_foreign_revision_id(
1127
new_ref = self.source.repository.lookup_bzr_revision_id(
1130
if remote_divergence(
1132
self.source.repository._git.object_store):
1133
raise errors.DivergedBranches(self.source, self.target)
1134
refs = {self.target.ref: new_ref}
1135
result.new_revid = stop_revision
1137
self.source.repository._git.refs.as_dict(b"refs/tags").items()):
1138
if sha not in self.source.repository._git:
1139
trace.mutter('Ignoring missing SHA: %s', sha)
1141
refs[tag_name_to_ref(name)] = sha
1143
self.target.repository.send_pack(
1145
self.source.repository._git.object_store.generate_pack_data)
1149
class InterGitLocalGitBranch(InterGitBranch):
1150
"""InterBranch that copies from a remote to a local git branch."""
1153
def _get_branch_formats_to_test():
1154
from .remote import RemoteGitBranchFormat
1156
(RemoteGitBranchFormat(), LocalGitBranchFormat()),
1157
(LocalGitBranchFormat(), LocalGitBranchFormat())]
1160
def is_compatible(self, source, target):
1161
return (isinstance(source, GitBranch) and
1162
isinstance(target, LocalGitBranch))
1164
def fetch(self, stop_revision=None, fetch_tags=None, limit=None, lossy=False):
1165
interrepo = _mod_repository.InterRepository.get(
1166
self.source.repository, self.target.repository)
1167
if stop_revision is None:
1168
stop_revision = self.source.last_revision()
1169
if fetch_tags is None:
1170
c = self.source.get_config_stack()
1171
fetch_tags = c.get('branch.fetch_tags')
1172
determine_wants = interrepo.get_determine_wants_revids(
1173
[stop_revision], include_tags=fetch_tags)
1174
interrepo.fetch_objects(determine_wants, limit=limit, lossy=lossy)
1175
return _mod_repository.FetchResult()
1177
def _basic_push(self, overwrite=False, stop_revision=None):
1178
if overwrite is True:
1179
overwrite = set(["history", "tags"])
1182
result = GitBranchPushResult()
1183
result.source_branch = self.source
1184
result.target_branch = self.target
1185
result.old_revid = self.target.last_revision()
1186
refs, stop_revision = self.update_refs(stop_revision)
1187
self.target.generate_revision_history(
1189
(result.old_revid if ("history" not in overwrite) else None),
1190
other_branch=self.source)
1191
tags_ret = self.source.tags.merge_to(
1193
source_tag_refs=remote_refs_dict_to_tag_refs(refs),
1194
overwrite=("tags" in overwrite))
1195
if isinstance(tags_ret, tuple):
1196
(result.tag_updates, result.tag_conflicts) = tags_ret
1198
result.tag_conflicts = tags_ret
1199
result.new_revid = self.target.last_revision()
1202
def update_refs(self, stop_revision=None):
1203
interrepo = _mod_repository.InterRepository.get(
1204
self.source.repository, self.target.repository)
1205
c = self.source.get_config_stack()
1206
fetch_tags = c.get('branch.fetch_tags')
1208
if stop_revision is None:
1209
result = interrepo.fetch(branches=[self.source.ref], include_tags=fetch_tags)
1211
head = result.refs[self.source.ref]
1213
stop_revision = revision.NULL_REVISION
1215
stop_revision = self.target.lookup_foreign_revision_id(head)
1217
result = interrepo.fetch(
1218
revision_id=stop_revision, include_tags=fetch_tags)
1219
return result.refs, stop_revision
1221
def pull(self, stop_revision=None, overwrite=False,
1222
possible_transports=None, run_hooks=True, local=False):
1223
# This type of branch can't be bound.
1225
raise errors.LocalRequiresBoundBranch()
1226
if overwrite is True:
1227
overwrite = set(["history", "tags"])
1231
result = GitPullResult()
1232
result.source_branch = self.source
1233
result.target_branch = self.target
1234
with self.target.lock_write(), self.source.lock_read():
1235
result.old_revid = self.target.last_revision()
1236
refs, stop_revision = self.update_refs(stop_revision)
1237
self.target.generate_revision_history(
1239
(result.old_revid if ("history" not in overwrite) else None),
1240
other_branch=self.source)
1241
tags_ret = self.source.tags.merge_to(
1242
self.target.tags, overwrite=("tags" in overwrite),
1243
source_tag_refs=remote_refs_dict_to_tag_refs(refs))
1244
if isinstance(tags_ret, tuple):
1245
(result.tag_updates, result.tag_conflicts) = tags_ret
1247
result.tag_conflicts = tags_ret
1248
result.new_revid = self.target.last_revision()
1249
result.local_branch = None
1250
result.master_branch = result.target_branch
1252
for hook in branch.Branch.hooks['post_pull']:
1257
class InterToGitBranch(branch.GenericInterBranch):
1258
"""InterBranch implementation that pulls into a Git branch."""
1260
def __init__(self, source, target):
1261
super(InterToGitBranch, self).__init__(source, target)
1262
self.interrepo = _mod_repository.InterRepository.get(source.repository,
1266
def _get_branch_formats_to_test():
1268
default_format = branch.format_registry.get_default()
1269
except AttributeError:
1270
default_format = branch.BranchFormat._default_format
1271
from .remote import RemoteGitBranchFormat
1273
(default_format, LocalGitBranchFormat()),
1274
(default_format, RemoteGitBranchFormat())]
1277
def is_compatible(self, source, target):
1278
return (not isinstance(source, GitBranch) and
1279
isinstance(target, GitBranch))
1281
def _get_new_refs(self, stop_revision=None, fetch_tags=None,
1283
if not self.source.is_locked():
1284
raise errors.ObjectNotLocked(self.source)
1285
if stop_revision is None:
1286
(stop_revno, stop_revision) = self.source.last_revision_info()
1287
elif stop_revno is None:
1289
stop_revno = self.source.revision_id_to_revno(stop_revision)
1290
except errors.NoSuchRevision:
1292
if not isinstance(stop_revision, bytes):
1293
raise TypeError(stop_revision)
1294
main_ref = self.target.ref
1295
refs = {main_ref: (None, stop_revision)}
1296
if fetch_tags is None:
1297
c = self.source.get_config_stack()
1298
fetch_tags = c.get('branch.fetch_tags')
1299
for name, revid in self.source.tags.get_tag_dict().items():
1300
if self.source.repository.has_revision(revid):
1301
ref = tag_name_to_ref(name)
1302
if not check_ref_format(ref):
1303
warning("skipping tag with invalid characters %s (%s)",
1307
# FIXME: Skip tags that are not in the ancestry
1308
refs[ref] = (None, revid)
1309
return refs, main_ref, (stop_revno, stop_revision)
1311
def _update_refs(self, result, old_refs, new_refs, overwrite):
1312
mutter("updating refs. old refs: %r, new refs: %r",
1314
result.tag_updates = {}
1315
result.tag_conflicts = []
1316
ret = dict(old_refs)
1318
def ref_equals(refs, ref, git_sha, revid):
1323
if (value[0] is not None and
1324
git_sha is not None and
1325
value[0] == git_sha):
1327
if (value[1] is not None and
1328
revid is not None and
1331
# FIXME: If one side only has the git sha available and the other
1332
# only has the bzr revid, then this will cause us to show a tag as
1333
# updated that hasn't actually been updated.
1335
# FIXME: Check for diverged branches
1336
for ref, (git_sha, revid) in new_refs.items():
1337
if ref_equals(ret, ref, git_sha, revid):
1338
# Already up to date
1340
git_sha = old_refs[ref][0]
1342
revid = old_refs[ref][1]
1343
ret[ref] = new_refs[ref] = (git_sha, revid)
1344
elif ref not in ret or overwrite:
1346
tag_name = ref_to_tag_name(ref)
1350
result.tag_updates[tag_name] = revid
1351
ret[ref] = (git_sha, revid)
1353
# FIXME: Check diverged
1357
name = ref_to_tag_name(ref)
1361
result.tag_conflicts.append(
1362
(name, revid, ret[name][1]))
1364
ret[ref] = (git_sha, revid)
1367
def fetch(self, stop_revision=None, fetch_tags=None, lossy=False,
1369
if stop_revision is None:
1370
stop_revision = self.source.last_revision()
1373
for k, v in self.source.tags.get_tag_dict().items():
1374
ret.append((None, v))
1375
ret.append((None, stop_revision))
1377
revidmap = self.interrepo.fetch_objects(ret, lossy=lossy, limit=limit)
1378
except NoPushSupport:
1379
raise errors.NoRoundtrippingSupport(self.source, self.target)
1380
return _mod_repository.FetchResult(revidmap={
1381
old_revid: new_revid
1382
for (old_revid, (new_sha, new_revid)) in revidmap.items()})
1384
def pull(self, overwrite=False, stop_revision=None, local=False,
1385
possible_transports=None, run_hooks=True, _stop_revno=None):
1386
result = GitBranchPullResult()
1387
result.source_branch = self.source
1388
result.target_branch = self.target
1389
with self.source.lock_read(), self.target.lock_write():
1390
new_refs, main_ref, stop_revinfo = self._get_new_refs(
1391
stop_revision, stop_revno=_stop_revno)
1393
def update_refs(old_refs):
1394
return self._update_refs(result, old_refs, new_refs, overwrite)
1396
result.revidmap, old_refs, new_refs = (
1397
self.interrepo.fetch_refs(update_refs, lossy=False))
1398
except NoPushSupport:
1399
raise errors.NoRoundtrippingSupport(self.source, self.target)
1400
(old_sha1, result.old_revid) = old_refs.get(
1401
main_ref, (ZERO_SHA, NULL_REVISION))
1402
if result.old_revid is None:
1403
result.old_revid = self.target.lookup_foreign_revision_id(
1405
result.new_revid = new_refs[main_ref][1]
1406
result.local_branch = None
1407
result.master_branch = self.target
1409
for hook in branch.Branch.hooks['post_pull']:
1413
def push(self, overwrite=False, stop_revision=None, lossy=False,
1414
_override_hook_source_branch=None, _stop_revno=None):
1415
result = GitBranchPushResult()
1416
result.source_branch = self.source
1417
result.target_branch = self.target
1418
result.local_branch = None
1419
result.master_branch = result.target_branch
1420
with self.source.lock_read(), self.target.lock_write():
1421
new_refs, main_ref, stop_revinfo = self._get_new_refs(
1422
stop_revision, stop_revno=_stop_revno)
1424
def update_refs(old_refs):
1425
return self._update_refs(result, old_refs, new_refs, overwrite)
1427
result.revidmap, old_refs, new_refs = (
1428
self.interrepo.fetch_refs(
1429
update_refs, lossy=lossy, overwrite=overwrite))
1430
except NoPushSupport:
1431
raise errors.NoRoundtrippingSupport(self.source, self.target)
1432
(old_sha1, result.old_revid) = old_refs.get(
1433
main_ref, (ZERO_SHA, NULL_REVISION))
1434
if lossy or result.old_revid is None:
1435
result.old_revid = self.target.lookup_foreign_revision_id(
1437
result.new_revid = new_refs[main_ref][1]
1438
(result.new_original_revno,
1439
result.new_original_revid) = stop_revinfo
1440
for hook in branch.Branch.hooks['post_push']:
1445
branch.InterBranch.register_optimiser(InterGitLocalGitBranch)
1446
branch.InterBranch.register_optimiser(InterFromGitBranch)
1447
branch.InterBranch.register_optimiser(InterToGitBranch)
1448
branch.InterBranch.register_optimiser(InterLocalGitRemoteGitBranch)