/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

Some minor doc fixes

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
        assert f.readline() == "unpeel map version 1\n"
 
201
        for l in f.readlines():
 
202
            (k, v) = l.split(":", 1)
 
203
            self._map[k.strip()].add(v.strip())
 
204
 
 
205
    def save(self, f):
 
206
        f.write("unpeel map version 1\n")
 
207
        for k, vs in self._map.iteritems():
 
208
            for v in vs:
 
209
                f.write("%s: %s\n" % (k, v))
 
210
 
 
211
    def save_in_repository(self, repository):
 
212
        f = StringIO()
 
213
        try:
 
214
            self.save(f)
 
215
            f.seek(0)
 
216
            repository.control_transport.put_file("git-unpeel-map", f)
 
217
        finally:
 
218
            f.close()
 
219
 
 
220
    def re_unpeel_tag(self, new_git_sha, old_git_sha):
 
221
        """Re-unpeel tags.
 
222
 
 
223
        Bazaar can't store unpeeled refs so in order to prevent peeling
 
224
        existing tags when pushing they are "re-peeled" here.
 
225
        """
 
226
        if old_git_sha is not None and old_git_sha in self._map[new_git_sha]:
 
227
            trace.mutter("re-unpeeling %r to %r", new_git_sha, old_git_sha)
 
228
            return old_git_sha
 
229
        return new_git_sha
 
230
 
 
231
    @classmethod
 
232
    def from_repository(cls, repository):
 
233
        """Load the unpeel map for a repository.
 
234
        """
 
235
        m = UnpeelMap()
 
236
        try:
 
237
            m.load(repository.control_transport.get("git-unpeel-map"))
 
238
        except errors.NoSuchFile:
 
239
            pass
 
240
        return m