-
Notifications
You must be signed in to change notification settings - Fork 1
/
revids.py
344 lines (295 loc) · 14.6 KB
/
revids.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# Copyright (C) 2006-2009 Jelmer Vernooij <jelmer@samba.org>
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""Revision id generation and caching."""
from __future__ import absolute_import
import subvertpy
from breezy import (
ui,
)
from breezy.errors import (
NoSuchRevision,
)
from six import (
text_type,
)
from breezy.lru_cache import LRUCache
from .errors import (
InvalidBzrSvnRevision,
InvalidPropertyValue,
warn_uuid_reuse,
)
from .mapping import (
SVN_PROP_BZR_REVISION_ID,
find_mapping_revprops,
find_new_lines,
is_bzr_revision_revprops,
mapping_registry,
parse_revid_property,
)
class RevidMap(object):
def __init__(self, repos):
self.repos = repos
def get_branch_revnum(self, revid, layout, project=None):
"""Find the (branch, revnum) tuple for a revision id.
:return: Tuple with foreign revision id and mapping.
"""
last_revnum = self.repos.get_latest_revnum()
fileprops_to_revnum = last_revnum
with ui.ui_factory.nested_progress_bar() as pb:
for entry_revid, branch, revnum, mapping in self.discover_revprop_revids(
last_revnum, 0, pb=pb):
if revid == entry_revid:
return (self.repos.uuid, branch, revnum), mapping
fileprops_to_revnum = min(fileprops_to_revnum, revnum)
for entry_revid, branch, min_revno, max_revno, mapping in self.discover_fileprop_revids(layout, fileprops_to_revnum, 0, project, pb=pb):
if revid == entry_revid:
(foreign_revid, mapping_name) = self.bisect_fileprop_revid_revnum(revid, branch, min_revno, max_revno)
return (foreign_revid, mapping_name)
raise NoSuchRevision(self, revid)
def discover_revprop_revids(self, from_revnum, to_revnum, pb=None):
"""Discover bzr-svn revision properties between from_revnum and to_revnum.
:return: First revision number on which a revision property was found, or None
"""
if self.repos.svn_transport.has_capability("log-revprops") != True:
return
for (paths, revnum, revprops) in self.repos._log.iter_changes(None, from_revnum, to_revnum):
if pb is not None:
pb.update("discovering revprop revisions", from_revnum-revnum, from_revnum-to_revnum)
if is_bzr_revision_revprops(revprops):
mapping = find_mapping_revprops(revprops)
assert mapping is not None
branch_path = mapping.get_branch_root(revprops)
if branch_path is None:
continue
revno, revid, hidden = mapping.get_revision_id_revprops(revprops)
if revid is not None:
assert isinstance(branch_path, text_type)
yield (revid, branch_path.strip("/"), revnum, mapping)
def find_branch_tips(self, layout, from_revnum, to_revnum, project=None):
assert from_revnum >= to_revnum
# TODO: Use RevisionMetadataBrowser to find the tip revmetas all at once
reuse_policy = self.repos.get_config().get_reuse_revisions()
assert reuse_policy in ("other-branches", "removed-branches", "none")
check_removed = (reuse_policy == "removed-branches")
for (branch, revno, exists) in self.repos.find_fileprop_paths(
layout, from_revnum, to_revnum, project,
check_removed=check_removed):
if not isinstance(branch, text_type):
raise TypeError(branch)
assert type(revno) is int
iterator = self.repos._revmeta_provider.iter_reverse_branch_changes(
branch, revno, to_revnum=0, limit=0)
yield next(iterator)
def discover_fileprop_revids(self, layout, from_revnum, to_revnum,
project=None, pb=None):
assert from_revnum >= to_revnum
for revmeta in self.find_branch_tips(layout, from_revnum, to_revnum,
project):
if pb is not None:
pb.update("finding fileprop revids",
from_revnum-revmeta.metarev.revnum, from_revnum-to_revnum)
# Look at their bzr:revision-id-vX
revids = set()
try:
if revmeta.consider_bzr_fileprops():
for revid, bzr_revno, mapping_name in revmeta.get_roundtrip_ancestor_revids():
revids.add(((bzr_revno, revid), mapping_name))
except subvertpy.SubversionException as e:
msg, num = e.args
if num in (subvertpy.ERR_FS_NOT_DIRECTORY,
subvertpy.ERR_RA_DAV_FORBIDDEN):
continue
raise
# If there are any new entries that are not yet in the cache,
# add them
for ((entry_revno, entry_revid), mapping_name) in revids:
try:
mapping = mapping_registry.parse_mapping_name("svn-" + mapping_name)
except KeyError:
pass
else:
yield (entry_revid, revmeta.metarev.branch_path, 0,
revmeta.metarev.revnum, mapping)
def bisect_fileprop_revid_revnum(self, revid, branch_path, min_revnum, max_revnum):
"""Find out what the actual revnum was that corresponds to a revid.
:param revid: Revision id to search for
:param branch_path: Branch path at which to start searching
:param min_revnum: Last revnum to check
:param max_revnum: First revnum to check
:return: Tuple with foreign revision id and mapping
"""
assert min_revnum <= max_revnum
# Find the branch property between min_revnum and max_revnum that
# added revid
for revmeta in self.repos._revmeta_provider.iter_reverse_branch_changes(
branch_path, max_revnum, min_revnum):
for propname, (oldpropvalue, propvalue) in revmeta.get_changed_fileprops().items():
if not propname.startswith(SVN_PROP_BZR_REVISION_ID):
continue
try:
new_lines = find_new_lines(oldpropvalue, propvalue)
if len(new_lines) != 1:
continue
except ValueError:
# Don't warn about encountering an invalid property,
# that will already have happened earlier
continue
try:
(entry_revno, entry_revid) = parse_revid_property(
new_lines[0])
except InvalidPropertyValue:
# Don't warn about encountering an invalid property,
# that will already have happened earlier
continue
if entry_revid == revid:
mapping_name = propname[len(SVN_PROP_BZR_REVISION_ID):]
mapping = mapping_registry.parse_mapping_name("svn-" + mapping_name)
assert mapping.is_branch_or_tag(revmeta.metarev.branch_path)
return (revmeta.metarev.get_foreign_revid(), mapping)
raise InvalidBzrSvnRevision(revid)
class MemoryCachingRevidMap(object):
def __init__(self, actual):
self.actual = actual
self._cache = LRUCache()
self._nonexistant_revnum = None
self._nonexistant = set()
def get_branch_revnum(self, revid, layout, project=None):
if revid in self._cache:
return self._cache[revid]
last_revnum = self.actual.repos.get_latest_revnum()
if self._nonexistant_revnum is not None:
if last_revnum <= self._nonexistant_revnum:
if revid in self._nonexistant:
raise NoSuchRevision(self, revid)
try:
ret = self.actual.get_branch_revnum(revid, layout, project)
except NoSuchRevision:
if self._nonexistant_revnum != last_revnum:
self._nonexistant_revnum = last_revnum
self._nonexistant = set()
self._nonexistant.add(revid)
raise
else:
self._cache[revid] = ret
return ret
class DiskCachingRevidMap(object):
def __init__(self, actual, cache):
self.cache = cache
self.actual = actual
self.revid_seen = set()
def remember_entry(self, entry_revid, branch, min_revnum, max_revnum,
mappingname):
if entry_revid not in self.revid_seen:
self.cache.insert_revid(entry_revid, branch, min_revnum, max_revnum,
mappingname)
self.revid_seen.add(entry_revid)
def _get_last_checked(self, layout, project):
return self.cache.last_revnum_checked(repr((layout, project)))
def _get_last_revnum(self):
return self.actual.repos.get_latest_revnum()
def get_branch_revnum(self, revid, layout, project=None):
# Check the record out of the cache, if it exists
try:
(branch_path, min_revnum, max_revnum, \
mapping) = self.cache.lookup_revid(revid)
if min_revnum == max_revnum:
return ((self.actual.repos.uuid, branch_path, min_revnum),
mapping_registry.parse_mapping_name("svn-" + mapping))
except NoSuchRevision as e:
last_revnum = self._get_last_revnum()
last_checked = self._get_last_checked(layout, project)
if last_checked > last_revnum:
warn_uuid_reuse(self.actual.repos.uuid, self.actual.repos.base)
if last_revnum == last_checked:
# All revision ids in this repository for the current
# layout have already been discovered. No need to
# check again.
raise e
found = None
fileprops_to_revnum = last_revnum
with ui.ui_factory.nested_progress_bar() as pb:
for entry_revid, branch, revnum, mapping in self.actual.discover_revprop_revids(
last_revnum, last_checked, pb=pb):
fileprops_to_revnum = min(fileprops_to_revnum, revnum)
if entry_revid == revid:
found = (branch, revnum, revnum, mapping)
self.remember_entry(entry_revid, branch, revnum,
revnum, mapping.name)
if fileprops_to_revnum > last_checked:
with ui.ui_factory.nested_progress_bar() as pb:
for entry_revid, branch, min_revno, max_revno, mapping in self.actual.discover_fileprop_revids(
layout, fileprops_to_revnum, last_checked, project, pb):
min_revno = max(last_checked, min_revno)
if entry_revid == revid:
found = (branch, min_revno, max_revno, mapping)
self.remember_entry(entry_revid, branch, min_revno,
max_revno, mapping.name)
# We've added all the revision ids for this layout in the
# repository, so no need to check again unless new revisions got
# added
self.cache.set_last_revnum_checked(repr((layout, project)), last_revnum)
if found is None:
raise e
(branch_path, min_revnum, max_revnum, mapping) = found
if min_revnum == max_revnum:
return (self.actual.repos.uuid, branch_path, min_revnum), mapping
if min_revnum > max_revnum:
raise AssertionError('%d > %d' % (min_revnum, max_revnum))
((uuid, branch_path, revnum), mapping) = self.actual.bisect_fileprop_revid_revnum(
revid, branch_path, min_revnum, max_revnum)
self.remember_entry(revid, branch_path, revnum, revnum, mapping.name)
return (uuid, branch_path, revnum), mapping
class RevisionIdMapCache(object):
"""Revision id mapping store.
Stores mapping from revid -> (path, revnum, mapping)
"""
def set_last_revnum_checked(self, layout, revnum):
"""Remember the latest revision number that has been checked
for a particular layout.
:param layout: Repository layout.
:param revnum: Revision number.
"""
raise NotImplementedError(self.set_last_revnum_checked)
def last_revnum_checked(self, layout):
"""Retrieve the latest revision number that has been checked
for revision ids for a particular layout.
:param layout: Repository layout.
:return: Last revision number checked or 0.
"""
raise NotImplementedError(self.last_revnum_checked)
def lookup_revid(self, revid):
"""Lookup the details for a particular revision id.
:param revid: Revision id.
:return: Tuple with path inside repository, minimum revision number, maximum revision number and
mapping.
"""
raise NotImplementedError(self.lookup_revid)
def lookup_branch_revnum(self, revnum, path, mapping):
"""Lookup a revision by revision number, branch path and mapping.
:param revnum: Subversion revision number.
:param path: Subversion branch path.
:param mapping: Mapping
"""
raise NotImplementedError(self.lookup_branch_revnum)
def insert_revid(self, revid, branch, min_revnum, max_revnum, mapping):
"""Insert a revision id into the revision id cache.
:param revid: Revision id for which to insert metadata.
:param branch: Branch path at which the revision was seen
:param min_revnum: Minimum Subversion revision number in which the
revid was found
:param max_revnum: Maximum Subversion revision number in which the
revid was found
:param mapping: Name of the mapping with which the revision
was found
"""
raise NotImplementedError(self.insert_revid)