/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

update copyright years

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010 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
"""Conversion between refs and Bazaar revision pointers."""
 
18
 
 
19
from dulwich.repo import (
 
20
    RefsContainer,
 
21
    )
 
22
 
 
23
from bzrlib import (
 
24
    errors,
 
25
    )
 
26
 
 
27
 
 
28
def extract_tags(refs):
 
29
    """Extract the tags from a refs dictionary.
 
30
 
 
31
    :param refs: Refs to extract the tags from.
 
32
    :return: Dictionary mapping tag names to SHA1s.
 
33
    """
 
34
    ret = {}
 
35
    for k,v in refs.iteritems():
 
36
        if k.startswith("refs/tags/") and not k.endswith("^{}"):
 
37
            v = refs.get(k+"^{}", v)
 
38
            ret[k[len("refs/tags/"):]] = v
 
39
    return ret
 
40
 
 
41
 
 
42
def branch_name_to_ref(name, default=None):
 
43
    """Map a branch name to a ref.
 
44
 
 
45
    :param name: Branch name
 
46
    :return: ref string
 
47
    """
 
48
    if name is None:
 
49
        return default
 
50
    if name == "HEAD":
 
51
        return "HEAD"
 
52
    if not name.startswith("refs/"):
 
53
        return "refs/heads/%s" % name
 
54
    else:
 
55
        return name
 
56
 
 
57
 
 
58
def tag_name_to_ref(name):
 
59
    """Map a tag name to a ref.
 
60
 
 
61
    :param name: Tag name
 
62
    :return: ref string
 
63
    """
 
64
    return "refs/tags/%s" % name
 
65
 
 
66
 
 
67
def ref_to_branch_name(ref):
 
68
    """Map a ref to a branch name
 
69
 
 
70
    :param ref: Ref
 
71
    :return: A branch name
 
72
    """
 
73
    if ref == "HEAD":
 
74
        return "HEAD"
 
75
    if ref.startswith("refs/heads/"):
 
76
        return ref[len("refs/heads/"):]
 
77
    raise ValueError("unable to map ref %s back to branch name" % ref)
 
78
 
 
79
 
 
80
def ref_to_tag_name(ref):
 
81
    if ref.startswith("refs/tags/"):
 
82
        return ref[len('refs/tags/'):]
 
83
    raise ValueError("unable to map ref %s back to branch name" % ref)
 
84
 
 
85
 
 
86
class BazaarRefsContainer(RefsContainer):
 
87
 
 
88
    def __init__(self, dir, object_store):
 
89
        self.dir = dir
 
90
        self.object_store = object_store
 
91
 
 
92
    def set_symbolic_ref(self, name, other):
 
93
        if name == "HEAD":
 
94
            pass # FIXME: Switch default branch
 
95
        else:
 
96
            raise NotImplementedError(
 
97
                "Symbolic references not supported for anything other than "
 
98
                "HEAD")
 
99
 
 
100
    def _get_revid_by_tag_name(self, tag_name):
 
101
        for branch in self.dir.list_branches():
 
102
            try:
 
103
                # FIXME: This is ambiguous!
 
104
                return branch.tags.lookup_tag(tag_name)
 
105
            except errors.NoSuchTag:
 
106
                pass
 
107
        return None
 
108
 
 
109
    def _get_revid_by_branch_name(self, branch_name):
 
110
        try:
 
111
            branch = self.dir.open_branch(branch_name)
 
112
        except errors.NoColocatedBranchSupport:
 
113
            raise
 
114
        return branch.last_revision()
 
115
 
 
116
    def read_loose_ref(self, ref):
 
117
        try:
 
118
            branch_name = ref_to_branch_name(ref)
 
119
        except ValueError:
 
120
            tag_name = ref_to_tag_name(ref)
 
121
            revid = self._get_revid_by_tag_name(tag_name)
 
122
        else:
 
123
            revid = self._get_revid_by_branch_name(branch_name)
 
124
        return self.object_store._lookup_revision_sha1(revid)
 
125
 
 
126
    def allkeys(self):
 
127
        keys = set()
 
128
        for branch in self.dir.list_branches():
 
129
            repo = branch.repository
 
130
            if repo.has_revision(branch.last_revision()):
 
131
                keys.add(branch_name_to_ref(branch.name, "HEAD"))
 
132
            for tag_name, revid in branch.tags.get_tag_dict().iteritems():
 
133
                if repo.has_revision(revid):
 
134
                    keys.add(tag_name_to_ref(tag_name))
 
135
        return keys
 
136
 
 
137
    def __delitem__(self, ref):
 
138
        try:
 
139
            branch_name = ref_to_branch_name(ref)
 
140
        except ValueError:
 
141
            return # FIXME: Cope with tags!
 
142
        self.dir.destroy_branch(branch_name)
 
143
 
 
144
    def __setitem__(self, ref, sha):
 
145
        try:
 
146
            branch_name = ref_to_branch_name(ref)
 
147
        except ValueError:
 
148
            # FIXME: Cope with tags!
 
149
            return
 
150
        try:
 
151
            target_branch = self.repo_dir.open_branch(branch_name)
 
152
        except errors.NotBranchError:
 
153
            target_branch = self.repo.create_branch(branch_name)
 
154
 
 
155
        rev_id = self.mapping.revision_id_foreign_to_bzr(sha)
 
156
        target_branch.lock_write()
 
157
        try:
 
158
            target_branch.generate_revision_history(rev_id)
 
159
        finally:
 
160
            target_branch.unlock()