/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 unpeel_map.py

  • Committer: Jelmer Vernooij
  • Date: 2018-04-02 14:59:43 UTC
  • mto: (0.200.1913 work)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@jelmer.uk-20180402145943-s5jmpbvvf1x42pao
Just don't touch the URL if it's already a valid URL.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2011-2018 Jelmer Vernooij <jelmer@jelmer.uk>
 
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Unpeel map storage."""
 
18
 
 
19
from __future__ import absolute_import
 
20
 
 
21
from collections import defaultdict
 
22
from cStringIO import StringIO
 
23
 
 
24
from ... import (
 
25
    errors,
 
26
    trace,
 
27
    )
 
28
 
 
29
 
 
30
class UnpeelMap(object):
 
31
    """Unpeel map.
 
32
 
 
33
    Keeps track of the unpeeled object id of tags.
 
34
    """
 
35
 
 
36
    def __init__(self):
 
37
        self._map = defaultdict(set)
 
38
        self._re_map = {}
 
39
 
 
40
    def update(self, m):
 
41
        for k, v in m.iteritems():
 
42
            self._map[k].update(v)
 
43
            for i in v:
 
44
                self._re_map[i] = k
 
45
 
 
46
    def load(self, f):
 
47
        firstline = f.readline()
 
48
        if firstline != "unpeel map version 1\n":
 
49
            raise AssertionError("invalid format for unpeel map: %r" % firstline)
 
50
        for l in f.readlines():
 
51
            (k, v) = l.split(":", 1)
 
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):
 
58
        f.write("unpeel map version 1\n")
 
59
        for k, vs in self._map.iteritems():
 
60
            for v in vs:
 
61
                f.write("%s: %s\n" % (k, v))
 
62
 
 
63
    def save_in_repository(self, repository):
 
64
        f = StringIO()
 
65
        try:
 
66
            self.save(f)
 
67
            f.seek(0)
 
68
            repository.control_transport.put_file("git-unpeel-map", f)
 
69
        finally:
 
70
            f.close()
 
71
 
 
72
    def peel_tag(self, git_sha, default=None):
 
73
        """Peel a tag."""
 
74
        return self._re_map.get(git_sha, default)
 
75
 
 
76
    def re_unpeel_tag(self, new_git_sha, old_git_sha):
 
77
        """Re-unpeel a tag.
 
78
 
 
79
        Bazaar can't store unpeeled refs so in order to prevent peeling
 
80
        existing tags when pushing they are "unpeeled" here.
 
81
        """
 
82
        if old_git_sha is not None and old_git_sha in self._map[new_git_sha]:
 
83
            trace.mutter("re-unpeeling %r to %r", new_git_sha, old_git_sha)
 
84
            return old_git_sha
 
85
        return new_git_sha
 
86
 
 
87
    @classmethod
 
88
    def from_repository(cls, repository):
 
89
        """Load the unpeel map for a repository.
 
90
        """
 
91
        m = UnpeelMap()
 
92
        try:
 
93
            m.load(repository.control_transport.get("git-unpeel-map"))
 
94
        except errors.NoSuchFile:
 
95
            pass
 
96
        return m