Split up too-long line.
[cvs2svn.git] / cvs2svn_lib / dvcs_common.py
blobfe2758d05a1ba4885bc2732912db22d2ae0fb626
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 Log
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 if self.propname in cvs_file.properties:
62 return
64 cvs_file.properties[self.propname] = self.value
67 class DVCSRunOptions(RunOptions):
68 """Dumping ground for whatever is common to GitRunOptions and
69 HgRunOptions."""
70 def __init__(self, progname, cmd_args, pass_manager):
71 Ctx().cross_project_commits = False
72 Ctx().cross_branch_commits = False
73 RunOptions.__init__(self, progname, cmd_args, pass_manager)
75 def set_project(
76 self,
77 project_cvs_repos_path,
78 symbol_transforms=None,
79 symbol_strategy_rules=[],
81 """Set the project to be converted.
83 If a project had already been set, overwrite it.
85 Most arguments are passed straight through to the Project
86 constructor. SYMBOL_STRATEGY_RULES is an iterable of
87 SymbolStrategyRules that will be applied to symbols in this
88 project."""
90 symbol_strategy_rules = list(symbol_strategy_rules)
92 project = Project(
94 project_cvs_repos_path,
95 symbol_transforms=symbol_transforms,
98 self.projects = [project]
99 self.project_symbol_strategy_rules = [symbol_strategy_rules]
101 def process_property_setter_options(self):
102 super(DVCSRunOptions, self).process_property_setter_options()
104 # Property setters for internal use:
105 Ctx().file_property_setters.append(
106 KeywordHandlingPropertySetter('collapsed')
109 def process_options(self):
110 # Consistency check for options and arguments.
111 if len(self.args) == 0:
112 self.usage()
113 sys.exit(1)
115 if len(self.args) > 1:
116 Log().error(error_prefix + ": must pass only one CVS repository.\n")
117 self.usage()
118 sys.exit(1)
120 cvsroot = self.args[0]
122 self.process_extraction_options()
123 self.process_output_options()
124 self.process_symbol_strategy_options()
125 self.process_property_setter_options()
127 # Create the project:
128 self.set_project(
129 cvsroot,
130 symbol_transforms=self.options.symbol_transforms,
131 symbol_strategy_rules=self.options.symbol_strategy_rules,
135 class DVCSOutputOption(OutputOption):
136 # name of output format (for error messages); must be set by
137 # subclasses
138 name = None
140 def __init__(self):
141 self._mirror = RepositoryMirror()
142 self._symbolings_reader = None
144 def normalize_author_transforms(self, author_transforms):
145 """Convert AUTHOR_TRANSFORMS into author strings.
147 AUTHOR_TRANSFORMS is a dict { CVSAUTHOR : DVCSAUTHOR } where
148 CVSAUTHOR is the CVS author and DVCSAUTHOR is either:
150 * a tuple (NAME, EMAIL) where NAME and EMAIL are strings. Such
151 entries are converted into a UTF-8 string of the form 'name
152 <email>'.
154 * a string already in the form 'name <email>'.
156 Return a similar dict { CVSAUTHOR : DVCSAUTHOR } where all keys
157 and values are UTF-8-encoded strings.
159 Any of the input strings may be Unicode strings (in which case
160 they are encoded to UTF-8) or 8-bit strings (in which case they
161 are used as-is). Also turns None into the empty dict."""
163 result = {}
164 if author_transforms is not None:
165 for (cvsauthor, dvcsauthor) in author_transforms.iteritems():
166 cvsauthor = to_utf8(cvsauthor)
167 if isinstance(dvcsauthor, basestring):
168 dvcsauthor = to_utf8(dvcsauthor)
169 else:
170 (name, email,) = dvcsauthor
171 name = to_utf8(name)
172 email = to_utf8(email)
173 dvcsauthor = "%s <%s>" % (name, email,)
174 result[cvsauthor] = dvcsauthor
175 return result
177 def register_artifacts(self, which_pass):
178 # These artifacts are needed for SymbolingsReader:
179 artifact_manager.register_temp_file_needed(
180 config.SYMBOL_OPENINGS_CLOSINGS_SORTED, which_pass
182 artifact_manager.register_temp_file_needed(
183 config.SYMBOL_OFFSETS_DB, which_pass
185 self._mirror.register_artifacts(which_pass)
187 def check(self):
188 if Ctx().cross_project_commits:
189 raise FatalError(
190 '%s output is not supported with cross-project commits' % self.name
192 if Ctx().cross_branch_commits:
193 raise FatalError(
194 '%s output is not supported with cross-branch commits' % self.name
196 if Ctx().username is None:
197 raise FatalError(
198 '%s output requires a default commit username' % self.name
201 def setup(self, svn_rev_count):
202 self._symbolings_reader = SymbolingsReader()
203 self._mirror.open()
205 def cleanup(self):
206 self._mirror.close()
207 self._symbolings_reader.close()
208 del self._symbolings_reader
210 def _get_source_groups(self, svn_commit):
211 """Return groups of sources for SVN_COMMIT.
213 SVN_COMMIT is an instance of SVNSymbolCommit. Yield tuples
214 (source_lod, svn_revnum, cvs_symbols) where source_lod is the line
215 of development and svn_revnum is the revision that should serve as
216 a source, and cvs_symbols is a list of CVSSymbolItems that can be
217 copied from that source. The groups are returned in arbitrary
218 order."""
220 # Get a map {CVSSymbol : SVNRevisionRange}:
221 range_map = self._symbolings_reader.get_range_map(svn_commit)
223 # range_map, split up into one map per LOD; i.e., {LOD :
224 # {CVSSymbol : SVNRevisionRange}}:
225 lod_range_maps = {}
227 for (cvs_symbol, range) in range_map.iteritems():
228 lod_range_map = lod_range_maps.get(range.source_lod)
229 if lod_range_map is None:
230 lod_range_map = {}
231 lod_range_maps[range.source_lod] = lod_range_map
232 lod_range_map[cvs_symbol] = range
234 # Sort the sources so that the branch that serves most often as
235 # parent is processed first:
236 lod_ranges = lod_range_maps.items()
237 lod_ranges.sort(
238 lambda (lod1,lod_range_map1),(lod2,lod_range_map2):
239 -cmp(len(lod_range_map1), len(lod_range_map2)) or cmp(lod1, lod2)
242 for (lod, lod_range_map) in lod_ranges:
243 while lod_range_map:
244 revision_scores = RevisionScores(lod_range_map.values())
245 (source_lod, revnum, score) = revision_scores.get_best_revnum()
246 assert source_lod == lod
247 cvs_symbols = []
248 for (cvs_symbol, range) in lod_range_map.items():
249 if revnum in range:
250 cvs_symbols.append(cvs_symbol)
251 del lod_range_map[cvs_symbol]
252 yield (lod, revnum, cvs_symbols)
254 def _is_simple_copy(self, svn_commit, source_groups):
255 """Return True iff SVN_COMMIT can be created as a simple copy.
257 SVN_COMMIT is an SVNTagCommit. Return True iff it can be created
258 as a simple copy from an existing revision (i.e., if the fixup
259 branch can be avoided for this tag creation)."""
261 # The first requirement is that there be exactly one source:
262 if len(source_groups) != 1:
263 return False
265 (source_lod, svn_revnum, cvs_symbols) = source_groups[0]
267 # The second requirement is that the destination LOD not already
268 # exist:
269 try:
270 self._mirror.get_current_lod_directory(svn_commit.symbol)
271 except KeyError:
272 # The LOD doesn't already exist. This is good.
273 pass
274 else:
275 # The LOD already exists. It cannot be created by a copy.
276 return False
278 # The third requirement is that the source LOD contains exactly
279 # the same files as we need to add to the symbol:
280 try:
281 source_node = self._mirror.get_old_lod_directory(source_lod, svn_revnum)
282 except KeyError:
283 raise InternalError('Source %r does not exist' % (source_lod,))
284 return (
285 set([cvs_symbol.cvs_file for cvs_symbol in cvs_symbols])
286 == set(self._get_all_files(source_node))
289 def _get_all_files(self, node):
290 """Generate all of the CVSFiles under NODE."""
292 for cvs_path in node:
293 subnode = node[cvs_path]
294 if subnode is None:
295 yield cvs_path
296 else:
297 for sub_cvs_path in self._get_all_files(subnode):
298 yield sub_cvs_path
301 class MirrorUpdater(object):
302 def register_artifacts(self, which_pass):
303 pass
305 def start(self, mirror):
306 self._mirror = mirror
308 def _mkdir_p(self, cvs_directory, lod):
309 """Make sure that CVS_DIRECTORY exists in LOD.
311 If not, create it. Return the node for CVS_DIRECTORY."""
313 try:
314 node = self._mirror.get_current_lod_directory(lod)
315 except KeyError:
316 node = self._mirror.add_lod(lod)
318 for sub_path in cvs_directory.get_ancestry()[1:]:
319 try:
320 node = node[sub_path]
321 except KeyError:
322 node = node.mkdir(sub_path)
323 if node is None:
324 raise ExpectedDirectoryError(
325 'File found at \'%s\' where directory was expected.' % (sub_path,)
328 return node
330 def add_file(self, cvs_rev, post_commit):
331 cvs_file = cvs_rev.cvs_file
332 if post_commit:
333 lod = cvs_file.project.get_trunk()
334 else:
335 lod = cvs_rev.lod
336 parent_node = self._mkdir_p(cvs_file.parent_directory, lod)
337 parent_node.add_file(cvs_file)
339 def modify_file(self, cvs_rev, post_commit):
340 cvs_file = cvs_rev.cvs_file
341 if post_commit:
342 lod = cvs_file.project.get_trunk()
343 else:
344 lod = cvs_rev.lod
345 if self._mirror.get_current_path(cvs_file, lod) is not None:
346 raise ExpectedFileError(
347 'Directory found at \'%s\' where file was expected.' % (cvs_file,)
350 def delete_file(self, cvs_rev, post_commit):
351 cvs_file = cvs_rev.cvs_file
352 if post_commit:
353 lod = cvs_file.project.get_trunk()
354 else:
355 lod = cvs_rev.lod
356 parent_node = self._mirror.get_current_path(
357 cvs_file.parent_directory, lod
359 if parent_node[cvs_file] is not None:
360 raise ExpectedFileError(
361 'Directory found at \'%s\' where file was expected.' % (cvs_file,)
363 del parent_node[cvs_file]
365 def process_revision(self, cvs_rev, post_commit):
366 if isinstance(cvs_rev, CVSRevisionAdd):
367 self.add_file(cvs_rev, post_commit)
368 elif isinstance(cvs_rev, CVSRevisionChange):
369 self.modify_file(cvs_rev, post_commit)
370 elif isinstance(cvs_rev, CVSRevisionDelete):
371 self.delete_file(cvs_rev, post_commit)
372 elif isinstance(cvs_rev, CVSRevisionNoop):
373 pass
374 else:
375 raise InternalError('Unexpected CVSRevision type: %s' % (cvs_rev,))
377 def branch_file(self, cvs_symbol):
378 cvs_file = cvs_symbol.cvs_file
379 parent_node = self._mkdir_p(cvs_file.parent_directory, cvs_symbol.symbol)
380 parent_node.add_file(cvs_file)
382 def finish(self):
383 del self._mirror
386 def to_utf8(s):
387 if isinstance(s, unicode):
388 return s.encode('utf8')
389 else:
390 return s