/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

More work on roundtrip push support.

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
 
 
22
19
from dulwich.repo import (
23
20
    RefsContainer,
24
21
    )
25
22
 
26
23
from bzrlib import (
27
24
    errors,
28
 
    trace,
29
25
    )
30
26
 
31
 
is_tag = lambda x: x.startswith("refs/tags/")
32
 
 
33
27
 
34
28
def extract_tags(refs):
35
29
    """Extract the tags from a refs dictionary.
36
30
 
37
31
    :param refs: Refs to extract the tags from.
38
 
    :return: Dictionary mapping tag names to SHA1s of the actual object
39
 
        and unpeeled object SHA1s.
 
32
    :return: Dictionary mapping tag names to SHA1s.
40
33
    """
41
34
    ret = {}
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
 
35
    for k,v in refs.iteritems():
 
36
        if k.startswith("refs/tags/") and not k.endswith("^{}"):
 
37
            v = refs.get(k+"^{}", v)
50
38
            try:
51
39
                tagname = ref_to_tag_name(k)
52
40
            except UnicodeDecodeError:
53
41
                pass
54
42
            else:
55
 
                ret[tagname] = (peeled, unpeeled)
 
43
                ret[tagname] = v
56
44
    return ret
57
45
 
58
46
 
97
85
def ref_to_tag_name(ref):
98
86
    if ref.startswith("refs/tags/"):
99
87
        return ref[len('refs/tags/'):].decode("utf-8")
100
 
    raise ValueError("unable to map ref %s back to tag name" % ref)
 
88
    raise ValueError("unable to map ref %s back to branch name" % ref)
101
89
 
102
90
 
103
91
class BazaarRefsContainer(RefsContainer):
181
169
            target_branch.generate_revision_history(rev_id)
182
170
        finally:
183
171
            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