76
101
return self._lookup_revno(self.new_revid)
79
class LocalGitTagDict(tag.BasicTags):
80
"""Dictionary with tags in a local repository."""
104
class GitTags(tag.BasicTags):
105
"""Ref-based tag dictionary."""
82
107
def __init__(self, branch):
83
108
self.branch = branch
84
109
self.repository = branch.repository
111
def _merge_to_remote_git(self, target_repo, source_tag_refs,
116
def get_changed_refs(old_refs):
118
for ref_name, tag_name, peeled, unpeeled in (
119
source_tag_refs.iteritems()):
120
if old_refs.get(ref_name) == unpeeled:
122
elif overwrite or ref_name not in old_refs:
123
ret[ref_name] = unpeeled
124
updates[tag_name] = target_repo.lookup_foreign_revision_id(
129
self.repository.lookup_foreign_revision_id(peeled),
130
target_repo.lookup_foreign_revision_id(
131
old_refs[ref_name])))
133
target_repo.controldir.send_pack(
134
get_changed_refs, lambda have, want: [])
135
return updates, conflicts
137
def _merge_to_local_git(self, target_repo, source_tag_refs,
141
for ref_name, tag_name, peeled, unpeeled in source_tag_refs:
142
if target_repo._git.refs.get(ref_name) == unpeeled:
144
elif overwrite or ref_name not in target_repo._git.refs:
146
updates[tag_name] = (
147
target_repo.lookup_foreign_revision_id(peeled))
149
trace.warning('%s does not point to a valid object',
152
target_repo._git.refs[ref_name] = unpeeled or peeled
155
source_revid = self.repository.lookup_foreign_revision_id(
157
target_revid = target_repo.lookup_foreign_revision_id(
158
target_repo._git.refs[ref_name])
160
trace.warning('%s does not point to a valid object',
163
conflicts.append((tag_name, source_revid, target_revid))
164
return updates, conflicts
166
def _merge_to_git(self, to_tags, source_tag_refs, overwrite=False):
167
target_repo = to_tags.repository
168
if self.repository.has_same_location(target_repo):
171
if getattr(target_repo, "_git", None):
172
return self._merge_to_local_git(
173
target_repo, source_tag_refs, overwrite)
175
return self._merge_to_remote_git(
176
target_repo, source_tag_refs, overwrite)
178
to_tags.branch._tag_refs = None
180
def _merge_to_non_git(self, to_tags, source_tag_refs, overwrite=False):
181
unpeeled_map = defaultdict(set)
184
result = dict(to_tags.get_tag_dict())
185
for ref_name, tag_name, peeled, unpeeled in source_tag_refs:
186
if unpeeled is not None:
187
unpeeled_map[peeled].add(unpeeled)
189
bzr_revid = self.branch.lookup_foreign_revision_id(peeled)
190
except NotCommitError:
192
if result.get(tag_name) == bzr_revid:
194
elif tag_name not in result or overwrite:
195
result[tag_name] = bzr_revid
196
updates[tag_name] = bzr_revid
198
conflicts.append((tag_name, bzr_revid, result[tag_name]))
199
to_tags._set_tag_dict(result)
200
if len(unpeeled_map) > 0:
201
map_file = UnpeelMap.from_repository(to_tags.branch.repository)
202
map_file.update(unpeeled_map)
203
map_file.save_in_repository(to_tags.branch.repository)
204
return updates, conflicts
206
def merge_to(self, to_tags, overwrite=False, ignore_master=False,
207
source_tag_refs=None):
208
"""See Tags.merge_to."""
209
if source_tag_refs is None:
210
source_tag_refs = self.branch.get_tag_refs()
213
if isinstance(to_tags, GitTags):
214
return self._merge_to_git(to_tags, source_tag_refs,
220
master = to_tags.branch.get_master_branch()
221
if master is not None:
224
updates, conflicts = self._merge_to_non_git(
225
to_tags, source_tag_refs, overwrite=overwrite)
226
if master is not None:
227
extra_updates, extra_conflicts = self.merge_to(
228
master.tags, overwrite=overwrite,
229
source_tag_refs=source_tag_refs,
230
ignore_master=ignore_master)
231
updates.update(extra_updates)
232
conflicts += extra_conflicts
233
return updates, conflicts
235
if master is not None:
86
238
def get_tag_dict(self):
88
for k,v in extract_tags(self.repository._git.get_refs()).iteritems():
240
for (ref_name, tag_name, peeled, unpeeled) in (
241
self.branch.get_tag_refs()):
90
obj = self.repository._git[v]
92
mutter("Tag %s points at unknown object %s, ignoring", v, obj)
94
while isinstance(obj, Tag):
96
obj = self.repository._git[v]
97
if not isinstance(obj, Commit):
98
mutter("Tag %s points at object %r that is not a commit, "
101
ret[k] = self.branch.lookup_foreign_revision_id(v)
243
bzr_revid = self.branch.lookup_foreign_revision_id(peeled)
244
except NotCommitError:
247
ret[tag_name] = bzr_revid
251
class LocalGitTagDict(GitTags):
252
"""Dictionary with tags in a local repository."""
254
def __init__(self, branch):
255
super(LocalGitTagDict, self).__init__(branch)
256
self.refs = self.repository.controldir._git.refs
104
258
def _set_tag_dict(self, to_dict):
105
extra = set(self.repository._git.get_refs().keys())
106
for k, revid in to_dict.iteritems():
259
extra = set(self.refs.allkeys())
260
for k, revid in viewitems(to_dict):
107
261
name = tag_name_to_ref(k)
108
262
if name in extra:
109
263
extra.remove(name)
110
264
self.set_tag(k, revid)
111
265
for name in extra:
112
if name.startswith("refs/tags/"):
113
267
del self.repository._git[name]
115
269
def set_tag(self, name, revid):
116
self.repository._git.refs[tag_name_to_ref(name)], _ = \
117
self.branch.lookup_bzr_revision_id(revid)
120
class DictTagDict(LocalGitTagDict):
122
def __init__(self, branch, tags):
123
super(DictTagDict, self).__init__(branch)
126
def get_tag_dict(self):
271
git_sha, mapping = self.branch.lookup_bzr_revision_id(revid)
272
except errors.NoSuchRevision:
273
raise errors.GhostTagsNotSupported(self)
274
self.refs[tag_name_to_ref(name)] = git_sha
275
self.branch._tag_refs = None
277
def delete_tag(self, name):
278
ref = tag_name_to_ref(name)
279
if ref not in self.refs:
280
raise errors.NoSuchTag(name)
282
self.branch._tag_refs = None
130
285
class GitBranchFormat(branch.BranchFormat):
132
def get_format_description(self):
135
287
def network_name(self):
138
290
def supports_tags(self):
293
def supports_leaving_lock(self):
296
def supports_tags_referencing_ghosts(self):
299
def tags_are_versioned(self):
141
302
def get_foreign_tests_branch_factory(self):
142
from bzrlib.plugins.git.tests.test_branch import ForeignTestsBranchFactory
303
from .tests.test_branch import ForeignTestsBranchFactory
143
304
return ForeignTestsBranchFactory()
145
306
def make_tags(self, branch):
146
if getattr(branch.repository, "get_refs", None) is not None:
147
from bzrlib.plugins.git.remote import RemoteGitTagDict
309
except AttributeError:
311
if getattr(branch.repository, "_git", None) is None:
312
from .remote import RemoteGitTagDict
148
313
return RemoteGitTagDict(branch)
150
315
return LocalGitTagDict(branch)
153
class GitReadLock(object):
155
def __init__(self, unlock):
159
class GitWriteLock(object):
161
def __init__(self, unlock):
317
def initialize(self, a_controldir, name=None, repository=None,
318
append_revisions_only=None):
319
raise NotImplementedError(self.initialize)
321
def get_reference(self, controldir, name=None):
322
return controldir.get_branch_reference(name=name)
324
def set_reference(self, controldir, name, target):
325
return controldir.set_branch_reference(target, name)
328
class LocalGitBranchFormat(GitBranchFormat):
330
def get_format_description(self):
331
return 'Local Git Branch'
334
def _matchingcontroldir(self):
335
from .dir import LocalGitControlDirFormat
336
return LocalGitControlDirFormat()
338
def initialize(self, a_controldir, name=None, repository=None,
339
append_revisions_only=None):
340
from .dir import LocalGitDir
341
if not isinstance(a_controldir, LocalGitDir):
342
raise errors.IncompatibleFormat(self, a_controldir._format)
343
return a_controldir.create_branch(
344
repository=repository, name=name,
345
append_revisions_only=append_revisions_only)
165
348
class GitBranch(ForeignBranch):
166
349
"""An adapter to git repositories for bzr Branch objects."""
168
def __init__(self, bzrdir, repository, ref, lockfiles, tagsdict=None):
352
def control_transport(self):
353
return self._control_transport
356
def user_transport(self):
357
return self._user_transport
359
def __init__(self, controldir, repository, ref, format):
169
360
self.repository = repository
170
self._format = GitBranchFormat()
171
self.control_files = lockfiles
361
self._format = format
362
self.controldir = controldir
363
self._lock_mode = None
173
365
super(GitBranch, self).__init__(repository.get_mapping())
174
if tagsdict is not None:
175
self.tags = DictTagDict(self, tagsdict)
177
self.name = ref_to_branch_name(ref)
178
367
self._head = None
179
self.base = bzrdir.root_transport.base
368
self._user_transport = controldir.user_transport.clone('.')
369
self._control_transport = controldir.control_transport.clone('.')
370
self._tag_refs = None
373
self.name = ref_to_branch_name(ref)
376
if self.ref is not None:
377
params = {"ref": urlutils.escape(self.ref)}
380
params = {"branch": urlutils.escape(self.name)}
381
for k, v in params.items():
382
self._user_transport.set_segment_parameter(k, v)
383
self._control_transport.set_segment_parameter(k, v)
384
self.base = controldir.user_transport.base
181
def _get_checkout_format(self):
386
def _get_checkout_format(self, lightweight=False):
182
387
"""Return the most suitable metadir for a checkout of this branch.
183
388
Weaves are used if this branch's repository uses weaves.
185
return bzrdir.format_registry.make_bzrdir("default")
391
return controldir.format_registry.make_controldir("git")
393
return controldir.format_registry.make_controldir("default")
187
395
def get_child_submit_format(self):
188
396
"""Return the preferred format of submissions to this branch."""
189
ret = self.get_config().get_user_option("child_submit_format")
397
ret = self.get_config_stack().get("child_submit_format")
190
398
if ret is not None:
402
def get_config(self):
403
return GitBranchConfig(self)
405
def get_config_stack(self):
406
return GitBranchStack(self)
194
408
def _get_nick(self, local=False, possible_master_transports=None):
195
409
"""Find the nick name for this branch.
197
411
:return: Branch nick
199
return self.name or "HEAD"
413
if getattr(self.repository, '_git', None):
414
cs = self.repository._git.get_config_stack()
416
return cs.get((b"branch", self.name.encode('utf-8')),
417
b"nick").decode("utf-8")
420
return self.name or u"HEAD"
201
422
def _set_nick(self, nick):
202
raise NotImplementedError
423
cf = self.repository._git.get_config()
424
cf.set((b"branch", self.name.encode('utf-8')),
425
b"nick", nick.encode("utf-8"))
428
self.repository._git._put_named_file('config', f.getvalue())
204
430
nick = property(_get_nick, _set_nick)
206
432
def __repr__(self):
207
433
return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
210
def generate_revision_history(self, revid, old_revid=None):
211
# FIXME: Check that old_revid is in the ancestry of revid
212
newhead, self.mapping = self.mapping.revision_id_bzr_to_foreign(revid)
213
self._set_head(newhead)
215
def lock_write(self):
216
self.control_files.lock_write()
217
return GitWriteLock(self.unlock)
436
def generate_revision_history(self, revid, last_rev=None,
438
if last_rev is not None:
439
graph = self.repository.get_graph()
440
if not graph.is_ancestor(last_rev, revid):
441
# our previous tip is not merged into stop_revision
442
raise errors.DivergedBranches(self, other_branch)
444
self.set_last_revision(revid)
446
def lock_write(self, token=None):
447
if token is not None:
448
raise errors.TokenLockingNotSupported(self)
450
if self._lock_mode == 'r':
451
raise errors.ReadOnlyError(self)
452
self._lock_count += 1
455
self._lock_mode = 'w'
457
self.repository.lock_write()
458
return lock.LogicalLockResult(self.unlock)
460
def leave_lock_in_place(self):
461
raise NotImplementedError(self.leave_lock_in_place)
463
def dont_leave_lock_in_place(self):
464
raise NotImplementedError(self.dont_leave_lock_in_place)
219
466
def get_stacked_on_url(self):
220
467
# Git doesn't do stacking (yet...)
221
raise errors.UnstackableBranchFormat(self._format, self.base)
223
def get_parent(self):
468
raise branch.UnstackableBranchFormat(self._format, self.base)
470
def _get_push_origin(self, cs):
471
"""Get the name for the push origin.
473
The exact behaviour is documented in the git-config(1) manpage.
476
return cs.get((b'branch', self.name.encode('utf-8')), b'pushRemote')
479
return cs.get((b'branch', ), b'remote')
482
return cs.get((b'branch', self.name.encode('utf-8')), b'remote')
486
def _get_origin(self, cs):
488
return cs.get((b'branch', self.name.encode('utf-8')), b'remote')
492
def _get_related_push_branch(self, cs):
493
remote = self._get_push_origin(cs)
495
location = cs.get((b"remote", remote), b"url")
499
return git_url_to_bzr_url(location.decode('utf-8'), ref=self.ref)
501
def _get_related_merge_branch(self, cs):
502
remote = self._get_origin(cs)
504
location = cs.get((b"remote", remote), b"url")
509
ref = cs.get((b"branch", remote), b"merge")
513
return git_url_to_bzr_url(location.decode('utf-8'), ref=ref)
515
def _get_parent_location(self):
224
516
"""See Branch.get_parent()."""
225
# FIXME: Set "origin" url from .git/config ?
228
def set_parent(self, url):
229
# FIXME: Set "origin" url in .git/config ?
517
cs = self.repository._git.get_config_stack()
518
return self._get_related_merge_branch(cs)
520
def _write_git_config(self, cs):
523
self.repository._git._put_named_file('config', f.getvalue())
525
def set_parent(self, location):
526
cs = self.repository._git.get_config()
527
remote = self._get_origin(cs)
528
this_url = urlutils.split_segment_parameters(self.user_url)[0]
529
target_url, branch, ref = bzr_url_to_git_url(location)
530
location = urlutils.relative_url(this_url, target_url)
531
cs.set((b"remote", remote), b"url", location)
533
cs.set((b"branch", remote), b"merge", branch_name_to_ref(branch))
535
cs.set((b"branch", remote), b"merge", ref)
537
# TODO(jelmer): Maybe unset rather than setting to HEAD?
538
cs.set((b"branch", remote), b"merge", b'HEAD')
539
self._write_git_config(cs)
541
def break_lock(self):
542
raise NotImplementedError(self.break_lock)
232
544
def lock_read(self):
233
self.control_files.lock_read()
234
return GitReadLock(self.unlock)
546
if self._lock_mode not in ('r', 'w'):
547
raise ValueError(self._lock_mode)
548
self._lock_count += 1
550
self._lock_mode = 'r'
552
self.repository.lock_read()
553
return lock.LogicalLockResult(self.unlock)
555
def peek_lock_mode(self):
556
return self._lock_mode
236
558
def is_locked(self):
237
return self.control_files.is_locked()
559
return (self._lock_mode is not None)
564
def _unlock_ref(self):
239
567
def unlock(self):
240
self.control_files.unlock()
568
"""See Branch.unlock()."""
569
if self._lock_count == 0:
570
raise errors.LockNotHeld(self)
572
self._lock_count -= 1
573
if self._lock_count == 0:
574
if self._lock_mode == 'w':
576
self._lock_mode = None
577
self._clear_cached_state()
579
self.repository.unlock()
242
581
def get_physical_lock_status(self):
246
584
def last_revision(self):
247
# perhaps should escape this ?
248
if self.head is None:
249
return revision.NULL_REVISION
250
return self.lookup_foreign_revision_id(self.head)
585
with self.lock_read():
586
# perhaps should escape this ?
587
if self.head is None:
588
return revision.NULL_REVISION
589
return self.lookup_foreign_revision_id(self.head)
252
591
def _basic_push(self, target, overwrite=False, stop_revision=None):
253
592
return branch.InterBranch.get(self, target)._basic_push(
254
593
overwrite, stop_revision)
256
595
def lookup_foreign_revision_id(self, foreign_revid):
257
return self.repository.lookup_foreign_revision_id(foreign_revid,
597
return self.repository.lookup_foreign_revision_id(foreign_revid,
601
return self.mapping.revision_id_foreign_to_bzr(foreign_revid)
260
603
def lookup_bzr_revision_id(self, revid):
261
604
return self.repository.lookup_bzr_revision_id(
262
605
revid, mapping=self.mapping)
607
def get_unshelver(self, tree):
608
raise errors.StoringUncommittedNotSupported(self)
610
def _clear_cached_state(self):
611
super(GitBranch, self)._clear_cached_state()
612
self._tag_refs = None
614
def _iter_tag_refs(self, refs):
615
"""Iterate over the tag refs.
617
:param refs: Refs dictionary (name -> git sha1)
618
:return: iterator over (ref_name, tag_name, peeled_sha1, unpeeled_sha1)
620
raise NotImplementedError(self._iter_tag_refs)
622
def get_tag_refs(self):
623
with self.lock_read():
624
if self._tag_refs is None:
625
self._tag_refs = list(self._iter_tag_refs())
626
return self._tag_refs
628
def import_last_revision_info_and_tags(self, source, revno, revid,
630
"""Set the last revision info, importing from another repo if necessary.
632
This is used by the bound branch code to upload a revision to
633
the master branch first before updating the tip of the local branch.
634
Revisions referenced by source's tags are also transferred.
636
:param source: Source branch to optionally fetch from
637
:param revno: Revision number of the new tip
638
:param revid: Revision id of the new tip
639
:param lossy: Whether to discard metadata that can not be
641
:return: Tuple with the new revision number and revision id
642
(should only be different from the arguments when lossy=True)
644
push_result = source.push(
645
self, stop_revision=revid, lossy=lossy, _stop_revno=revno)
646
return (push_result.new_revno, push_result.new_revid)
648
def reconcile(self, thorough=True):
649
"""Make sure the data stored in this branch is consistent."""
650
from ..reconcile import ReconcileResult
652
return ReconcileResult()
265
655
class LocalGitBranch(GitBranch):
266
656
"""A local Git branch."""
268
def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
269
super(LocalGitBranch, self).__init__(bzrdir, repository, name,
271
refs = repository._git.get_refs()
272
if not (name in refs.keys() or "HEAD" in refs.keys()):
273
raise errors.NotBranchError(self.base)
658
def __init__(self, controldir, repository, ref):
659
super(LocalGitBranch, self).__init__(controldir, repository, ref,
660
LocalGitBranchFormat())
275
662
def create_checkout(self, to_location, revision_id=None, lightweight=False,
276
accelerator_tree=None, hardlink=False):
663
accelerator_tree=None, hardlink=False):
664
t = transport.get_transport(to_location)
666
format = self._get_checkout_format(lightweight=lightweight)
667
checkout = format.initialize_on_transport(t)
278
t = transport.get_transport(to_location)
280
format = self._get_checkout_format()
281
checkout = format.initialize_on_transport(t)
282
from_branch = branch.BranchReferenceFormat().initialize(checkout,
284
tree = checkout.create_workingtree(revision_id,
285
from_branch=from_branch, hardlink=hardlink)
669
from_branch = checkout.set_branch_reference(target_branch=self)
288
return self._create_heavyweight_checkout(to_location, revision_id,
291
def _create_heavyweight_checkout(self, to_location, revision_id=None,
293
"""Create a new heavyweight checkout of this branch.
295
:param to_location: URL of location to create the new checkout in.
296
:param revision_id: Revision that should be the tip of the checkout.
297
:param hardlink: Whether to hardlink
298
:return: WorkingTree object of checkout.
300
checkout_branch = bzrdir.BzrDir.create_branch_convenience(
301
to_location, force_new_tree=False)
302
checkout = checkout_branch.bzrdir
303
checkout_branch.bind(self)
304
# pull up to the specified revision_id to set the initial
305
# branch tip correctly, and seed it with history.
306
checkout_branch.pull(self, stop_revision=revision_id)
307
return checkout.create_workingtree(revision_id, hardlink=hardlink)
671
policy = checkout.determine_repository_policy()
672
policy.acquire_repository()
673
checkout_branch = checkout.create_branch()
674
checkout_branch.bind(self)
675
checkout_branch.pull(self, stop_revision=revision_id)
677
return checkout.create_workingtree(
678
revision_id, from_branch=from_branch, hardlink=hardlink)
681
self._ref_lock = self.repository._git.refs.lock_ref(self.ref)
683
def _unlock_ref(self):
684
self._ref_lock.unlock()
686
def break_lock(self):
687
self.repository._git.refs.unlock_ref(self.ref)
689
def fetch(self, from_branch, last_revision=None, limit=None):
690
return branch.InterBranch.get(from_branch, self).fetch(
691
stop_revision=last_revision, limit=limit)
309
693
def _gen_revision_history(self):
310
694
if self.head is None:
312
ret = list(self.repository.iter_reverse_revision_history(
313
self.last_revision()))
696
last_revid = self.last_revision()
697
graph = self.repository.get_graph()
699
ret = list(graph.iter_lefthand_ancestry(
700
last_revid, (revision.NULL_REVISION, )))
701
except errors.RevisionNotPresent as e:
702
raise errors.GhostRevisionsHaveNoRevno(last_revid, e.revision_id)
317
706
def _get_head(self):
319
return self.repository._git.ref(self.ref or "HEAD")
708
return self.repository._git.refs[self.ref]
323
def set_last_revision_info(self, revno, revid):
324
self.set_last_revision(revid)
712
def _read_last_revision_info(self):
713
last_revid = self.last_revision()
714
graph = self.repository.get_graph()
716
revno = graph.find_distance_to_null(
717
last_revid, [(revision.NULL_REVISION, 0)])
718
except errors.GhostRevisionsHaveNoRevno:
720
return revno, last_revid
722
def set_last_revision_info(self, revno, revision_id):
723
self.set_last_revision(revision_id)
724
self._last_revision_info_cache = revno, revision_id
326
726
def set_last_revision(self, revid):
327
(newhead, self.mapping) = self.repository.lookup_bzr_revision_id(revid)
727
if not revid or not isinstance(revid, bytes):
728
raise errors.InvalidRevisionId(revision_id=revid, branch=self)
729
if revid == NULL_REVISION:
732
(newhead, self.mapping) = self.repository.lookup_bzr_revision_id(
734
if self.mapping is None:
736
self._set_head(newhead)
330
738
def _set_head(self, value):
739
if value == ZERO_SHA:
740
raise ValueError(value)
331
741
self._head = value
332
self.repository._git.refs[self.ref or "HEAD"] = self._head
743
del self.repository._git.refs[self.ref]
745
self.repository._git.refs[self.ref] = self._head
333
746
self._clear_cached_state()
335
748
head = property(_get_head, _set_head)
337
def get_config(self):
338
return GitBranchConfig(self)
340
750
def get_push_location(self):
341
751
"""See Branch.get_push_location."""
342
push_loc = self.get_config().get_user_option('push_location')
752
push_loc = self.get_config_stack().get('push_location')
753
if push_loc is not None:
755
cs = self.repository._git.get_config_stack()
756
return self._get_related_push_branch(cs)
345
758
def set_push_location(self, location):
346
759
"""See Branch.set_push_location."""
427
893
def _get_branch_formats_to_test():
895
default_format = branch.format_registry.get_default()
896
except AttributeError:
897
default_format = branch.BranchFormat._default_format
898
from .remote import RemoteGitBranchFormat
900
(RemoteGitBranchFormat(), default_format),
901
(LocalGitBranchFormat(), default_format)]
431
904
def _get_interrepo(self, source, target):
432
return repository.InterRepository.get(source.repository,
905
return _mod_repository.InterRepository.get(
906
source.repository, target.repository)
436
909
def is_compatible(cls, source, target):
437
return (isinstance(source, GitBranch) and
438
not isinstance(target, GitBranch) and
439
(getattr(cls._get_interrepo(source, target), "fetch_objects", None) is not None))
441
def _update_revisions(self, stop_revision=None, overwrite=False,
442
graph=None, limit=None):
443
"""Like InterBranch.update_revisions(), but with additions.
445
Compared to the `update_revisions()` below, this function takes a
446
`limit` argument that limits how many git commits will be converted
447
and returns the new git head.
910
if not isinstance(source, GitBranch):
912
if isinstance(target, GitBranch):
913
# InterLocalGitRemoteGitBranch or InterToGitBranch should be used
915
if (getattr(cls._get_interrepo(source, target), "fetch_objects", None)
917
# fetch_objects is necessary for this to work
921
def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
922
self.fetch_objects(stop_revision, fetch_tags=fetch_tags, limit=limit)
924
def fetch_objects(self, stop_revision, fetch_tags, limit=None):
449
925
interrepo = self._get_interrepo(self.source, self.target)
926
if fetch_tags is None:
927
c = self.source.get_config_stack()
928
fetch_tags = c.get('branch.fetch_tags')
450
930
def determine_wants(heads):
451
if self.source.ref is not None and not self.source.ref in heads:
452
raise NoSuchRef(self.source.ref, heads.keys())
453
if stop_revision is not None:
931
if stop_revision is None:
933
head = heads[self.source.ref]
935
self._last_revid = revision.NULL_REVISION
937
self._last_revid = self.source.lookup_foreign_revision_id(
454
940
self._last_revid = stop_revision
455
head, mapping = self.source.repository.lookup_bzr_revision_id(
458
if self.source.ref is not None:
459
head = heads[self.source.ref]
462
self._last_revid = self.source.lookup_foreign_revision_id(head)
463
if self.target.repository.has_revision(self._last_revid):
941
real = interrepo.get_determine_wants_revids(
942
[self._last_revid], include_tags=fetch_tags)
466
944
pack_hint, head, refs = interrepo.fetch_objects(
467
945
determine_wants, self.source.mapping, limit=limit)
468
946
if (pack_hint is not None and
469
self.target.repository._format.pack_compresses):
947
self.target.repository._format.pack_compresses):
470
948
self.target.repository.pack(hint=pack_hint)
472
self._last_revid = self.source.lookup_foreign_revision_id(head)
951
def _update_revisions(self, stop_revision=None, overwrite=False):
952
head, refs = self.fetch_objects(stop_revision, fetch_tags=None)
474
954
prev_last_revid = None
476
956
prev_last_revid = self.target.last_revision()
477
self.target.generate_revision_history(self._last_revid,
481
def update_revisions(self, stop_revision=None, overwrite=False,
483
"""See InterBranch.update_revisions()."""
484
self._update_revisions(stop_revision, overwrite, graph)
486
def pull(self, overwrite=False, stop_revision=None,
487
possible_transports=None, _hook_master=None, run_hooks=True,
488
_override_hook_target=None, local=False, limit=None):
491
:param _hook_master: Private parameter - set the branch to
492
be supplied as the master to pull hooks.
493
:param run_hooks: Private parameter - if false, this branch
494
is being called because it's the master of the primary branch,
495
so it should not run its hooks.
496
:param _override_hook_target: Private parameter - set the branch to be
497
supplied as the target_branch to pull hooks.
498
:param limit: Only import this many revisons. `None`, the default,
499
means import all revisions.
501
# This type of branch can't be bound.
503
raise errors.LocalRequiresBoundBranch()
957
self.target.generate_revision_history(
958
self._last_revid, last_rev=prev_last_revid,
959
other_branch=self.source)
962
def _basic_pull(self, stop_revision, overwrite, run_hooks,
963
_override_hook_target, _hook_master):
964
if overwrite is True:
965
overwrite = set(["history", "tags"])
504
968
result = GitBranchPullResult()
505
969
result.source_branch = self.source
506
970
if _override_hook_target is None:
507
971
result.target_branch = self.target
509
973
result.target_branch = _override_hook_target
510
self.source.lock_read()
974
with self.target.lock_write(), self.source.lock_read():
512
975
# We assume that during 'pull' the target repository is closer than
513
976
# the source one.
514
graph = self.target.repository.get_graph(self.source.repository)
515
977
(result.old_revno, result.old_revid) = \
516
978
self.target.last_revision_info()
517
result.new_git_head = self._update_revisions(
518
stop_revision, overwrite=overwrite, graph=graph, limit=limit)
519
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
979
result.new_git_head, remote_refs = self._update_revisions(
980
stop_revision, overwrite=("history" in overwrite))
981
tags_ret = self.source.tags.merge_to(
982
self.target.tags, ("tags" in overwrite), ignore_master=True)
983
if isinstance(tags_ret, tuple):
984
result.tag_updates, result.tag_conflicts = tags_ret
986
result.tag_conflicts = tags_ret
521
987
(result.new_revno, result.new_revid) = \
522
988
self.target.last_revision_info()
550
1067
class InterGitBranch(branch.GenericInterBranch):
551
1068
"""InterBranch implementation that pulls between Git branches."""
554
class InterGitLocalRemoteBranch(InterGitBranch):
1070
def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
1071
raise NotImplementedError(self.fetch)
1074
class InterLocalGitRemoteGitBranch(InterGitBranch):
555
1075
"""InterBranch that copies from a local to a remote git branch."""
558
1078
def _get_branch_formats_to_test():
1079
from .remote import RemoteGitBranchFormat
1081
(LocalGitBranchFormat(), RemoteGitBranchFormat())]
562
1084
def is_compatible(self, source, target):
563
from bzrlib.plugins.git.remote import RemoteGitBranch
1085
from .remote import RemoteGitBranch
564
1086
return (isinstance(source, LocalGitBranch) and
565
1087
isinstance(target, RemoteGitBranch))
567
def _basic_push(self, overwrite=False, stop_revision=None):
568
from dulwich.protocol import ZERO_SHA
1089
def _basic_push(self, overwrite, stop_revision):
569
1090
result = GitBranchPushResult()
570
1091
result.source_branch = self.source
571
1092
result.target_branch = self.target
572
1093
if stop_revision is None:
573
1094
stop_revision = self.source.last_revision()
574
# FIXME: Check for diverged branches
575
1096
def get_changed_refs(old_refs):
576
result.old_revid = self.target.lookup_foreign_revision_id(old_refs.get(self.target.ref, ZERO_SHA))
577
refs = { self.target.ref: self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
1097
old_ref = old_refs.get(self.target.ref, None)
1099
result.old_revid = revision.NULL_REVISION
1101
result.old_revid = self.target.lookup_foreign_revision_id(
1103
new_ref = self.source.repository.lookup_bzr_revision_id(
1106
if remote_divergence(
1108
self.source.repository._git.object_store):
1109
raise errors.DivergedBranches(self.source, self.target)
1110
refs = {self.target.ref: new_ref}
578
1111
result.new_revid = stop_revision
579
for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
1112
for name, sha in viewitems(
1113
self.source.repository._git.refs.as_dict(b"refs/tags")):
580
1114
refs[tag_name_to_ref(name)] = sha
582
self.target.repository.send_pack(get_changed_refs,
583
self.source.repository._git.object_store.generate_pack_contents)
1116
self.target.repository.send_pack(
1118
self.source.repository._git.object_store.generate_pack_data)
587
class InterGitRemoteLocalBranch(InterGitBranch):
1122
class InterGitLocalGitBranch(InterGitBranch):
588
1123
"""InterBranch that copies from a remote to a local git branch."""
591
1126
def _get_branch_formats_to_test():
1127
from .remote import RemoteGitBranchFormat
1129
(RemoteGitBranchFormat(), LocalGitBranchFormat()),
1130
(LocalGitBranchFormat(), LocalGitBranchFormat())]
595
1133
def is_compatible(self, source, target):
596
from bzrlib.plugins.git.remote import RemoteGitBranch
597
return (isinstance(source, RemoteGitBranch) and
1134
return (isinstance(source, GitBranch) and
598
1135
isinstance(target, LocalGitBranch))
1137
def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
1138
interrepo = _mod_repository.InterRepository.get(self.source.repository,
1139
self.target.repository)
1140
if stop_revision is None:
1141
stop_revision = self.source.last_revision()
1142
determine_wants = interrepo.get_determine_wants_revids(
1143
[stop_revision], include_tags=fetch_tags)
1144
interrepo.fetch_objects(determine_wants, limit=limit)
600
1146
def _basic_push(self, overwrite=False, stop_revision=None):
601
result = branch.BranchPushResult()
1147
if overwrite is True:
1148
overwrite = set(["history", "tags"])
1151
result = GitBranchPushResult()
602
1152
result.source_branch = self.source
603
1153
result.target_branch = self.target
604
1154
result.old_revid = self.target.last_revision()
605
1155
refs, stop_revision = self.update_refs(stop_revision)
606
self.target.generate_revision_history(stop_revision, result.old_revid)
607
self.update_tags(refs)
1156
self.target.generate_revision_history(
1158
(result.old_revid if ("history" not in overwrite) else None),
1159
other_branch=self.source)
1160
tags_ret = self.source.tags.merge_to(
1162
source_tag_refs=remote_refs_dict_to_tag_refs(refs),
1163
overwrite=("tags" in overwrite))
1164
if isinstance(tags_ret, tuple):
1165
(result.tag_updates, result.tag_conflicts) = tags_ret
1167
result.tag_conflicts = tags_ret
608
1168
result.new_revid = self.target.last_revision()
611
def update_tags(self, refs):
612
for name, v in extract_tags(refs).iteritems():
613
revid = self.target.lookup_foreign_revision_id(v)
614
self.target.tags.set_tag(name, revid)
616
1171
def update_refs(self, stop_revision=None):
617
interrepo = repository.InterRepository.get(self.source.repository,
618
self.target.repository)
1172
interrepo = _mod_repository.InterRepository.get(
1173
self.source.repository, self.target.repository)
1174
c = self.source.get_config_stack()
1175
fetch_tags = c.get('branch.fetch_tags')
619
1177
if stop_revision is None:
620
refs = interrepo.fetch(branches=["HEAD"])
621
stop_revision = self.target.lookup_foreign_revision_id(refs["HEAD"])
1178
refs = interrepo.fetch(branches=[self.source.ref], include_tags=fetch_tags)
1180
head = refs[self.source.ref]
1182
stop_revision = revision.NULL_REVISION
1184
stop_revision = self.target.lookup_foreign_revision_id(head)
623
refs = interrepo.fetch(revision_id=stop_revision)
1186
refs = interrepo.fetch(
1187
revision_id=stop_revision, include_tags=fetch_tags)
624
1188
return refs, stop_revision
626
1190
def pull(self, stop_revision=None, overwrite=False,
627
possible_transports=None, run_hooks=True,local=False):
1191
possible_transports=None, run_hooks=True, local=False):
628
1192
# This type of branch can't be bound.
630
1194
raise errors.LocalRequiresBoundBranch()
1195
if overwrite is True:
1196
overwrite = set(["history", "tags"])
631
1200
result = GitPullResult()
632
1201
result.source_branch = self.source
633
1202
result.target_branch = self.target
634
result.old_revid = self.target.last_revision()
635
refs, stop_revision = self.update_refs(stop_revision)
636
self.target.generate_revision_history(stop_revision, result.old_revid)
637
self.update_tags(refs)
638
result.new_revid = self.target.last_revision()
1203
with self.target.lock_write(), self.source.lock_read():
1204
result.old_revid = self.target.last_revision()
1205
refs, stop_revision = self.update_refs(stop_revision)
1206
self.target.generate_revision_history(
1208
(result.old_revid if ("history" not in overwrite) else None),
1209
other_branch=self.source)
1210
tags_ret = self.source.tags.merge_to(
1211
self.target.tags, overwrite=("tags" in overwrite),
1212
source_tag_refs=remote_refs_dict_to_tag_refs(refs))
1213
if isinstance(tags_ret, tuple):
1214
(result.tag_updates, result.tag_conflicts) = tags_ret
1216
result.tag_conflicts = tags_ret
1217
result.new_revid = self.target.last_revision()
1218
result.local_branch = None
1219
result.master_branch = result.target_branch
1221
for hook in branch.Branch.hooks['post_pull']:
642
1226
class InterToGitBranch(branch.GenericInterBranch):
643
"""InterBranch implementation that pulls from Git into bzr."""
1227
"""InterBranch implementation that pulls into a Git branch."""
645
1229
def __init__(self, source, target):
646
1230
super(InterToGitBranch, self).__init__(source, target)
647
self.interrepo = repository.InterRepository.get(source.repository,
1231
self.interrepo = _mod_repository.InterRepository.get(source.repository,
651
1235
def _get_branch_formats_to_test():
1237
default_format = branch.format_registry.get_default()
1238
except AttributeError:
1239
default_format = branch.BranchFormat._default_format
1240
from .remote import RemoteGitBranchFormat
1242
(default_format, LocalGitBranchFormat()),
1243
(default_format, RemoteGitBranchFormat())]
655
1246
def is_compatible(self, source, target):
656
1247
return (not isinstance(source, GitBranch) and
657
1248
isinstance(target, GitBranch))
659
def update_revisions(self, *args, **kwargs):
660
raise NoPushSupport()
662
def _get_new_refs(self, stop_revision=None):
1250
def _get_new_refs(self, stop_revision=None, fetch_tags=None,
1252
if not self.source.is_locked():
1253
raise errors.ObjectNotLocked(self.source)
663
1254
if stop_revision is None:
664
stop_revision = self.source.last_revision()
665
assert type(stop_revision) is str
666
main_ref = self.target.ref or "refs/heads/master"
667
refs = { main_ref: (None, stop_revision) }
668
for name, revid in self.source.tags.get_tag_dict().iteritems():
1255
(stop_revno, stop_revision) = self.source.last_revision_info()
1256
elif stop_revno is None:
1258
stop_revno = self.source.revision_id_to_revno(stop_revision)
1259
except errors.NoSuchRevision:
1261
if not isinstance(stop_revision, bytes):
1262
raise TypeError(stop_revision)
1263
main_ref = self.target.ref
1264
refs = {main_ref: (None, stop_revision)}
1265
if fetch_tags is None:
1266
c = self.source.get_config_stack()
1267
fetch_tags = c.get('branch.fetch_tags')
1268
for name, revid in viewitems(self.source.tags.get_tag_dict()):
669
1269
if self.source.repository.has_revision(revid):
670
refs[tag_name_to_ref(name)] = (None, revid)
671
return refs, main_ref
1270
ref = tag_name_to_ref(name)
1271
if not check_ref_format(ref):
1272
warning("skipping tag with invalid characters %s (%s)",
1276
# FIXME: Skip tags that are not in the ancestry
1277
refs[ref] = (None, revid)
1278
return refs, main_ref, (stop_revno, stop_revision)
1280
def _update_refs(self, result, old_refs, new_refs, overwrite):
1281
mutter("updating refs. old refs: %r, new refs: %r",
1283
result.tag_updates = {}
1284
result.tag_conflicts = []
1285
ret = dict(old_refs)
1287
def ref_equals(refs, ref, git_sha, revid):
1292
if (value[0] is not None and
1293
git_sha is not None and
1294
value[0] == git_sha):
1296
if (value[1] is not None and
1297
revid is not None and
1300
# FIXME: If one side only has the git sha available and the other
1301
# only has the bzr revid, then this will cause us to show a tag as
1302
# updated that hasn't actually been updated.
1304
# FIXME: Check for diverged branches
1305
for ref, (git_sha, revid) in viewitems(new_refs):
1306
if ref_equals(ret, ref, git_sha, revid):
1307
# Already up to date
1309
git_sha = old_refs[ref][0]
1311
revid = old_refs[ref][1]
1312
ret[ref] = new_refs[ref] = (git_sha, revid)
1313
elif ref not in ret or overwrite:
1315
tag_name = ref_to_tag_name(ref)
1319
result.tag_updates[tag_name] = revid
1320
ret[ref] = (git_sha, revid)
1322
# FIXME: Check diverged
1326
name = ref_to_tag_name(ref)
1330
result.tag_conflicts.append(
1331
(name, revid, ret[name][1]))
1333
ret[ref] = (git_sha, revid)
1336
def fetch(self, stop_revision=None, fetch_tags=None, lossy=False,
1338
if stop_revision is None:
1339
stop_revision = self.source.last_revision()
1342
for k, v in viewitems(self.source.tags.get_tag_dict()):
1343
ret.append((None, v))
1344
ret.append((None, stop_revision))
1346
self.interrepo.fetch_objects(ret, lossy=lossy, limit=limit)
1347
except NoPushSupport:
1348
raise errors.NoRoundtrippingSupport(self.source, self.target)
673
1350
def pull(self, overwrite=False, stop_revision=None, local=False,
674
possible_transports=None):
675
from dulwich.protocol import ZERO_SHA
1351
possible_transports=None, run_hooks=True, _stop_revno=None):
676
1352
result = GitBranchPullResult()
677
1353
result.source_branch = self.source
678
1354
result.target_branch = self.target
679
new_refs, main_ref = self._get_new_refs(stop_revision)
680
def update_refs(old_refs):
681
refs = dict(old_refs)
682
# FIXME: Check for diverged branches
683
refs.update(new_refs)
685
old_refs, new_refs = self.interrepo.fetch_refs(update_refs)
686
result.old_revid = self.target.lookup_foreign_revision_id(
687
old_refs.get(main_ref, ZERO_SHA))
688
result.new_revid = new_refs[main_ref]
691
def push(self, overwrite=False, stop_revision=None,
692
_override_hook_source_branch=None):
693
from dulwich.protocol import ZERO_SHA
694
result = GitBranchPushResult()
695
result.source_branch = self.source
696
result.target_branch = self.target
697
new_refs, main_ref = self._get_new_refs(stop_revision)
698
def update_refs(old_refs):
699
refs = dict(old_refs)
700
# FIXME: Check for diverged branches
701
refs.update(new_refs)
703
old_refs, new_refs = self.interrepo.fetch_refs(update_refs)
704
(result.old_revid, old_sha1) = old_refs.get(main_ref, (ZERO_SHA, NULL_REVISION))
705
if result.old_revid is None:
706
result.old_revid = self.target.lookup_foreign_revision_id(old_sha1)
707
result.new_revid = new_refs[main_ref]
710
def lossy_push(self, stop_revision=None):
711
result = GitBranchPushResult()
712
result.source_branch = self.source
713
result.target_branch = self.target
714
new_refs, main_ref = self._get_new_refs(stop_revision)
715
def update_refs(old_refs):
716
refs = dict(old_refs)
717
# FIXME: Check for diverged branches
718
refs.update(new_refs)
720
result.revidmap, old_refs, new_refs = self.interrepo.dfetch_refs(
722
result.old_revid = old_refs.get(self.target.ref, (None, NULL_REVISION))[1]
723
result.new_revid = new_refs[main_ref][1]
727
branch.InterBranch.register_optimiser(InterGitRemoteLocalBranch)
1355
with self.source.lock_read(), self.target.lock_write():
1356
new_refs, main_ref, stop_revinfo = self._get_new_refs(
1357
stop_revision, stop_revno=_stop_revno)
1359
def update_refs(old_refs):
1360
return self._update_refs(result, old_refs, new_refs, overwrite)
1362
result.revidmap, old_refs, new_refs = (
1363
self.interrepo.fetch_refs(update_refs, lossy=False))
1364
except NoPushSupport:
1365
raise errors.NoRoundtrippingSupport(self.source, self.target)
1366
(old_sha1, result.old_revid) = old_refs.get(
1367
main_ref, (ZERO_SHA, NULL_REVISION))
1368
if result.old_revid is None:
1369
result.old_revid = self.target.lookup_foreign_revision_id(
1371
result.new_revid = new_refs[main_ref][1]
1372
result.local_branch = None
1373
result.master_branch = self.target
1375
for hook in branch.Branch.hooks['post_pull']:
1379
def push(self, overwrite=False, stop_revision=None, lossy=False,
1380
_override_hook_source_branch=None, _stop_revno=None):
1381
result = GitBranchPushResult()
1382
result.source_branch = self.source
1383
result.target_branch = self.target
1384
result.local_branch = None
1385
result.master_branch = result.target_branch
1386
with self.source.lock_read(), self.target.lock_write():
1387
new_refs, main_ref, stop_revinfo = self._get_new_refs(
1388
stop_revision, stop_revno=_stop_revno)
1390
def update_refs(old_refs):
1391
return self._update_refs(result, old_refs, new_refs, overwrite)
1393
result.revidmap, old_refs, new_refs = (
1394
self.interrepo.fetch_refs(
1395
update_refs, lossy=lossy, overwrite=overwrite))
1396
except NoPushSupport:
1397
raise errors.NoRoundtrippingSupport(self.source, self.target)
1398
(old_sha1, result.old_revid) = old_refs.get(
1399
main_ref, (ZERO_SHA, NULL_REVISION))
1400
if result.old_revid is None:
1401
result.old_revid = self.target.lookup_foreign_revision_id(
1403
result.new_revid = new_refs[main_ref][1]
1404
(result.new_original_revno,
1405
result.new_original_revid) = stop_revinfo
1406
for hook in branch.Branch.hooks['post_push']:
1411
branch.InterBranch.register_optimiser(InterGitLocalGitBranch)
728
1412
branch.InterBranch.register_optimiser(InterFromGitBranch)
729
1413
branch.InterBranch.register_optimiser(InterToGitBranch)
730
branch.InterBranch.register_optimiser(InterGitLocalRemoteBranch)
1414
branch.InterBranch.register_optimiser(InterLocalGitRemoteGitBranch)