/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
 
        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