comparison hgfastimport/hgimport.py @ 42:71f1e5ed6213

Convert "lightweight" tags, since that's how cvs2git handle CVS tags. - add lightweight_tags attr to HgImportProcessor - modify reset_handler() to detect and record lightweight tags - add write_lightweight_tags() method to add them all in one commit - update test-fastimport-cvs2git-fixup
author Greg Ward <greg-hg@gerg.ca>
date Sun, 10 May 2009 12:00:23 -0400
parents a1ccf1817b65
children 000d4174071c
comparison
equal deleted inserted replaced
41:a1ccf1817b65 42:71f1e5ed6213
26 import shutil 26 import shutil
27 27
28 import mercurial.hg 28 import mercurial.hg
29 import mercurial.commands 29 import mercurial.commands
30 from mercurial import util 30 from mercurial import util
31 from mercurial.node import nullrev 31 from mercurial.node import nullrev, hex
32 32
33 from fastimport import processor 33 from fastimport import processor
34 from hgfastimport import hgechoprocessor 34 from hgfastimport import hgechoprocessor
35 35
36 class HgImportProcessor(processor.ImportProcessor): 36 class HgImportProcessor(processor.ImportProcessor):
37 37
38 def __init__(self, ui, repo, **opts): 38 def __init__(self, ui, repo, **opts):
39 super(HgImportProcessor, self).__init__()
39 self.ui = ui 40 self.ui = ui
40 self.repo = repo 41 self.repo = repo
41 self.opts = opts 42 self.opts = opts
42 self.last_commit = None # CommitCommand object 43 self.last_commit = None # CommitCommand object
43 self.mark_map = {} # map mark (e.g. ":1") to changelog node ID(?) 44 self.mark_map = {} # map mark (e.g. ":1") to revision number
44 self.branch_map = {} # map git branch name to changelog node ID(?) 45 self.branch_map = {} # map git branch name to revision number
46 self.lightweight_tags = [] # list of (ref, mark) tuples
45 47
46 self.numblobs = 0 # for progress reporting 48 self.numblobs = 0 # for progress reporting
47 self.blobdir = None 49 self.blobdir = None
48 50
49 def setup(self): 51 def setup(self):
50 """Setup before processing any streams.""" 52 """Setup before processing any streams."""
51 pass 53 pass
52 54
53 def teardown(self): 55 def teardown(self):
54 """Cleanup after processing all streams.""" 56 """Cleanup after processing all streams."""
57 # Hmmm: this isn't really a cleanup step, it's a post-processing
58 # step. But we currently have one processor per input
59 # stream... despite the fact that state like mark_map,
60 # branch_map, and lightweight_tags really should span input
61 # streams.
62 self.write_lightweight_tags()
63
55 if self.blobdir and os.path.exists(self.blobdir): 64 if self.blobdir and os.path.exists(self.blobdir):
56 self.ui.status("Removing blob dir %r ...\n" % self.blobdir) 65 self.ui.status("Removing blob dir %r ...\n" % self.blobdir)
57 shutil.rmtree(self.blobdir) 66 shutil.rmtree(self.blobdir)
58 67
59 def progress_handler(self, cmd): 68 def progress_handler(self, cmd):
178 # print "go for it" 187 # print "go for it"
179 #return res 188 #return res
180 return "%d %d" % res 189 return "%d %d" % res
181 190
182 def reset_handler(self, cmd): 191 def reset_handler(self, cmd):
183 if cmd.from_ is not None: 192 if cmd.ref.startswith("refs/heads/"):
184 self.branch_map[cmd.ref] = self.committish_rev(cmd.from_) 193 # The usual case for 'reset': (re)create the named branch.
194 # XXX what should we do if cmd.from_ is None?
195 if cmd.from_ is not None:
196 self.branch_map[cmd.ref] = self.committish_rev(cmd.from_)
197 #else:
198 # # XXX filename? line number?
199 # self.ui.warn("ignoring branch reset with no 'from'\n")
200 elif cmd.ref.startswith("refs/tags/"):
201 # Create a "lightweight tag" in Git terms. As I understand
202 # it, that's a tag with no description and no history --
203 # rather like CVS tags. cvs2git turns CVS tags into Git
204 # lightweight tags, so we should make sure they become
205 # Mercurial tags. But we don't have to fake a history for
206 # them; save them up for the end.
207 self.lightweight_tags.append((cmd.ref, cmd.from_))
185 208
186 def tag_handler(self, cmd): 209 def tag_handler(self, cmd):
187 pass 210 pass
211
212 def write_lightweight_tags(self):
213 if not self.lightweight_tags: # avoid writing empty .hgtags
214 return
215
216 # XXX what about duplicate tags? lightweight_tags is
217 # deliberately a list, to preserve order ... but do we need to
218 # worry about repeated tags? (Certainly not for cvs2git output,
219 # since CVS has no tag history.)
220
221 # Create Mercurial tags from git-style "lightweight tags" in the
222 # input stream.
223 self.ui.status("updating tags\n")
224 mercurial.hg.clean(self.repo, self.repo.lookup("default"))
225 tagfile = open(self.repo.wjoin(".hgtags"), "ab")
226 for (ref, mark) in self.lightweight_tags:
227 tag = ref[len("refs/tags/"):]
228 rev = self.mark_map[mark]
229 node = self.repo.changelog.node(rev)
230 tagfile.write("%s %s\n" % (hex(node), tag))
231 tagfile.close()
232
233 files = [".hgtags"]
234 self.repo.rawcommit(
235 files=files, text="update tags", user="convert-repo", date=None)
188 236
189 class HgImportCommitHandler(processor.CommitHandler): 237 class HgImportCommitHandler(processor.CommitHandler):
190 238
191 def __init__(self, parent, command, ui, repo, **opts): 239 def __init__(self, parent, command, ui, repo, **opts):
192 self.parent = parent # HgImportProcessor running the show 240 self.parent = parent # HgImportProcessor running the show