Add option for excluding paths from conversion
[cvs2svn.git] / cvs2svn_lib / dvcs_common.py
blobb3dee5f1c2dbde72b114ab2d3af470563a9c0a8c
1 # (Be in -*- python -*- mode.)
3 # ====================================================================
4 # Copyright (c) 2007-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 """Miscellaneous utility code common to DVCS backends (like
18 Git, Mercurial, or Bazaar).
19 """
21 import sys
23 from cvs2svn_lib import config
24 from cvs2svn_lib.common import FatalError
25 from cvs2svn_lib.common import InternalError
26 from cvs2svn_lib.run_options import RunOptions
27 from cvs2svn_lib.log import logger
28 from cvs2svn_lib.common import error_prefix
29 from cvs2svn_lib.context import Ctx
30 from cvs2svn_lib.artifact_manager import artifact_manager
31 from cvs2svn_lib.project import Project
32 from cvs2svn_lib.cvs_item import CVSRevisionAdd
33 from cvs2svn_lib.cvs_item import CVSRevisionChange
34 from cvs2svn_lib.cvs_item import CVSRevisionDelete
35 from cvs2svn_lib.cvs_item import CVSRevisionNoop
36 from cvs2svn_lib.svn_revision_range import RevisionScores
37 from cvs2svn_lib.openings_closings import SymbolingsReader
38 from cvs2svn_lib.repository_mirror import RepositoryMirror
39 from cvs2svn_lib.output_option import OutputOption
40 from cvs2svn_lib.property_setters import FilePropertySetter
43 class KeywordHandlingPropertySetter(FilePropertySetter):
44 """Set property _keyword_handling to a specified value.
46 This keyword is used to tell the RevisionReader whether it has to
47 collapse/expand RCS keywords when generating the fulltext or leave
48 them alone."""
50 propname = '_keyword_handling'
52 def __init__(self, value):
53 if value not in ['collapsed', 'expanded', 'untouched', None]:
54 raise FatalError(
55 'Value for %s must be "collapsed", "expanded", or "untouched"'
56 % (self.propname,)
58 self.value = value
60 def set_properties(self, cvs_file):
61 self.maybe_set_property(cvs_file, self.propname, self.value)
64 class DVCSRunOptions(RunOptions):
65 """Dumping ground for whatever is common to GitRunOptions and
66 HgRunOptions."""
67 def __init__(self, progname, cmd_args, pass_manager):
68 Ctx().cross_project_commits = False
69 Ctx().cross_branch_commits = False
70 RunOptions.__init__(self, progname, cmd_args, pass_manager)
72 def set_project(
73 self,
74 project_cvs_repos_path,
75 symbol_transforms=None,
76 symbol_strategy_rules=[],
77 exclude_paths=set(),
79 """Set the project to be converted.
81 If a project had already been set, overwrite it.
83 Most arguments are passed straight through to the Project
84 constructor. SYMBOL_STRATEGY_RULES is an iterable of
85 SymbolStrategyRules that will be applied to symbols in this
86 project."""
88 symbol_strategy_rules = list(symbol_strategy_rules)
90 project = Project(
92 project_cvs_repos_path,
93 symbol_transforms=symbol_transforms,
94 exclude_paths=exclude_paths,
97 self.projects = [project]
98 self.project_symbol_strategy_rules = [symbol_strategy_rules]
100 def process_property_setter_options(self):
101 super(DVCSRunOptions, self).process_property_setter_options()
103 # Property setters for internal use:
104 Ctx().file_property_setters.append(
105 KeywordHandlingPropertySetter('collapsed')
108 def process_options(self):
109 # Consistency check for options and arguments.
110 if len(self.args) == 0:
111 self.usage()
112 sys.exit(1)
114 if len(self.args) > 1:
115 logger.error(error_prefix + ": must pass only one CVS repository.\n")
116 self.usage()
117 sys.exit(1)
119 cvsroot = self.args[0]
121 self.process_extraction_options()
122 self.process_output_options()
123 self.process_symbol_strategy_options()
124 self.process_property_setter_options()
126 # Create the project:
127 self.set_project(
128 cvsroot,
129 symbol_transforms=self.options.symbol_transforms,
130 symbol_strategy_rules=self.options.symbol_strategy_rules,
134 class DVCSOutputOption(OutputOption):
135 def __init__(self):
136 self._mirror = RepositoryMirror()
137 self._symbolings_reader = None
139 def normalize_author_transforms(self, author_transforms):
140 """Convert AUTHOR_TRANSFORMS into author strings.
142 AUTHOR_TRANSFORMS is a dict { CVSAUTHOR : DVCSAUTHOR } where
143 CVSAUTHOR is the CVS author and DVCSAUTHOR is either:
145 * a tuple (NAME, EMAIL) where NAME and EMAIL are strings. Such
146 entries are converted into a UTF-8 string of the form 'name
147 <email>'.
149 * a string already in the form 'name <email>'.
151 Return a similar dict { CVSAUTHOR : DVCSAUTHOR } where all keys
152 and values are UTF-8-encoded strings.
154 Any of the input strings may be Unicode strings (in which case
155 they are encoded to UTF-8) or 8-bit strings (in which case they
156 are used as-is). Also turns None into the empty dict."""
158 result = {}
159 if author_transforms is not None:
160 for (cvsauthor, dvcsauthor) in author_transforms.iteritems():
161 cvsauthor = to_utf8(cvsauthor)
162 if isinstance(dvcsauthor, basestring):
163 dvcsauthor = to_utf8(dvcsauthor)
164 else:
165 (name, email,) = dvcsauthor
166 name = to_utf8(name)
167 email = to_utf8(email)
168 dvcsauthor = "%s <%s>" % (name, email,)
169 result[cvsauthor] = dvcsauthor
170 return result
172 def register_artifacts(self, which_pass):
173 # These artifacts are needed for SymbolingsReader:
174 artifact_manager.register_temp_file_needed(
175 config.SYMBOL_OPENINGS_CLOSINGS_SORTED, which_pass
177 artifact_manager.register_temp_file_needed(
178 config.SYMBOL_OFFSETS_DB, which_pass
180 self._mirror.register_artifacts(which_pass)
182 def check(self):
183 if Ctx().cross_project_commits:
184 raise FatalError(
185 '%s output is not supported with cross-project commits' % self.name
187 if Ctx().cross_branch_commits:
188 raise FatalError(
189 '%s output is not supported with cross-branch commits' % self.name
191 if Ctx().username is None:
192 raise FatalError(
193 '%s output requires a default commit username' % self.name
196 def setup(self, svn_rev_count):
197 self._symbolings_reader = SymbolingsReader()
198 self._mirror.open()
200 def cleanup(self):
201 self._mirror.close()
202 self._symbolings_reader.close()
203 del self._symbolings_reader
205 def _get_source_groups(self, svn_commit):
206 """Return groups of sources for SVN_COMMIT.
208 SVN_COMMIT is an instance of SVNSymbolCommit. Return a list of tuples
209 (svn_revnum, source_lod, cvs_symbols) where svn_revnum is the revision
210 that should serve as a source, source_lod is the CVS line of
211 development, and cvs_symbols is a list of CVSSymbolItems that can be
212 copied from that source. The list is in arbitrary order."""
214 # Get a map {CVSSymbol : SVNRevisionRange}:
215 range_map = self._symbolings_reader.get_range_map(svn_commit)
217 # range_map, split up into one map per LOD; i.e., {LOD :
218 # {CVSSymbol : SVNRevisionRange}}:
219 lod_range_maps = {}
221 for (cvs_symbol, range) in range_map.iteritems():
222 lod_range_map = lod_range_maps.get(range.source_lod)
223 if lod_range_map is None:
224 lod_range_map = {}
225 lod_range_maps[range.source_lod] = lod_range_map
226 lod_range_map[cvs_symbol] = range
228 # Sort the sources so that the branch that serves most often as
229 # parent is processed first:
230 lod_ranges = lod_range_maps.items()
231 lod_ranges.sort(
232 lambda (lod1,lod_range_map1),(lod2,lod_range_map2):
233 -cmp(len(lod_range_map1), len(lod_range_map2)) or cmp(lod1, lod2)
236 source_groups = []
237 for (lod, lod_range_map) in lod_ranges:
238 while lod_range_map:
239 revision_scores = RevisionScores(lod_range_map.values())
240 (source_lod, revnum, score) = revision_scores.get_best_revnum()
241 assert source_lod == lod
242 cvs_symbols = []
243 for (cvs_symbol, range) in lod_range_map.items():
244 if revnum in range:
245 cvs_symbols.append(cvs_symbol)
246 del lod_range_map[cvs_symbol]
247 source_groups.append((revnum, lod, cvs_symbols))
249 return source_groups
251 def _is_simple_copy(self, svn_commit, source_groups):
252 """Return True iff SVN_COMMIT can be created as a simple copy.
254 SVN_COMMIT is an SVNTagCommit. Return True iff it can be created
255 as a simple copy from an existing revision (i.e., if the fixup
256 branch can be avoided for this tag creation)."""
258 # The first requirement is that there be exactly one source:
259 if len(source_groups) != 1:
260 return False
262 (svn_revnum, source_lod, cvs_symbols) = source_groups[0]
264 # The second requirement is that the destination LOD not already
265 # exist:
266 try:
267 self._mirror.get_current_lod_directory(svn_commit.symbol)
268 except KeyError:
269 # The LOD doesn't already exist. This is good.
270 pass
271 else:
272 # The LOD already exists. It cannot be created by a copy.
273 return False
275 # The third requirement is that the source LOD contains exactly
276 # the same files as we need to add to the symbol:
277 try:
278 source_node = self._mirror.get_old_lod_directory(source_lod, svn_revnum)
279 except KeyError:
280 raise InternalError('Source %r does not exist' % (source_lod,))
281 return (
282 set([cvs_symbol.cvs_file for cvs_symbol in cvs_symbols])
283 == set(self._get_all_files(source_node))
286 def _get_all_files(self, node):
287 """Generate all of the CVSFiles under NODE."""
289 for cvs_path in node:
290 subnode = node[cvs_path]
291 if subnode is None:
292 yield cvs_path
293 else:
294 for sub_cvs_path in self._get_all_files(subnode):
295 yield sub_cvs_path
298 class MirrorUpdater(object):
299 def register_artifacts(self, which_pass):
300 pass
302 def start(self, mirror):
303 self._mirror = mirror
305 def _mkdir_p(self, cvs_directory, lod):
306 """Make sure that CVS_DIRECTORY exists in LOD.
308 If not, create it. Return the node for CVS_DIRECTORY."""
310 try:
311 node = self._mirror.get_current_lod_directory(lod)
312 except KeyError:
313 node = self._mirror.add_lod(lod)
315 for sub_path in cvs_directory.get_ancestry()[1:]:
316 try:
317 node = node[sub_path]
318 except KeyError:
319 node = node.mkdir(sub_path)
320 if node is None:
321 raise ExpectedDirectoryError(
322 'File found at \'%s\' where directory was expected.' % (sub_path,)
325 return node
327 def add_file(self, cvs_rev, post_commit):
328 cvs_file = cvs_rev.cvs_file
329 if post_commit:
330 lod = cvs_file.project.get_trunk()
331 else:
332 lod = cvs_rev.lod
333 parent_node = self._mkdir_p(cvs_file.parent_directory, lod)
334 parent_node.add_file(cvs_file)
336 def modify_file(self, cvs_rev, post_commit):
337 cvs_file = cvs_rev.cvs_file
338 if post_commit:
339 lod = cvs_file.project.get_trunk()
340 else:
341 lod = cvs_rev.lod
342 if self._mirror.get_current_path(cvs_file, lod) is not None:
343 raise ExpectedFileError(
344 'Directory found at \'%s\' where file was expected.' % (cvs_file,)
347 def delete_file(self, cvs_rev, post_commit):
348 cvs_file = cvs_rev.cvs_file
349 if post_commit:
350 lod = cvs_file.project.get_trunk()
351 else:
352 lod = cvs_rev.lod
353 parent_node = self._mirror.get_current_path(
354 cvs_file.parent_directory, lod
356 if parent_node[cvs_file] is not None:
357 raise ExpectedFileError(
358 'Directory found at \'%s\' where file was expected.' % (cvs_file,)
360 del parent_node[cvs_file]
362 def process_revision(self, cvs_rev, post_commit):
363 if isinstance(cvs_rev, CVSRevisionAdd):
364 self.add_file(cvs_rev, post_commit)
365 elif isinstance(cvs_rev, CVSRevisionChange):
366 self.modify_file(cvs_rev, post_commit)
367 elif isinstance(cvs_rev, CVSRevisionDelete):
368 self.delete_file(cvs_rev, post_commit)
369 elif isinstance(cvs_rev, CVSRevisionNoop):
370 pass
371 else:
372 raise InternalError('Unexpected CVSRevision type: %s' % (cvs_rev,))
374 def branch_file(self, cvs_symbol):
375 cvs_file = cvs_symbol.cvs_file
376 parent_node = self._mkdir_p(cvs_file.parent_directory, cvs_symbol.symbol)
377 parent_node.add_file(cvs_file)
379 def finish(self):
380 del self._mirror
383 def to_utf8(s):
384 if isinstance(s, unicode):
385 return s.encode('utf8')
386 else:
387 return s