/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.1292 by Jelmer Vernooij
Fix repeeling objects when determining what to send.
1
# Copyright (C) 2011 Jelmer Vernooij <jelmer@samba.org>
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Unpeel map storage."""
18
19
from collections import defaultdict
20
from cStringIO import StringIO
21
22
from bzrlib import (
23
    errors,
24
    trace,
25
    )
26
27
28
class UnpeelMap(object):
29
    """Unpeel map.
30
31
    Keeps track of the unpeeled object id of tags.
32
    """
33
34
    def __init__(self):
35
        self._map = defaultdict(set)
36
        self._re_map = {}
37
38
    def update(self, m):
39
        for k, v in m.iteritems():
40
            self._map[k].update(v)
0.200.1293 by Jelmer Vernooij
Fix unpeel map.
41
            for i in v:
42
                self._re_map[i] = k
0.200.1292 by Jelmer Vernooij
Fix repeeling objects when determining what to send.
43
44
    def load(self, f):
45
        firstline = f.readline()
46
        if firstline != "unpeel map version 1\n":
47
            raise AssertionError("invalid format for unpeel map: %r" % firstline)
48
        for l in f.readlines():
49
            (k, v) = l.split(":", 1)
50
            k = k.strip()
51
            v = v.strip()
52
            self._map[k].add(v)
53
            self._re_map[v] = k
54
55
    def save(self, f):
56
        f.write("unpeel map version 1\n")
57
        for k, vs in self._map.iteritems():
58
            for v in vs:
59
                f.write("%s: %s\n" % (k, v))
60
61
    def save_in_repository(self, repository):
62
        f = StringIO()
63
        try:
64
            self.save(f)
65
            f.seek(0)
66
            repository.control_transport.put_file("git-unpeel-map", f)
67
        finally:
68
            f.close()
69
70
    def peel_tag(self, git_sha, default=None):
71
        """Peel a tag."""
72
        return self._re_map.get(git_sha, default)
73
74
    def re_unpeel_tag(self, new_git_sha, old_git_sha):
75
        """Re-unpeel a tag.
76
77
        Bazaar can't store unpeeled refs so in order to prevent peeling
78
        existing tags when pushing they are "unpeeled" here.
79
        """
80
        if old_git_sha is not None and old_git_sha in self._map[new_git_sha]:
81
            trace.mutter("re-unpeeling %r to %r", new_git_sha, old_git_sha)
82
            return old_git_sha
83
        return new_git_sha
84
85
    @classmethod
86
    def from_repository(cls, repository):
87
        """Load the unpeel map for a repository.
88
        """
89
        m = UnpeelMap()
90
        try:
91
            m.load(repository.control_transport.get("git-unpeel-map"))
92
        except errors.NoSuchFile:
93
            pass
94
        return m