bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
0.200.872
by Jelmer Vernooij
Move refs code to separate module. |
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 |
||
|
0.200.873
by Jelmer Vernooij
Add convenience command for accessing virtual git refs. |
19 |
from dulwich.repo import ( |
20 |
RefsContainer, |
|
21 |
)
|
|
22 |
||
23 |
from bzrlib import ( |
|
24 |
errors, |
|
|
0.200.1370
by Jelmer Vernooij
Cope with utf8 tags. |
25 |
osutils, |
|
0.200.873
by Jelmer Vernooij
Add convenience command for accessing virtual git refs. |
26 |
)
|
27 |
||
|
0.200.1061
by Jelmer Vernooij
Add support for using unpeel map. |
28 |
is_tag = lambda x: x.startswith("refs/tags/") |
|
0.200.1480
by Jelmer Vernooij
Don't request unpeeled objects, newer versions of github refuse them. |
29 |
is_head = lambda x: x.startswith("refs/heads/") |
30 |
is_peeled = lambda x: x.endswith("^{}") |
|
|
0.200.1061
by Jelmer Vernooij
Add support for using unpeel map. |
31 |
|
|
0.200.873
by Jelmer Vernooij
Add convenience command for accessing virtual git refs. |
32 |
|
|
0.200.1457
by Jelmer Vernooij
Factor out gather_peeled method. |
33 |
def gather_peeled(refs): |
|
0.200.872
by Jelmer Vernooij
Move refs code to separate module. |
34 |
ret = {} |
|
0.200.1060
by Jelmer Vernooij
Return unpeeled tags in extract_tags. |
35 |
for k, v in refs.iteritems(): |
|
0.200.1480
by Jelmer Vernooij
Don't request unpeeled objects, newer versions of github refuse them. |
36 |
if is_peeled(k): |
37 |
continue
|
|
38 |
try: |
|
39 |
peeled = refs[k+"^{}"] |
|
40 |
unpeeled = v |
|
41 |
except KeyError: |
|
42 |
peeled = v |
|
43 |
unpeeled = None |
|
44 |
ret[k] = (peeled, unpeeled) |
|
|
0.200.1457
by Jelmer Vernooij
Factor out gather_peeled method. |
45 |
return ret |
46 |
||
47 |
||
48 |
def extract_tags(refs): |
|
49 |
"""Extract the tags from a refs dictionary. |
|
50 |
||
51 |
:param refs: Refs to extract the tags from.
|
|
52 |
:return: Dictionary mapping tag names to SHA1s of the actual object
|
|
53 |
and unpeeled object SHA1s.
|
|
54 |
"""
|
|
55 |
ret = {} |
|
56 |
for k, v in gather_peeled(refs).iteritems(): |
|
57 |
try: |
|
58 |
tagname = ref_to_tag_name(k) |
|
59 |
except (ValueError, UnicodeDecodeError): |
|
60 |
pass
|
|
61 |
else: |
|
62 |
ret[tagname] = v |
|
|
0.200.872
by Jelmer Vernooij
Move refs code to separate module. |
63 |
return ret |
64 |
||
65 |
||
66 |
def branch_name_to_ref(name, default=None): |
|
67 |
"""Map a branch name to a ref. |
|
68 |
||
69 |
:param name: Branch name
|
|
70 |
:return: ref string
|
|
71 |
"""
|
|
72 |
if name is None: |
|
73 |
return default |
|
74 |
if name == "HEAD": |
|
|
0.200.1370
by Jelmer Vernooij
Cope with utf8 tags. |
75 |
return osutils.safe_utf8(name) |
|
0.200.872
by Jelmer Vernooij
Move refs code to separate module. |
76 |
if not name.startswith("refs/"): |
|
0.200.1370
by Jelmer Vernooij
Cope with utf8 tags. |
77 |
return "refs/heads/%s" % osutils.safe_utf8(name) |
|
0.200.872
by Jelmer Vernooij
Move refs code to separate module. |
78 |
else: |
|
0.200.1370
by Jelmer Vernooij
Cope with utf8 tags. |
79 |
return osutils.safe_utf8(name) |
|
0.200.872
by Jelmer Vernooij
Move refs code to separate module. |
80 |
|
81 |
||
|
0.200.874
by Jelmer Vernooij
Support tag refs. |
82 |
def tag_name_to_ref(name): |
83 |
"""Map a tag name to a ref. |
|
84 |
||
85 |
:param name: Tag name
|
|
86 |
:return: ref string
|
|
87 |
"""
|
|
|
0.200.1370
by Jelmer Vernooij
Cope with utf8 tags. |
88 |
return "refs/tags/%s" % osutils.safe_utf8(name) |
|
0.200.874
by Jelmer Vernooij
Support tag refs. |
89 |
|
90 |
||
|
0.200.872
by Jelmer Vernooij
Move refs code to separate module. |
91 |
def ref_to_branch_name(ref): |
92 |
"""Map a ref to a branch name |
|
93 |
||
94 |
:param ref: Ref
|
|
95 |
:return: A branch name
|
|
96 |
"""
|
|
|
0.200.916
by Jelmer Vernooij
Set refs/heads/master if no ref is set yet. |
97 |
if ref in (None, "HEAD"): |
|
0.200.915
by Jelmer Vernooij
Cope with the fact that the old format didn't export file ids. |
98 |
return ref |
|
0.200.872
by Jelmer Vernooij
Move refs code to separate module. |
99 |
if ref.startswith("refs/heads/"): |
|
0.200.1370
by Jelmer Vernooij
Cope with utf8 tags. |
100 |
return osutils.safe_unicode(ref[len("refs/heads/"):]) |
|
0.200.874
by Jelmer Vernooij
Support tag refs. |
101 |
raise ValueError("unable to map ref %s back to branch name" % ref) |
102 |
||
103 |
||
104 |
def ref_to_tag_name(ref): |
|
105 |
if ref.startswith("refs/tags/"): |
|
|
0.200.1008
by Jelmer Vernooij
Cope with utf8 invalid data. |
106 |
return ref[len('refs/tags/'):].decode("utf-8") |
|
0.200.1051
by Jelmer Vernooij
Fix typo. |
107 |
raise ValueError("unable to map ref %s back to tag name" % ref) |
|
0.200.872
by Jelmer Vernooij
Move refs code to separate module. |
108 |
|
|
0.200.873
by Jelmer Vernooij
Add convenience command for accessing virtual git refs. |
109 |
|
110 |
class BazaarRefsContainer(RefsContainer): |
|
111 |
||
112 |
def __init__(self, dir, object_store): |
|
113 |
self.dir = dir |
|
114 |
self.object_store = object_store |
|
115 |
||
116 |
def set_symbolic_ref(self, name, other): |
|
117 |
if name == "HEAD": |
|
118 |
pass # FIXME: Switch default branch |
|
119 |
else: |
|
120 |
raise NotImplementedError( |
|
121 |
"Symbolic references not supported for anything other than "
|
|
122 |
"HEAD") |
|
123 |
||
|
0.200.874
by Jelmer Vernooij
Support tag refs. |
124 |
def _get_revid_by_tag_name(self, tag_name): |
125 |
for branch in self.dir.list_branches(): |
|
126 |
try: |
|
127 |
# FIXME: This is ambiguous!
|
|
128 |
return branch.tags.lookup_tag(tag_name) |
|
129 |
except errors.NoSuchTag: |
|
130 |
pass
|
|
131 |
return None |
|
132 |
||
133 |
def _get_revid_by_branch_name(self, branch_name): |
|
|
0.200.873
by Jelmer Vernooij
Add convenience command for accessing virtual git refs. |
134 |
try: |
135 |
branch = self.dir.open_branch(branch_name) |
|
136 |
except errors.NoColocatedBranchSupport: |
|
|
0.252.47
by Jelmer Vernooij
Fix handling of HEAD refs. |
137 |
if branch_name in ("HEAD", "master"): |
|
0.252.45
by Jelmer Vernooij
Finish fetching roundtripped revisions back into bzr. |
138 |
branch = self.dir.open_branch() |
139 |
else: |
|
140 |
raise
|
|
|
0.200.874
by Jelmer Vernooij
Support tag refs. |
141 |
return branch.last_revision() |
142 |
||
143 |
def read_loose_ref(self, ref): |
|
144 |
try: |
|
145 |
branch_name = ref_to_branch_name(ref) |
|
146 |
except ValueError: |
|
147 |
tag_name = ref_to_tag_name(ref) |
|
148 |
revid = self._get_revid_by_tag_name(tag_name) |
|
149 |
else: |
|
150 |
revid = self._get_revid_by_branch_name(branch_name) |
|
151 |
return self.object_store._lookup_revision_sha1(revid) |
|
|
0.200.873
by Jelmer Vernooij
Add convenience command for accessing virtual git refs. |
152 |
|
153 |
def allkeys(self): |
|
154 |
keys = set() |
|
155 |
for branch in self.dir.list_branches(): |
|
|
0.200.876
by Jelmer Vernooij
Filter out objects that aren't actually present locally. |
156 |
repo = branch.repository |
157 |
if repo.has_revision(branch.last_revision()): |
|
|
0.269.7
by Jelmer Vernooij
Cope with branch.name not being available. |
158 |
ref = branch_name_to_ref(getattr(branch, "name", None), |
159 |
"refs/heads/master") |
|
|
0.252.47
by Jelmer Vernooij
Fix handling of HEAD refs. |
160 |
keys.add(ref) |
|
0.269.7
by Jelmer Vernooij
Cope with branch.name not being available. |
161 |
if getattr(branch, "name", None) is None: |
|
0.252.47
by Jelmer Vernooij
Fix handling of HEAD refs. |
162 |
keys.add("HEAD") |
|
0.200.1439
by Jelmer Vernooij
Support accessing bzr branches without tag support. |
163 |
try: |
164 |
for tag_name, revid in branch.tags.get_tag_dict().iteritems(): |
|
165 |
if repo.has_revision(revid): |
|
166 |
keys.add(tag_name_to_ref(tag_name)) |
|
167 |
except errors.TagsNotSupported: |
|
168 |
pass
|
|
|
0.200.873
by Jelmer Vernooij
Add convenience command for accessing virtual git refs. |
169 |
return keys |
170 |
||
171 |
def __delitem__(self, ref): |
|
172 |
try: |
|
173 |
branch_name = ref_to_branch_name(ref) |
|
174 |
except ValueError: |
|
175 |
return # FIXME: Cope with tags! |
|
176 |
self.dir.destroy_branch(branch_name) |
|
177 |
||
178 |
def __setitem__(self, ref, sha): |
|
179 |
try: |
|
180 |
branch_name = ref_to_branch_name(ref) |
|
181 |
except ValueError: |
|
182 |
# FIXME: Cope with tags!
|
|
183 |
return
|
|
184 |
try: |
|
185 |
target_branch = self.repo_dir.open_branch(branch_name) |
|
186 |
except errors.NotBranchError: |
|
187 |
target_branch = self.repo.create_branch(branch_name) |
|
188 |
||
189 |
rev_id = self.mapping.revision_id_foreign_to_bzr(sha) |
|
190 |
target_branch.lock_write() |
|
191 |
try: |
|
192 |
target_branch.generate_revision_history(rev_id) |
|
193 |
finally: |
|
194 |
target_branch.unlock() |
|
|
0.200.1433
by Jelmer Vernooij
Fix fetching between git repositories. |
195 |
|
196 |
||
197 |
def get_refs_container(controldir, object_store): |
|
198 |
repo = controldir.find_repository() |
|
199 |
git_repo = getattr(repo, "_git", None) |
|
200 |
if git_repo is not None: |
|
201 |
return git_repo.refs |
|
202 |
return BazaarRefsContainer(controldir, object_store) |
|
203 |
||
204 |
||
|
0.200.1434
by Jelmer Vernooij
Move refs access to control dir. |
205 |
def get_refs(controldir, object_store=None): |
206 |
cb = getattr(controldir, "get_refs", None) |
|
|
0.200.1433
by Jelmer Vernooij
Fix fetching between git repositories. |
207 |
if cb is not None: |
208 |
return cb() |
|
|
0.200.1434
by Jelmer Vernooij
Move refs access to control dir. |
209 |
return BazaarRefsContainer(controldir, object_store).as_dict() |