71
def branch_name_to_ref(name):
72
"""Map a branch name to a ref.
74
:param name: Branch name
77
if name is None or name == "HEAD":
79
if not name.startswith("refs/"):
80
return "refs/heads/%s" % name
85
def ref_to_branch_name(ref):
86
"""Map a ref to a branch name
89
:return: A branch name
93
if ref.startswith("refs/heads/"):
94
return ref[len("refs/heads/"):]
95
raise ValueError("unable to map ref %s back to branch name")
66
98
class GitPullResult(branch.PullResult):
68
100
def _lookup_revno(self, revid):
104
136
ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
139
def _set_tag_dict(self, to_dict):
140
extra = set(self.repository._git.get_refs().keys())
141
for k, revid in to_dict.iteritems():
142
name = "refs/tags/%s" % k
145
self.set_tag(k, revid)
147
if name.startswith("refs/tags/"):
148
del self.repository._git[name]
107
150
def set_tag(self, name, revid):
108
151
self.repository._git.refs["refs/tags/%s" % name], _ = \
109
152
self.branch.mapping.revision_id_bzr_to_foreign(revid)
147
188
class GitBranch(ForeignBranch):
148
189
"""An adapter to git repositories for bzr Branch objects."""
150
def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
191
def __init__(self, bzrdir, repository, ref, lockfiles, tagsdict=None):
151
192
self.repository = repository
152
193
self._format = GitBranchFormat()
153
194
self.control_files = lockfiles
185
227
nick = property(_get_nick, _set_nick)
187
229
def __repr__(self):
188
return "%s(%r, %r)" % (self.__class__.__name__, self.repository.base, self.name)
230
return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
190
233
def generate_revision_history(self, revid, old_revid=None):
191
234
# FIXME: Check that old_revid is in the ancestry of revid
235
278
class LocalGitBranch(GitBranch):
236
279
"""A local Git branch."""
281
def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
282
super(LocalGitBranch, self).__init__(bzrdir, repository, name,
284
if not name in repository._git.get_refs().keys():
285
raise errors.NotBranchError(self.base)
238
287
def create_checkout(self, to_location, revision_id=None, lightweight=False,
239
288
accelerator_tree=None, hardlink=False):
361
412
not isinstance(target, GitBranch) and
362
413
(getattr(cls._get_interrepo(source, target), "fetch_objects", None) is not None))
364
def update_revisions(self, stop_revision=None, overwrite=False,
366
"""See InterBranch.update_revisions()."""
415
def _update_revisions(self, stop_revision=None, overwrite=False,
416
graph=None, limit=None):
417
"""Like InterBranch.update_revisions(), but with additions.
419
Compared to the `update_revisions()` below, this function takes a
420
`limit` argument that limits how many git commits will be converted
421
and returns the new git head.
367
423
interrepo = self._get_interrepo(self.source, self.target)
369
self._last_revid = None
370
424
def determine_wants(heads):
371
if not self.source.name in heads:
372
raise NoSuchRef(self.source.name, heads.keys())
425
if not self.source.ref in heads:
426
raise NoSuchRef(self.source.ref, heads.keys())
373
427
if stop_revision is not None:
374
428
self._last_revid = stop_revision
375
self._head, mapping = self.source.repository.lookup_bzr_revision_id(
429
head, mapping = self.source.repository.lookup_bzr_revision_id(
378
self._head = heads[self.source.name]
380
self.source.mapping.revision_id_foreign_to_bzr(self._head)
432
head = heads[self.source.ref]
433
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
381
435
if self.target.repository.has_revision(self._last_revid):
384
interrepo.fetch_objects(determine_wants, self.source.mapping)
438
pack_hint, head = interrepo.fetch_objects(
439
determine_wants, self.source.mapping, limit=limit)
440
if pack_hint is not None and self.target.repository._format.pack_compresses:
441
self.target.repository.pack(hint=pack_hint)
443
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
386
445
prev_last_revid = None
388
447
prev_last_revid = self.target.last_revision()
389
self.target.generate_revision_history(self._last_revid, prev_last_revid)
448
self.target.generate_revision_history(self._last_revid,
452
def update_revisions(self, stop_revision=None, overwrite=False,
454
"""See InterBranch.update_revisions()."""
455
self._update_revisions(stop_revision, overwrite, graph)
391
457
def pull(self, overwrite=False, stop_revision=None,
392
458
possible_transports=None, _hook_master=None, run_hooks=True,
393
_override_hook_target=None, local=False):
459
_override_hook_target=None, local=False, limit=None):
394
460
"""See Branch.pull.
396
462
:param _hook_master: Private parameter - set the branch to
415
483
# We assume that during 'pull' the target repository is closer than
416
484
# the source one.
417
485
graph = self.target.repository.get_graph(self.source.repository)
418
result.old_revno, result.old_revid = \
486
(result.old_revno, result.old_revid) = \
419
487
self.target.last_revision_info()
420
self.update_revisions(stop_revision, overwrite=overwrite,
422
result.new_git_head = self._head
488
result.new_git_head = self._update_revisions(
489
stop_revision, overwrite=overwrite, graph=graph, limit=limit)
423
490
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
425
result.new_revno, result.new_revid = self.target.last_revision_info()
492
(result.new_revno, result.new_revid) = \
493
self.target.last_revision_info()
427
495
result.master_branch = _hook_master
428
496
result.local_branch = result.target_branch
442
510
result.target_branch = self.target
443
511
graph = self.target.repository.get_graph(self.source.repository)
444
512
result.old_revno, result.old_revid = self.target.last_revision_info()
445
self.update_revisions(stop_revision, overwrite=overwrite, graph=graph)
446
result.new_git_head = self._head
513
result.new_git_head = self._update_revisions(
514
stop_revision, overwrite=overwrite, graph=graph)
447
515
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
449
517
result.new_revno, result.new_revid = self.target.last_revision_info()
518
586
return refs, stop_revision
520
588
def pull(self, stop_revision=None, overwrite=False,
521
possible_transports=None, local=False):
589
possible_transports=None, run_hooks=True,local=False):
522
590
# This type of branch can't be bound.
524
592
raise errors.LocalRequiresBoundBranch()