[PATCH] convert-repo: --ignore and --usermap for CVS

Timo Sirainen tss at iki.fi
Fri May 18 18:56:12 UTC 2007


--usermap=file takes a file in CVSROOT/users file format

--ignore=file ignores a file completely in conversion. With CVS I was
generating a ChangeLog file and committing it all the time. I really
didn't want those 7000 changesets into Mercurial repository.

If the changes have something stupid in them, it's because I'm not a
real Python programmer.

# HG changeset patch
# User Timo Sirainen <tss at iki.fi>
# Date 1179513790 -10800
# Node ID d6c1406103506efe8c86f639b1d311a19d10a187
# Parent  3900f684a150ba9a847c499dd7bf6bf139070866
Added --ignore=<file> and --usermap=<file> options for CVS converter.

diff -r 3900f684a150 -r d6c140610350 contrib/convert-repo
--- a/contrib/convert-repo	Thu May 17 09:29:30 2007 -0700
+++ b/contrib/convert-repo	Fri May 18 21:43:10 2007 +0300
@@ -58,11 +58,21 @@ def recode(s):
 
 # CVS conversion code inspired by hg-cvs-import and git-cvsimport
 class convert_cvs:
-    def __init__(self, path):
+    def __init__(self, path, opts):
         self.path = path
         cvs = os.path.join(path, "CVS")
         if not os.path.exists(cvs):
             raise NoRepo("couldn't open CVS repo %s" % path)
+
+        self.usermap = {}
+        if opts.get('usermap'):
+            for l in file(opts['usermap']):
+                user, name = l[:-1].split(":")
+                self.usermap[user] = name
+
+        self.file_ignores = {}
+        if opts.get('ignore'):
+            self.file_ignores[opts['ignore']] = True
 
         self.changeset = {}
         self.files = {}
@@ -84,23 +94,24 @@ class convert_cvs:
         try:
             os.chdir(self.path)
             id = None
+            ancestor = None
             state = 0
             for l in os.popen("cvsps -A -u --cvs-direct -q"):
                 if state == 0: # header
                     if l.startswith("PatchSet"):
                         id = l[9:-2]
+                        ancestor = None
                     elif l.startswith("Date"):
                         date = util.parsedate(l[6:-1], ["%Y/%m/%d %H:%M:%S"])
                         date = util.datestr(date)
                     elif l.startswith("Branch"):
                         branch = l[8:-1]
-                        self.parent[id] = self.lastbranch.get(branch,'bad')
-                        self.lastbranch[branch] = id
                     elif l.startswith("Ancestor branch"):
                         ancestor = l[17:-1]
-                        self.parent[id] = self.lastbranch[ancestor]
                     elif l.startswith("Author"):
                         author = self.recode(l[8:-1])
+                        if self.usermap.has_key(author):
+                          author = self.usermap[author]
                     elif l.startswith("Tag: "):
                         t = l[5:-1].rstrip()
                         if t != "(none)":
@@ -120,17 +131,23 @@ class convert_cvs:
                 elif state == 2:
                     if l == "\n": #
                         state = 0
-                        p = [self.parent[id]]
-                        if id == "1":
-                            p = []
-                        c = commit(author=author, date=date, parents=p,
-                                   desc=log, branch=branch)
-                        self.changeset[id] = c
-                        self.files[id] = files
+                        if len(files) > 0:
+                          self.parent[id] = self.lastbranch.get(branch,'bad')
+                          self.lastbranch[branch] = id
+                          if ancestor is not None:
+                            self.parent[id] = self.lastbranch[ancestor]
+                          p = [self.parent[id]]
+                          if id == "1":
+                              p = []
+                          c = commit(author=author, date=date, parents=p,
+                                     desc=log, branch=branch)
+                          self.changeset[id] = c
+                          self.files[id] = files
                     else:
                         file,rev = l[1:-2].rsplit(':',1)
                         rev = rev.split("->")[1]
-                        files[file] = rev
+                        if not self.file_ignores.has_key(file):
+                          files[file] = rev
 
             self.heads = self.lastbranch.values()
         finally:
@@ -289,7 +306,7 @@ class convert_cvs:
         return self.tags
 
 class convert_git:
-    def __init__(self, path):
+    def __init__(self, path, opts):
         if os.path.isdir(path + "/.git"):
             path += "/.git"
         self.path = path
@@ -375,7 +392,7 @@ class convert_git:
         return tags
 
 class convert_mercurial:
-    def __init__(self, path):
+    def __init__(self, path, opts):
         self.path = path
         u = ui.ui()
         try:
@@ -461,12 +478,12 @@ class convert_mercurial:
 
 converters = [convert_cvs, convert_git, convert_mercurial]
 
-def converter(path):
+def converter(path, opts):
     if not os.path.isdir(path):
         abort("%s: not a directory\n" % path)
     for c in converters:
         try:
-            return c(path)
+            return c(path, opts)
         except NoRepo:
             pass
     abort("%s: unknown repository type\n" % path)
@@ -611,7 +628,7 @@ class convert:
                 file(self.mapfile, "a").write("%s %s\n" % (c, nrev))
 
 def command(src, dest=None, mapfile=None, **opts):
-    srcc = converter(src)
+    srcc = converter(src, opts)
     if not hasattr(srcc, "getcommit"):
         abort("%s: can't read from this repo type\n" % src)
 
@@ -621,7 +638,7 @@ def command(src, dest=None, mapfile=None
         if not os.path.isdir(dest):
             status("creating repository %s\n" % dest)
             os.system("hg init " + dest)
-    destc = converter(dest)
+    destc = converter(dest, opts)
     if not hasattr(destc, "putcommit"):
         abort("%s: can't write to this repo type\n" % src)
 
@@ -635,7 +652,9 @@ def command(src, dest=None, mapfile=None
     c.convert()
 
 options = [('q', 'quiet', None, 'suppress output'),
-           ('', 'datesort', None, 'try to sort changesets by date')]
+           ('', 'datesort', None, 'try to sort changesets by date'),
+           ('', 'ignore', '', 'ignore the given file'),
+           ('', 'usermap', '', 'username map file')]
 opts = {}
 args = fancyopts.fancyopts(sys.argv[1:], options, opts)
 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://lists.mercurial-scm.org/pipermail/mercurial/attachments/20070518/4326f939/attachment-0001.asc>


More information about the Mercurial mailing list