cvs2git: Make the --blobfile argument optional.
[cvs2svn.git] / cvs2svn_lib / git_revision_collector.py
blob70cd297502992818c5fa25966bb9f8ee4c2e89f4
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 """Write file contents to a stream of git-fast-import blobs."""
19 from cvs2svn_lib import config
20 from cvs2svn_lib.cvs_item import CVSRevisionDelete
21 from cvs2svn_lib.revision_manager import RevisionCollector
22 from cvs2svn_lib.key_generator import KeyGenerator
23 from cvs2svn_lib.artifact_manager import artifact_manager
26 class GitRevisionCollector(RevisionCollector):
27 """Output file revisions to git-fast-import."""
29 def __init__(self, revision_reader, blob_filename=None):
30 self.revision_reader = revision_reader
31 self.blob_filename = blob_filename
33 def register_artifacts(self, which_pass):
34 self.revision_reader.register_artifacts(which_pass)
35 if self.blob_filename is None:
36 artifact_manager.register_temp_file(
37 config.GIT_BLOB_DATAFILE, which_pass,
40 def start(self):
41 self.revision_reader.start()
42 if self.blob_filename is None:
43 self.dump_file = open(
44 artifact_manager.get_temp_file(config.GIT_BLOB_DATAFILE), 'wb',
46 else:
47 self.dump_file = open(self.blob_filename, 'wb')
48 self._mark_generator = KeyGenerator()
50 def _process_revision(self, cvs_rev):
51 """Write the revision fulltext to a blob if it is not dead."""
53 if isinstance(cvs_rev, CVSRevisionDelete):
54 # There is no need to record a delete revision, and its token
55 # will never be needed:
56 return
58 # FIXME: We have to decide what to do about keyword substitution
59 # and eol_style here:
60 fulltext = self.revision_reader.get_content(cvs_rev)
62 mark = self._mark_generator.gen_id()
63 self.dump_file.write('blob\n')
64 self.dump_file.write('mark :%d\n' % (mark,))
65 self.dump_file.write('data %d\n' % (len(fulltext),))
66 self.dump_file.write(fulltext)
67 self.dump_file.write('\n')
68 cvs_rev.revision_reader_token = mark
70 def _process_symbol(self, cvs_symbol, cvs_file_items):
71 """Record the original source of CVS_SYMBOL.
73 Determine the original revision source of CVS_SYMBOL, and store it
74 as the symbol's revision_reader_token."""
76 cvs_source = cvs_symbol.get_cvs_revision_source(cvs_file_items)
77 cvs_symbol.revision_reader_token = cvs_source.revision_reader_token
79 def process_file(self, cvs_file_items):
80 for lod_items in cvs_file_items.iter_lods():
81 for cvs_rev in lod_items.cvs_revisions:
82 self._process_revision(cvs_rev)
84 # Now that all CVSRevisions' revision_reader_tokens are set,
85 # iterate through symbols and set their tokens to those of their
86 # original source revisions:
87 for lod_items in cvs_file_items.iter_lods():
88 if lod_items.cvs_branch is not None:
89 self._process_symbol(lod_items.cvs_branch, cvs_file_items)
90 for cvs_tag in lod_items.cvs_tags:
91 self._process_symbol(cvs_tag, cvs_file_items)
93 def finish(self):
94 self.revision_reader.finish()
95 self.dump_file.close()