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, |
|
25 |
)
|
|
26 |
||
27 |
||
0.200.872
by Jelmer Vernooij
Move refs code to separate module. |
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) |
|
0.200.1008
by Jelmer Vernooij
Cope with utf8 invalid data. |
38 |
try: |
39 |
tagname = ref_to_tag_name(k) |
|
40 |
except UnicodeDecodeError: |
|
41 |
pass
|
|
42 |
else: |
|
43 |
ret[tagname] = v |
|
0.200.872
by Jelmer Vernooij
Move refs code to separate module. |
44 |
return ret |
45 |
||
46 |
||
47 |
def branch_name_to_ref(name, default=None): |
|
48 |
"""Map a branch name to a ref. |
|
49 |
||
50 |
:param name: Branch name
|
|
51 |
:return: ref string
|
|
52 |
"""
|
|
53 |
if name is None: |
|
54 |
return default |
|
55 |
if name == "HEAD": |
|
0.200.915
by Jelmer Vernooij
Cope with the fact that the old format didn't export file ids. |
56 |
return name |
0.200.872
by Jelmer Vernooij
Move refs code to separate module. |
57 |
if not name.startswith("refs/"): |
58 |
return "refs/heads/%s" % name |
|
59 |
else: |
|
60 |
return name |
|
61 |
||
62 |
||
0.200.874
by Jelmer Vernooij
Support tag refs. |
63 |
def tag_name_to_ref(name): |
64 |
"""Map a tag name to a ref. |
|
65 |
||
66 |
:param name: Tag name
|
|
67 |
:return: ref string
|
|
68 |
"""
|
|
69 |
return "refs/tags/%s" % name |
|
70 |
||
71 |
||
0.200.872
by Jelmer Vernooij
Move refs code to separate module. |
72 |
def ref_to_branch_name(ref): |
73 |
"""Map a ref to a branch name |
|
74 |
||
75 |
:param ref: Ref
|
|
76 |
:return: A branch name
|
|
77 |
"""
|
|
0.200.916
by Jelmer Vernooij
Set refs/heads/master if no ref is set yet. |
78 |
if ref in (None, "HEAD"): |
0.200.915
by Jelmer Vernooij
Cope with the fact that the old format didn't export file ids. |
79 |
return ref |
0.200.872
by Jelmer Vernooij
Move refs code to separate module. |
80 |
if ref.startswith("refs/heads/"): |
81 |
return ref[len("refs/heads/"):] |
|
0.200.874
by Jelmer Vernooij
Support tag refs. |
82 |
raise ValueError("unable to map ref %s back to branch name" % ref) |
83 |
||
84 |
||
85 |
def ref_to_tag_name(ref): |
|
86 |
if ref.startswith("refs/tags/"): |
|
0.200.1008
by Jelmer Vernooij
Cope with utf8 invalid data. |
87 |
return ref[len('refs/tags/'):].decode("utf-8") |
0.200.874
by Jelmer Vernooij
Support tag refs. |
88 |
raise ValueError("unable to map ref %s back to branch name" % ref) |
0.200.872
by Jelmer Vernooij
Move refs code to separate module. |
89 |
|
0.200.873
by Jelmer Vernooij
Add convenience command for accessing virtual git refs. |
90 |
|
91 |
class BazaarRefsContainer(RefsContainer): |
|
92 |
||
93 |
def __init__(self, dir, object_store): |
|
94 |
self.dir = dir |
|
95 |
self.object_store = object_store |
|
96 |
||
97 |
def set_symbolic_ref(self, name, other): |
|
98 |
if name == "HEAD": |
|
99 |
pass # FIXME: Switch default branch |
|
100 |
else: |
|
101 |
raise NotImplementedError( |
|
102 |
"Symbolic references not supported for anything other than "
|
|
103 |
"HEAD") |
|
104 |
||
0.200.874
by Jelmer Vernooij
Support tag refs. |
105 |
def _get_revid_by_tag_name(self, tag_name): |
106 |
for branch in self.dir.list_branches(): |
|
107 |
try: |
|
108 |
# FIXME: This is ambiguous!
|
|
109 |
return branch.tags.lookup_tag(tag_name) |
|
110 |
except errors.NoSuchTag: |
|
111 |
pass
|
|
112 |
return None |
|
113 |
||
114 |
def _get_revid_by_branch_name(self, branch_name): |
|
0.200.873
by Jelmer Vernooij
Add convenience command for accessing virtual git refs. |
115 |
try: |
116 |
branch = self.dir.open_branch(branch_name) |
|
117 |
except errors.NoColocatedBranchSupport: |
|
0.252.47
by Jelmer Vernooij
Fix handling of HEAD refs. |
118 |
if branch_name in ("HEAD", "master"): |
0.252.45
by Jelmer Vernooij
Finish fetching roundtripped revisions back into bzr. |
119 |
branch = self.dir.open_branch() |
120 |
else: |
|
121 |
raise
|
|
0.200.874
by Jelmer Vernooij
Support tag refs. |
122 |
return branch.last_revision() |
123 |
||
124 |
def read_loose_ref(self, ref): |
|
125 |
try: |
|
126 |
branch_name = ref_to_branch_name(ref) |
|
127 |
except ValueError: |
|
128 |
tag_name = ref_to_tag_name(ref) |
|
129 |
revid = self._get_revid_by_tag_name(tag_name) |
|
130 |
else: |
|
131 |
revid = self._get_revid_by_branch_name(branch_name) |
|
132 |
return self.object_store._lookup_revision_sha1(revid) |
|
0.200.873
by Jelmer Vernooij
Add convenience command for accessing virtual git refs. |
133 |
|
134 |
def allkeys(self): |
|
135 |
keys = set() |
|
136 |
for branch in self.dir.list_branches(): |
|
0.200.876
by Jelmer Vernooij
Filter out objects that aren't actually present locally. |
137 |
repo = branch.repository |
138 |
if repo.has_revision(branch.last_revision()): |
|
0.252.47
by Jelmer Vernooij
Fix handling of HEAD refs. |
139 |
ref = branch_name_to_ref(branch.name, "refs/heads/master") |
140 |
keys.add(ref) |
|
141 |
if branch.name is None: |
|
142 |
keys.add("HEAD") |
|
0.200.876
by Jelmer Vernooij
Filter out objects that aren't actually present locally. |
143 |
for tag_name, revid in branch.tags.get_tag_dict().iteritems(): |
144 |
if repo.has_revision(revid): |
|
145 |
keys.add(tag_name_to_ref(tag_name)) |
|
0.200.873
by Jelmer Vernooij
Add convenience command for accessing virtual git refs. |
146 |
return keys |
147 |
||
148 |
def __delitem__(self, ref): |
|
149 |
try: |
|
150 |
branch_name = ref_to_branch_name(ref) |
|
151 |
except ValueError: |
|
152 |
return # FIXME: Cope with tags! |
|
153 |
self.dir.destroy_branch(branch_name) |
|
154 |
||
155 |
def __setitem__(self, ref, sha): |
|
156 |
try: |
|
157 |
branch_name = ref_to_branch_name(ref) |
|
158 |
except ValueError: |
|
159 |
# FIXME: Cope with tags!
|
|
160 |
return
|
|
161 |
try: |
|
162 |
target_branch = self.repo_dir.open_branch(branch_name) |
|
163 |
except errors.NotBranchError: |
|
164 |
target_branch = self.repo.create_branch(branch_name) |
|
165 |
||
166 |
rev_id = self.mapping.revision_id_foreign_to_bzr(sha) |
|
167 |
target_branch.lock_write() |
|
168 |
try: |
|
169 |
target_branch.generate_revision_history(rev_id) |
|
170 |
finally: |
|
171 |
target_branch.unlock() |