/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 refs.py

Fix two mistakes in 'bzr help git'.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Conversion between refs and Bazaar revision pointers."""
18
18
 
 
19
from collections import defaultdict
 
20
from cStringIO import StringIO
 
21
 
19
22
from dulwich.repo import (
20
23
    RefsContainer,
21
24
    )
22
25
 
23
26
from bzrlib import (
24
27
    errors,
 
28
    trace,
25
29
    )
26
30
 
 
31
is_tag = lambda x: x.startswith("refs/tags/")
 
32
 
27
33
 
28
34
def extract_tags(refs):
29
35
    """Extract the tags from a refs dictionary.
30
36
 
31
37
    :param refs: Refs to extract the tags from.
32
 
    :return: Dictionary mapping tag names to SHA1s.
 
38
    :return: Dictionary mapping tag names to SHA1s of the actual object
 
39
        and unpeeled object SHA1s.
33
40
    """
34
41
    ret = {}
35
 
    for k,v in refs.iteritems():
36
 
        if k.startswith("refs/tags/") and not k.endswith("^{}"):
37
 
            v = refs.get(k+"^{}", v)
 
42
    for k, v in refs.iteritems():
 
43
        if is_tag(k) and not k.endswith("^{}"):
 
44
            try:
 
45
                peeled = refs[k+"^{}"]
 
46
                unpeeled = v
 
47
            except KeyError:
 
48
                peeled = v
 
49
                unpeeled = None
38
50
            try:
39
51
                tagname = ref_to_tag_name(k)
40
52
            except UnicodeDecodeError:
41
53
                pass
42
54
            else:
43
 
                ret[tagname] = v
 
55
                ret[tagname] = (peeled, unpeeled)
44
56
    return ret
45
57
 
46
58
 
85
97
def ref_to_tag_name(ref):
86
98
    if ref.startswith("refs/tags/"):
87
99
        return ref[len('refs/tags/'):].decode("utf-8")
88
 
    raise ValueError("unable to map ref %s back to branch name" % ref)
 
100
    raise ValueError("unable to map ref %s back to tag name" % ref)
89
101
 
90
102
 
91
103
class BazaarRefsContainer(RefsContainer):
169
181
            target_branch.generate_revision_history(rev_id)
170
182
        finally:
171
183
            target_branch.unlock()
 
184
 
 
185
 
 
186
class UnpeelMap(object):
 
187
    """Unpeel map.
 
188
 
 
189
    Keeps track of the unpeeled object id of tags.
 
190
    """
 
191
 
 
192
    def __init__(self):
 
193
        self._map = defaultdict(set)
 
194
 
 
195
    def update(self, m):
 
196
        for k, v in m.iteritems():
 
197
            self._map[k].update(v)
 
198
 
 
199
    def load(self, f):
 
200
        firstline = f.readline()
 
201
        if firstline != "unpeel map version 1\n":
 
202
            raise AssertionError("invalid format for unpeel map: %r" % firstline)
 
203
        for l in f.readlines():
 
204
            (k, v) = l.split(":", 1)
 
205
            self._map[k.strip()].add(v.strip())
 
206
 
 
207
    def save(self, f):
 
208
        f.write("unpeel map version 1\n")
 
209
        for k, vs in self._map.iteritems():
 
210
            for v in vs:
 
211
                f.write("%s: %s\n" % (k, v))
 
212
 
 
213
    def save_in_repository(self, repository):
 
214
        f = StringIO()
 
215
        try:
 
216
            self.save(f)
 
217
            f.seek(0)
 
218
            repository.control_transport.put_file("git-unpeel-map", f)
 
219
        finally:
 
220
            f.close()
 
221
 
 
222
    def re_unpeel_tag(self, new_git_sha, old_git_sha):
 
223
        """Re-unpeel tags.
 
224
 
 
225
        Bazaar can't store unpeeled refs so in order to prevent peeling
 
226
        existing tags when pushing they are "re-peeled" here.
 
227
        """
 
228
        if old_git_sha is not None and old_git_sha in self._map[new_git_sha]:
 
229
            trace.mutter("re-unpeeling %r to %r", new_git_sha, old_git_sha)
 
230
            return old_git_sha
 
231
        return new_git_sha
 
232
 
 
233
    @classmethod
 
234
    def from_repository(cls, repository):
 
235
        """Load the unpeel map for a repository.
 
236
        """
 
237
        m = UnpeelMap()
 
238
        try:
 
239
            m.load(repository.control_transport.get("git-unpeel-map"))
 
240
        except errors.NoSuchFile:
 
241
            pass
 
242
        return m