57
57
def extract_tags(refs):
58
"""Extract the tags from a refs dictionary.
60
:param refs: Refs to extract the tags from.
61
:return: Dictionary mapping tag names to SHA1s.
64
59
for k,v in refs.iteritems():
65
60
if k.startswith("refs/tags/") and not k.endswith("^{}"):
71
def branch_name_to_ref(name, default=None):
72
"""Map a branch name to a ref.
74
:param name: Branch name
81
if not name.startswith("refs/"):
82
return "refs/heads/%s" % name
87
def ref_to_branch_name(ref):
88
"""Map a ref to a branch name
91
:return: A branch name
95
if ref.startswith("refs/heads/"):
96
return ref[len("refs/heads/"):]
97
raise ValueError("unable to map ref %s back to branch name")
100
66
class GitPullResult(branch.PullResult):
102
68
def _lookup_revno(self, revid):
157
123
class DictTagDict(LocalGitTagDict):
159
126
def __init__(self, branch, tags):
160
127
super(DictTagDict, self).__init__(branch)
161
128
self._tags = tags
190
158
class GitBranch(ForeignBranch):
191
159
"""An adapter to git repositories for bzr Branch objects."""
193
def __init__(self, bzrdir, repository, ref, lockfiles, tagsdict=None):
161
def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
194
162
self.repository = repository
195
163
self._format = GitBranchFormat()
196
164
self.control_files = lockfiles
198
166
super(GitBranch, self).__init__(repository.get_mapping())
199
167
if tagsdict is not None:
200
168
self.tags = DictTagDict(self, tagsdict)
202
self.name = ref_to_branch_name(ref)
203
170
self._head = None
204
171
self.base = bzrdir.root_transport.base
172
if self.name != "HEAD":
173
self.base += ",%s" % self.name
206
175
def _get_checkout_format(self):
207
176
"""Return the most suitable metadir for a checkout of this branch.
229
198
nick = property(_get_nick, _set_nick)
231
200
def __repr__(self):
232
return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
201
return "%s(%r, %r)" % (self.__class__.__name__, self.repository.base, self.name)
235
203
def generate_revision_history(self, revid, old_revid=None):
236
204
# FIXME: Check that old_revid is in the ancestry of revid
280
248
class LocalGitBranch(GitBranch):
281
249
"""A local Git branch."""
283
def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
284
super(LocalGitBranch, self).__init__(bzrdir, repository, name,
286
if not name in repository._git.get_refs().keys():
287
raise errors.NotBranchError(self.base)
289
251
def create_checkout(self, to_location, revision_id=None, lightweight=False,
290
252
accelerator_tree=None, hardlink=False):
345
307
def _set_head(self, value):
346
308
self._head = value
347
self.repository._git.refs[self.ref] = self._head
309
self.repository._git.refs[self.name] = self._head
348
310
self._clear_cached_state()
350
312
head = property(_get_head, _set_head)
425
387
interrepo = self._get_interrepo(self.source, self.target)
426
388
def determine_wants(heads):
427
if not self.source.ref in heads:
428
raise NoSuchRef(self.source.ref, heads.keys())
389
if not self.source.name in heads:
390
raise NoSuchRef(self.source.name, heads.keys())
429
391
if stop_revision is not None:
430
392
self._last_revid = stop_revision
431
393
head, mapping = self.source.repository.lookup_bzr_revision_id(
434
head = heads[self.source.ref]
396
head = heads[self.source.name]
435
397
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
437
399
if self.target.repository.has_revision(self._last_revid):
440
pack_hint, head = interrepo.fetch_objects(
402
_, head = interrepo.fetch_objects(
441
403
determine_wants, self.source.mapping, limit=limit)
442
if pack_hint is not None and self.target.repository._format.pack_compresses:
443
self.target.repository.pack(hint=pack_hint)
444
404
if head is not None:
445
405
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
447
407
prev_last_revid = None
449
409
prev_last_revid = self.target.last_revision()
450
self.target.generate_revision_history(self._last_revid,
410
self.target.generate_revision_history(self._last_revid, prev_last_revid)
454
413
def update_revisions(self, stop_revision=None, overwrite=False,
541
500
stop_revision = self.source.last_revision()
542
501
# FIXME: Check for diverged branches
543
502
def get_changed_refs(old_refs):
544
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get(self.target.ref, "0" * 40))
545
refs = { self.target.ref: self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
503
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get("refs/heads/master", "0" * 40))
504
refs = { "refs/heads/master": self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
546
505
result.new_revid = stop_revision
547
506
for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
548
507
refs["refs/tags/%s" % name] = sha
626
585
result = GitBranchPushResult()
627
586
result.source_branch = self.source
628
587
result.target_branch = self.target
589
result.old_revid = self.target.last_revision()
591
result.old_revid = revision.NULL_REVISION
629
592
if stop_revision is None:
630
593
stop_revision = self.source.last_revision()
631
594
# FIXME: Check for diverged branches
632
refs = { self.target.ref: stop_revision }
595
refs = { "refs/heads/master": stop_revision }
633
596
for name, revid in self.source.tags.get_tag_dict().iteritems():
634
597
if self.source.repository.has_revision(revid):
635
598
refs["refs/tags/%s" % name] = revid
636
revidmap, old_refs, new_refs = self.target.repository.dfetch_refs(
599
revidmap, new_refs = self.target.repository.dfetch_refs(
637
600
self.source.repository, refs)
638
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(
639
old_refs.get(self.target.ref, "0" * 40))
640
result.new_revid = self.target.mapping.revision_id_foreign_to_bzr(
641
new_refs[self.target.ref])
602
self.target.generate_revision_history(revidmap[stop_revision])
603
result.new_revid = revidmap[stop_revision]
605
result.new_revid = result.old_revid
642
606
result.revidmap = revidmap