/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.358.2 by Jelmer Vernooij
Refresh copyright headers, add my email.
1
# Copyright (C) 2011-2018 Jelmer Vernooij <jelmer@jelmer.uk>
0.200.1292 by Jelmer Vernooij
Fix repeeling objects when determining what to send.
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
0.358.1 by Jelmer Vernooij
Fix FSF address.
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0.200.1292 by Jelmer Vernooij
Fix repeeling objects when determining what to send.
16
17
"""Unpeel map storage."""
18
0.200.1594 by Jelmer Vernooij
Use absolute_import everywhere.
19
0.200.1292 by Jelmer Vernooij
Fix repeeling objects when determining what to send.
20
from collections import defaultdict
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
21
from io import BytesIO
0.200.1292 by Jelmer Vernooij
Fix repeeling objects when determining what to send.
22
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
23
from .. import (
0.200.1292 by Jelmer Vernooij
Fix repeeling objects when determining what to send.
24
    errors,
25
    trace,
26
    )
27
28
29
class UnpeelMap(object):
30
    """Unpeel map.
31
32
    Keeps track of the unpeeled object id of tags.
33
    """
34
35
    def __init__(self):
36
        self._map = defaultdict(set)
37
        self._re_map = {}
38
39
    def update(self, m):
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
40
        for k, v in m.items():
0.200.1292 by Jelmer Vernooij
Fix repeeling objects when determining what to send.
41
            self._map[k].update(v)
0.200.1293 by Jelmer Vernooij
Fix unpeel map.
42
            for i in v:
43
                self._re_map[i] = k
0.200.1292 by Jelmer Vernooij
Fix repeeling objects when determining what to send.
44
45
    def load(self, f):
46
        firstline = f.readline()
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
47
        if firstline != b"unpeel map version 1\n":
7143.15.2 by Jelmer Vernooij
Run autopep8.
48
            raise AssertionError(
49
                "invalid format for unpeel map: %r" % firstline)
0.200.1292 by Jelmer Vernooij
Fix repeeling objects when determining what to send.
50
        for l in f.readlines():
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
51
            (k, v) = l.split(b":", 1)
0.200.1292 by Jelmer Vernooij
Fix repeeling objects when determining what to send.
52
            k = k.strip()
53
            v = v.strip()
54
            self._map[k].add(v)
55
            self._re_map[v] = k
56
57
    def save(self, f):
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
58
        f.write(b"unpeel map version 1\n")
59
        for k, vs in self._map.items():
0.200.1292 by Jelmer Vernooij
Fix repeeling objects when determining what to send.
60
            for v in vs:
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
61
                f.write(b"%s: %s\n" % (k, v))
0.200.1292 by Jelmer Vernooij
Fix repeeling objects when determining what to send.
62
63
    def save_in_repository(self, repository):
7356.1.5 by Jelmer Vernooij
Use more ExitStacks.
64
        with BytesIO() as f:
0.200.1292 by Jelmer Vernooij
Fix repeeling objects when determining what to send.
65
            self.save(f)
66
            f.seek(0)
67
            repository.control_transport.put_file("git-unpeel-map", f)
68
69
    def peel_tag(self, git_sha, default=None):
70
        """Peel a tag."""
71
        return self._re_map.get(git_sha, default)
72
73
    def re_unpeel_tag(self, new_git_sha, old_git_sha):
74
        """Re-unpeel a tag.
75
76
        Bazaar can't store unpeeled refs so in order to prevent peeling
77
        existing tags when pushing they are "unpeeled" here.
78
        """
79
        if old_git_sha is not None and old_git_sha in self._map[new_git_sha]:
80
            trace.mutter("re-unpeeling %r to %r", new_git_sha, old_git_sha)
81
            return old_git_sha
82
        return new_git_sha
83
84
    @classmethod
85
    def from_repository(cls, repository):
86
        """Load the unpeel map for a repository.
87
        """
88
        m = UnpeelMap()
89
        try:
90
            m.load(repository.control_transport.get("git-unpeel-map"))
91
        except errors.NoSuchFile:
92
            pass
93
        return m