1 # (Be in -*- python -*- mode.)
3 # ====================================================================
4 # Copyright (c) 2000-2009 CollabNet. All rights reserved.
6 # This software is licensed as described in the file COPYING, which
7 # you should have received as part of this distribution. The terms
8 # are also available at http://subversion.tigris.org/license-1.html.
9 # If newer versions of this license are posted there, you may use a
10 # newer version instead, at your option.
12 # This software consists of voluntary contributions made by many
13 # individuals. For exact contribution history, see the revision
14 # history and logs, available at http://cvs2svn.tigris.org/.
15 # ====================================================================
17 """This module contains database facilities used by cvs2svn."""
21 from hashlib
import md5
23 from md5
import new
as md5
26 from cvs2svn_lib
import config
27 from cvs2svn_lib
.common
import FatalError
28 from cvs2svn_lib
.common
import InternalError
29 from cvs2svn_lib
.common
import canonicalize_eol
30 from cvs2svn_lib
.common
import path_split
31 from cvs2svn_lib
.context
import Ctx
32 from cvs2svn_lib
.cvs_path
import CVSDirectory
33 from cvs2svn_lib
.cvs_path
import CVSFile
34 from cvs2svn_lib
.svn_repository_delegate
import SVNRepositoryDelegate
35 from cvs2svn_lib
.apple_single_filter
import get_maybe_apple_single
38 # Things that can happen to a file.
43 class DumpstreamDelegate(SVNRepositoryDelegate
):
44 """Write output in Subversion dumpfile format."""
46 def __init__(self
, revision_reader
, dumpfile
):
47 """Return a new DumpstreamDelegate instance.
49 DUMPFILE should be a file-like object opened in binary mode, to
50 which the dump stream will be written. The only methods called on
51 the object are write() and close()."""
53 self
._revision
_reader
= revision_reader
54 self
._dumpfile
= dumpfile
55 self
._write
_dumpfile
_header
()
57 # A set of the basic project infrastructure project directories
58 # that have been created so far, as SVN paths. (The root
59 # directory is considered to be present at initialization.) This
60 # includes all of the LOD paths, and all of their parent
62 self
._basic
_directories
= set([''])
64 def _write_dumpfile_header(self
):
65 """Initialize the dumpfile with the standard headers.
67 Since the CVS repository doesn't have a UUID, and the Subversion
68 repository will be created with one anyway, we don't specify a
69 UUID in the dumpfile."""
71 self
._dumpfile
.write('SVN-fs-dump-format-version: 2\n\n')
73 def _utf8_path(self
, path
):
74 """Return a copy of PATH encoded in UTF-8."""
76 # Convert each path component separately (as they may each use
77 # different encodings).
80 Ctx().cvs_filename_decoder(piece
).encode('utf8')
81 for piece
in path
.split('/')
85 "Unable to convert a path '%s' to internal encoding.\n"
86 "Consider rerunning with one or more '--encoding' parameters or\n"
87 "with '--fallback-encoding'."
91 def _string_for_props(properties
):
92 """Return PROPERTIES in the form needed for the dumpfile."""
95 for (k
, v
) in sorted(properties
.iteritems()):
97 # Such properties are for internal use only.
100 # None indicates that the property should be left unset.
103 prop_strings
.append('K %d\n%s\nV %d\n%s\n' % (len(k
), k
, len(v
), v
))
105 prop_strings
.append('PROPS-END\n')
107 return ''.join(prop_strings
)
109 def start_commit(self
, revnum
, revprops
):
110 """Emit the start of SVN_COMMIT (an SVNCommit)."""
112 # The start of a new commit typically looks like this:
115 # Prop-content-length: 129
116 # Content-length: 129
121 # Log message for revision 1.
129 # 2003-04-22T22:57:58.132837Z
132 # Notice that the length headers count everything -- not just the
133 # length of the data but also the lengths of the lengths, including
134 # the 'K ' or 'V ' prefixes.
136 # The reason there are both Prop-content-length and Content-length
137 # is that the former includes just props, while the latter includes
138 # everything. That's the generic header form for any entity in a
139 # dumpfile. But since revisions only have props, the two lengths
140 # are always the same for revisions.
142 # Calculate the output needed for the property definitions.
143 all_prop_strings
= self
._string
_for
_props
(revprops
)
144 total_len
= len(all_prop_strings
)
146 # Print the revision header and revprops
147 self
._dumpfile
.write(
148 'Revision-number: %d\n'
149 'Prop-content-length: %d\n'
150 'Content-length: %d\n'
154 % (revnum
, total_len
, total_len
, all_prop_strings
)
157 def end_commit(self
):
160 def _make_any_dir(self
, path
):
161 """Emit the creation of directory PATH."""
163 self
._dumpfile
.write(
169 % self
._utf
8_path
(path
)
172 def _register_basic_directory(self
, path
, create
):
173 """Register the creation of PATH if it is not already there.
175 Create any parent directories that do not already exist. If
176 CREATE is set, also create PATH if it doesn't already exist. This
177 method should only be used for the LOD paths and the directories
178 containing them, not for directories within an LOD path."""
180 if path
not in self
._basic
_directories
:
181 # Make sure that the parent directory is present:
182 self
._register
_basic
_directory
(path_split(path
)[0], True)
184 self
._make
_any
_dir
(path
)
185 self
._basic
_directories
.add(path
)
187 def initialize_project(self
, project
):
188 """Create any initial directories for the project.
190 The trunk, tags, and branches directories directories are created
191 the first time the project is seen. Be sure not to create parent
192 directories that already exist (e.g., because two directories
193 share part of their paths either within or across projects)."""
195 for path
in project
.get_initial_directories():
196 self
._register
_basic
_directory
(path
, True)
198 def initialize_lod(self
, lod
):
199 lod_path
= lod
.get_path()
201 self
._register
_basic
_directory
(lod_path
, True)
203 def mkdir(self
, lod
, cvs_directory
):
204 self
._make
_any
_dir
(lod
.get_path(cvs_directory
.cvs_path
))
206 def _add_or_change_path(self
, cvs_rev
, op
):
207 """Emit the addition or change corresponding to CVS_REV.
209 OP is either the constant OP_ADD or OP_CHANGE."""
211 assert op
in [OP_ADD
, OP_CHANGE
]
213 # The property handling here takes advantage of an undocumented
214 # but IMHO consistent feature of the Subversion dumpfile-loading
215 # code. When a node's properties aren't mentioned (that is, the
216 # "Prop-content-length:" header is absent, no properties are
217 # listed at all, and there is no "PROPS-END\n" line) then no
218 # change is made to the node's properties.
220 # This is consistent with the way dumpfiles behave w.r.t. text
221 # content changes, so I'm comfortable relying on it. If you
222 # commit a change to *just* the properties of some node that
223 # already has text contents from a previous revision, then in the
224 # dumpfile output for the prop change, no "Text-content-length:"
225 # nor "Text-content-md5:" header will be present, and the text of
226 # the file will not be given. But this does not cause the file's
227 # text to be erased! It simply remains unchanged.
229 # This works out great for cvs2svn, due to lucky coincidences:
231 # For files, we set most properties in the first revision and
232 # never change them. (The only exception is the 'cvs2svn:cvs-rev'
233 # property.) If 'cvs2svn:cvs-rev' is not being used, then there
234 # is no need to remember the full set of properties on a given
235 # file once we've set it.
237 # For directories, the only property we set is "svn:ignore", and
238 # while we may change it after the first revision, we always do so
239 # based on the contents of a ".cvsignore" file -- in other words,
240 # CVS is doing the remembering for us, so we still don't have to
241 # preserve the previous value of the property ourselves.
243 # Calculate the (sorted-by-name) property string and length, if any.
244 svn_props
= cvs_rev
.get_properties()
245 if cvs_rev
.properties_changed
:
246 prop_contents
= self
._string
_for
_props
(svn_props
)
247 props_header
= 'Prop-content-length: %d\n' % len(prop_contents
)
252 data
= self
._revision
_reader
.get_content(cvs_rev
)
254 # treat .cvsignore as a directory property
255 dir_path
, basename
= path_split(cvs_rev
.get_svn_path())
256 if basename
== '.cvsignore':
257 ignore_contents
= self
._string
_for
_props
({
258 'svn:ignore' : ''.join((s
+ '\n') for s
in generate_ignores(data
))
260 ignore_len
= len(ignore_contents
)
262 # write headers, then props
263 self
._dumpfile
.write(
266 'Node-action: change\n'
267 'Prop-content-length: %d\n'
268 'Content-length: %d\n'
271 % (self
._utf
8_path
(dir_path
),
272 ignore_len
, ignore_len
, ignore_contents
)
274 if not Ctx().keep_cvsignore
:
278 checksum
.update(data
)
280 # The content length is the length of property data, text data,
281 # and any metadata around/inside around them:
282 self
._dumpfile
.write(
286 '%s' # no property header if no props
287 'Text-content-length: %d\n'
288 'Text-content-md5: %s\n'
289 'Content-length: %d\n'
291 self
._utf
8_path
(cvs_rev
.get_svn_path()), op
, props_header
,
292 len(data
), checksum
.hexdigest(), len(data
) + len(prop_contents
),
297 self
._dumpfile
.write(prop_contents
)
299 self
._dumpfile
.write(data
)
301 # This record is done (write two newlines -- one to terminate
302 # contents that weren't themselves newline-termination, one to
303 # provide a blank line for readability.
304 self
._dumpfile
.write('\n\n')
306 def add_path(self
, cvs_rev
):
307 """Emit the addition corresponding to CVS_REV, a CVSRevisionAdd."""
309 self
._add
_or
_change
_path
(cvs_rev
, OP_ADD
)
311 def change_path(self
, cvs_rev
):
312 """Emit the change corresponding to CVS_REV, a CVSRevisionChange."""
314 self
._add
_or
_change
_path
(cvs_rev
, OP_CHANGE
)
316 def delete_lod(self
, lod
):
317 """Emit the deletion of LOD."""
319 self
._dumpfile
.write(
321 'Node-action: delete\n'
323 % (self
._utf
8_path
(lod
.get_path()),)
325 self
._basic
_directories
.remove(lod
.get_path())
327 def delete_path(self
, lod
, cvs_path
):
328 dir_path
, basename
= path_split(lod
.get_path(cvs_path
.get_cvs_path()))
329 if basename
== '.cvsignore':
330 # When a .cvsignore file is deleted, the directory's svn:ignore
331 # property needs to be deleted.
332 ignore_contents
= 'PROPS-END\n'
333 ignore_len
= len(ignore_contents
)
335 # write headers, then props
336 self
._dumpfile
.write(
339 'Node-action: change\n'
340 'Prop-content-length: %d\n'
341 'Content-length: %d\n'
344 % (self
._utf
8_path
(dir_path
),
345 ignore_len
, ignore_len
, ignore_contents
)
347 if not Ctx().keep_cvsignore
:
350 self
._dumpfile
.write(
352 'Node-action: delete\n'
354 % (self
._utf
8_path
(lod
.get_path(cvs_path
.cvs_path
)),)
357 def copy_lod(self
, src_lod
, dest_lod
, src_revnum
):
358 # Register the main LOD directory, and create parent directories
360 self
._register
_basic
_directory
(dest_lod
.get_path(), False)
362 self
._dumpfile
.write(
366 'Node-copyfrom-rev: %d\n'
367 'Node-copyfrom-path: %s\n'
369 % (self
._utf
8_path
(dest_lod
.get_path()),
370 src_revnum
, self
._utf
8_path
(src_lod
.get_path()))
373 def copy_path(self
, cvs_path
, src_lod
, dest_lod
, src_revnum
):
374 if isinstance(cvs_path
, CVSFile
):
376 if cvs_path
.basename
== '.cvsignore':
377 # FIXME: Here we have to adjust the containing directory's
378 # svn:ignore property to reflect the addition of the
379 # .cvsignore file to the LOD! This is awkward because we
380 # don't have the contents of the .cvsignore file available.
381 if not Ctx().keep_cvsignore
:
383 elif isinstance(cvs_path
, CVSDirectory
):
386 raise InternalError()
388 self
._dumpfile
.write(
392 'Node-copyfrom-rev: %d\n'
393 'Node-copyfrom-path: %s\n'
396 self
._utf
8_path
(dest_lod
.get_path(cvs_path
.cvs_path
)),
399 self
._utf
8_path
(src_lod
.get_path(cvs_path
.cvs_path
))
404 """Perform any cleanup necessary after all revisions have been
407 self
._dumpfile
.close()
410 def generate_ignores(raw_ignore_val
):
412 for ignore
in raw_ignore_val
.split():
413 # Reset the list if we encounter a '!'
414 # See http://cvsbook.red-bean.com/cvsbook.html#cvsignore
418 ignore_vals
.append(ignore
)