/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to branch.py

Implement to_files() for git merge directives.

Show diffs side-by-side

added added

removed removed

Lines of Context:
55
55
 
56
56
 
57
57
def extract_tags(refs):
58
 
    """Extract the tags from a refs dictionary.
59
 
 
60
 
    :param refs: Refs to extract the tags from.
61
 
    :return: Dictionary mapping tag names to SHA1s.
62
 
    """
63
58
    ret = {}
64
59
    for k,v in refs.iteritems():
65
60
        if k.startswith("refs/tags/") and not k.endswith("^{}"):
68
63
    return ret
69
64
 
70
65
 
71
 
def branch_name_to_ref(name, default=None):
72
 
    """Map a branch name to a ref.
73
 
 
74
 
    :param name: Branch name
75
 
    :return: ref string
76
 
    """
77
 
    if name is None:
78
 
        return default
79
 
    if name == "HEAD":
80
 
        return "HEAD"
81
 
    if not name.startswith("refs/"):
82
 
        return "refs/heads/%s" % name
83
 
    else:
84
 
        return name
85
 
 
86
 
 
87
 
def ref_to_branch_name(ref):
88
 
    """Map a ref to a branch name
89
 
 
90
 
    :param ref: Ref
91
 
    :return: A branch name
92
 
    """
93
 
    if ref == "HEAD":
94
 
        return "HEAD"
95
 
    if ref.startswith("refs/heads/"):
96
 
        return ref[len("refs/heads/"):]
97
 
    raise ValueError("unable to map ref %s back to branch name")
98
 
 
99
 
 
100
66
class GitPullResult(branch.PullResult):
101
67
 
102
68
    def _lookup_revno(self, revid):
156
122
 
157
123
class DictTagDict(LocalGitTagDict):
158
124
 
 
125
 
159
126
    def __init__(self, branch, tags):
160
127
        super(DictTagDict, self).__init__(branch)
161
128
        self._tags = tags
164
131
        return self._tags
165
132
 
166
133
 
 
134
 
167
135
class GitBranchFormat(branch.BranchFormat):
168
136
 
169
137
    def get_format_description(self):
190
158
class GitBranch(ForeignBranch):
191
159
    """An adapter to git repositories for bzr Branch objects."""
192
160
 
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)
201
 
        self.ref = ref
202
 
        self.name = ref_to_branch_name(ref)
 
169
        self.name = name
203
170
        self._head = None
204
171
        self.base = bzrdir.root_transport.base
 
172
        if self.name != "HEAD":
 
173
            self.base += ",%s" % self.name
205
174
 
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)
230
199
 
231
200
    def __repr__(self):
232
 
        return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
233
 
            self.ref)
 
201
        return "%s(%r, %r)" % (self.__class__.__name__, self.repository.base, self.name)
234
202
 
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."""
282
250
 
283
 
    def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
284
 
        super(LocalGitBranch, self).__init__(bzrdir, repository, name, 
285
 
              lockfiles, tagsdict)
286
 
        if not name in repository._git.get_refs().keys():
287
 
            raise errors.NotBranchError(self.base)
288
 
 
289
251
    def create_checkout(self, to_location, revision_id=None, lightweight=False,
290
252
        accelerator_tree=None, hardlink=False):
291
253
        if lightweight:
330
292
 
331
293
    def _get_head(self):
332
294
        try:
333
 
            return self.repository._git.ref(self.ref)
 
295
            return self.repository._git.ref(self.name)
334
296
        except KeyError:
335
297
            return None
336
298
 
344
306
 
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()
349
311
 
350
312
    head = property(_get_head, _set_head)
424
386
        """
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(
432
394
                    stop_revision)
433
395
            else:
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(
436
398
                    head)
437
399
            if self.target.repository.has_revision(self._last_revid):
438
400
                return []
439
401
            return [head]
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)
446
406
        if overwrite:
447
407
            prev_last_revid = None
448
408
        else:
449
409
            prev_last_revid = self.target.last_revision()
450
 
        self.target.generate_revision_history(self._last_revid,
451
 
            prev_last_revid)
 
410
        self.target.generate_revision_history(self._last_revid, prev_last_revid)
452
411
        return head
453
412
 
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
 
588
        try:
 
589
            result.old_revid = self.target.last_revision()
 
590
        except NoSuchRef:
 
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])
 
601
        if revidmap != {}:
 
602
            self.target.generate_revision_history(revidmap[stop_revision])
 
603
            result.new_revid = revidmap[stop_revision]
 
604
        else:
 
605
            result.new_revid = result.old_revid
642
606
        result.revidmap = revidmap
643
607
        return result
644
608