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
345
346
347
348
349
350
351
352
353
354
355
356
357
|
# Copyright (C) 2005-2006 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 3 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
"""Access to stored Subversion basis trees."""
from bzrlib import urlutils
from bzrlib.branch import Branch
from bzrlib.inventory import Inventory, InventoryDirectory, TreeReference
from bzrlib import osutils, urlutils
from bzrlib.trace import mutter
from bzrlib.revisiontree import RevisionTree
import os
import md5
from cStringIO import StringIO
import urllib
import svn.core, svn.wc, svn.delta
from svn.core import Pool
import errors
def parse_externals_description(base_url, val):
"""Parse an svn:externals property value.
:param base_url: URL on which the property is set. Used for
relative externals.
:returns: dictionary with local names as keys, (revnum, url)
as value. revnum is the revision number and is
set to None if not applicable.
"""
ret = {}
for l in val.splitlines():
if l == "" or l[0] == "#":
continue
pts = l.rsplit(None, 2)
if len(pts) == 3:
if not pts[1].startswith("-r"):
raise errors.InvalidExternalsDescription()
ret[pts[0]] = (int(pts[1][2:]), urlutils.join(base_url, pts[2]))
elif len(pts) == 2:
if pts[1].startswith("//"):
raise NotImplementedError("Relative to the scheme externals not yet supported")
if pts[1].startswith("^/"):
raise NotImplementedError("Relative to the repository root externals not yet supported")
ret[pts[0]] = (None, urlutils.join(base_url, pts[1]))
else:
raise errors.InvalidExternalsDescription()
return ret
def inventory_add_external(inv, parent_id, path, revid, ref_revnum, url):
"""Add an svn:externals entry to an inventory as a tree-reference.
:param inv: Inventory to add to.
:param parent_id: File id of directory the entry was set on.
:param path: Path of the entry, relative to entry with parent_id.
:param revid: Revision to store in newly created inventory entries.
:param ref_revnum: Referenced revision of tree that's being referenced, or
None if no specific revision is being referenced.
:param url: URL of referenced tree.
"""
assert ref_revnum is None or isinstance(ref_revnum, int)
assert revid is None or isinstance(revid, str)
(dir, name) = os.path.split(path)
parent = inv[parent_id]
if dir != "":
for part in dir.split("/"):
if parent.children.has_key(part):
parent = parent.children[part]
else:
# Implicitly add directory if it doesn't exist yet
# TODO: Generate a file id
parent = inv.add(InventoryDirectory('someid', part,
parent_id=parent.file_id))
parent.revision = revid
reference_branch = Branch.open(url)
file_id = reference_branch.get_root_id()
ie = TreeReference(file_id, name, parent.file_id, revision=revid)
if ref_revnum is not None:
ie.reference_revision = reference_branch.get_rev_id(ref_revnum)
inv.add(ie)
# Deal with Subversion 1.5 and the patched Subversion 1.4 (which are
# slightly different).
if hasattr(svn.delta, 'tx_invoke_window_handler'):
def apply_txdelta_handler(src_stream, target_stream, pool):
assert hasattr(src_stream, 'read')
assert hasattr(target_stream, 'write')
window_handler, baton = svn.delta.tx_apply(src_stream, target_stream,
None, pool)
def wrapper(window):
window_handler(window, baton)
return wrapper
else:
def apply_txdelta_handler(src_stream, target_stream, pool):
assert hasattr(src_stream, 'read')
assert hasattr(target_stream, 'write')
ret = svn.delta.svn_txdelta_apply(src_stream, target_stream, None, pool)
def wrapper(window):
svn.delta.invoke_txdelta_window_handler(
ret[1], window, ret[2])
return wrapper
class SvnRevisionTree(RevisionTree):
"""A tree that existed in a historical Subversion revision."""
def __init__(self, repository, revision_id):
self._repository = repository
self._revision_id = revision_id
pool = Pool()
(self.branch_path, self.revnum, mapping) = repository.lookup_revision_id(revision_id)
self._inventory = Inventory()
self.id_map = repository.get_fileid_map(self.revnum, self.branch_path,
mapping)
editor = TreeBuildEditor(self, pool)
self.file_data = {}
root_repos = repository.transport.get_svn_repos_root()
reporter = repository.transport.do_switch(
self.revnum, True,
urlutils.join(root_repos, self.branch_path), editor, pool)
reporter.set_path("", 0, True, None, pool)
reporter.finish_report(pool)
pool.destroy()
def get_file_lines(self, file_id):
return osutils.split_lines(self.file_data[file_id])
class TreeBuildEditor(svn.delta.Editor):
"""Builds a tree given Subversion tree transform calls."""
def __init__(self, tree, pool):
self.tree = tree
self.repository = tree._repository
self.last_revnum = {}
self.dir_revnum = {}
self.dir_ignores = {}
self.pool = pool
def set_target_revision(self, revnum):
self.revnum = revnum
def open_root(self, revnum, baton):
file_id, revision_id = self.tree.id_map[""]
ie = self.tree._inventory.add_path("", 'directory', file_id)
ie.revision = revision_id
self.tree._inventory.revision_id = revision_id
return file_id
def add_directory(self, path, parent_baton, copyfrom_path, copyfrom_revnum, pool):
path = path.decode("utf-8")
file_id, revision_id = self.tree.id_map[path]
ie = self.tree._inventory.add_path(path, 'directory', file_id)
ie.revision = revision_id
return file_id
def change_dir_prop(self, id, name, value, pool):
if name == svn.core.SVN_PROP_ENTRY_COMMITTED_REV:
self.dir_revnum[id] = int(value)
elif name == svn.core.SVN_PROP_IGNORE:
self.dir_ignores[id] = value
elif name in (svn.core.SVN_PROP_ENTRY_COMMITTED_DATE,
svn.core.SVN_PROP_ENTRY_LAST_AUTHOR,
svn.core.SVN_PROP_ENTRY_LOCK_TOKEN,
svn.core.SVN_PROP_ENTRY_UUID,
svn.core.SVN_PROP_EXECUTABLE):
pass
elif name.startswith(svn.core.SVN_PROP_WC_PREFIX):
pass
elif name.startswith(svn.core.SVN_PROP_PREFIX):
mutter('unsupported dir property %r' % name)
def change_file_prop(self, id, name, value, pool):
if name == svn.core.SVN_PROP_EXECUTABLE:
self.is_executable = (value != None)
elif name == svn.core.SVN_PROP_SPECIAL:
self.is_symlink = (value != None)
elif name == svn.core.SVN_PROP_EXTERNALS:
mutter('%r property on file!' % name)
elif name == svn.core.SVN_PROP_ENTRY_COMMITTED_REV:
self.last_file_rev = int(value)
elif name in (svn.core.SVN_PROP_ENTRY_COMMITTED_DATE,
svn.core.SVN_PROP_ENTRY_LAST_AUTHOR,
svn.core.SVN_PROP_ENTRY_LOCK_TOKEN,
svn.core.SVN_PROP_ENTRY_UUID,
svn.core.SVN_PROP_MIME_TYPE):
pass
elif name.startswith(svn.core.SVN_PROP_WC_PREFIX):
pass
elif name.startswith(svn.core.SVN_PROP_PREFIX):
mutter('unsupported file property %r' % name)
def add_file(self, path, parent_id, copyfrom_path, copyfrom_revnum, baton):
path = path.decode("utf-8")
self.is_symlink = False
self.is_executable = False
return path
def close_dir(self, id):
if id in self.tree._inventory and self.dir_ignores.has_key(id):
self.tree._inventory[id].ignores = self.dir_ignores[id]
def close_file(self, path, checksum):
file_id, revision_id = self.tree.id_map[path]
if self.is_symlink:
ie = self.tree._inventory.add_path(path, 'symlink', file_id)
else:
ie = self.tree._inventory.add_path(path, 'file', file_id)
ie.revision = revision_id
if self.file_stream:
self.file_stream.seek(0)
file_data = self.file_stream.read()
else:
file_data = ""
actual_checksum = md5.new(file_data).hexdigest()
assert(checksum is None or checksum == actual_checksum,
"checksum mismatch: %r != %r" % (checksum, actual_checksum))
if self.is_symlink:
ie.symlink_target = file_data[len("link "):]
ie.text_sha1 = None
ie.text_size = None
ie.text_id = None
ie.executable = False
else:
ie.text_sha1 = osutils.sha_string(file_data)
ie.text_size = len(file_data)
self.tree.file_data[file_id] = file_data
ie.executable = self.is_executable
self.file_stream = None
def close_edit(self):
pass
def abort_edit(self):
pass
def apply_textdelta(self, file_id, base_checksum):
self.file_stream = StringIO()
return apply_txdelta_handler(StringIO(""), self.file_stream, self.pool)
class SvnBasisTree(RevisionTree):
"""Optimized version of SvnRevisionTree."""
def __init__(self, workingtree):
self.workingtree = workingtree
self._revision_id = workingtree.branch.generate_revision_id(
workingtree.base_revnum)
self.id_map = workingtree.branch.repository.get_fileid_map(
workingtree.base_revnum,
workingtree.branch.get_branch_path(workingtree.base_revnum),
workingtree.branch.mapping)
self._inventory = Inventory(root_id=None)
self._repository = workingtree.branch.repository
def add_file_to_inv(relpath, id, revid, wc):
props = svn.wc.get_prop_diffs(self.workingtree.abspath(relpath).encode("utf-8"), wc)
if isinstance(props, list): # Subversion 1.5
props = props[1]
if props.has_key(svn.core.SVN_PROP_SPECIAL):
ie = self._inventory.add_path(relpath, 'symlink', id)
ie.symlink_target = open(self._abspath(relpath)).read()[len("link "):]
ie.text_sha1 = None
ie.text_size = None
ie.text_id = None
ie.executable = False
else:
ie = self._inventory.add_path(relpath, 'file', id)
data = osutils.fingerprint_file(open(self._abspath(relpath)))
ie.text_sha1 = data['sha1']
ie.text_size = data['size']
ie.executable = props.has_key(svn.core.SVN_PROP_EXECUTABLE)
ie.revision = revid
return ie
def find_ids(entry):
relpath = urllib.unquote(entry.url[len(entry.repos):].strip("/"))
if entry.schedule in (svn.wc.schedule_normal,
svn.wc.schedule_delete,
svn.wc.schedule_replace):
return self.id_map[workingtree.branch.unprefix(relpath.decode("utf-8"))]
return (None, None)
def add_dir_to_inv(relpath, wc, parent_id):
entries = svn.wc.entries_read(wc, False)
entry = entries[""]
(id, revid) = find_ids(entry)
if id == None:
return
# First handle directory itself
ie = self._inventory.add_path(relpath, 'directory', id)
ie.revision = revid
if relpath == u"":
self._inventory.revision_id = revid
for name, entry in entries.items():
name = name.decode("utf-8")
if name == u"":
continue
assert isinstance(relpath, unicode)
assert isinstance(name, unicode)
subrelpath = os.path.join(relpath, name)
assert entry
if entry.kind == svn.core.svn_node_dir:
subwc = svn.wc.adm_open3(wc,
self.workingtree.abspath(subrelpath),
False, 0, None)
try:
add_dir_to_inv(subrelpath, subwc, id)
finally:
svn.wc.adm_close(subwc)
else:
(subid, subrevid) = find_ids(entry)
if subid is not None:
add_file_to_inv(subrelpath, subid, subrevid, wc)
wc = workingtree._get_wc()
try:
add_dir_to_inv(u"", wc, None)
finally:
svn.wc.adm_close(wc)
def _abspath(self, relpath):
return svn.wc.get_pristine_copy_path(self.workingtree.abspath(relpath).encode("utf-8"))
def get_file_lines(self, file_id):
base_copy = self._abspath(self.id2path(file_id))
return osutils.split_lines(open(base_copy).read())
|