changeset 6717:2011bb8ada9a

convert: hg sink commits without working dir
author Patrick Mezard <pmezard@gmail.com>
date Thu, 19 Jun 2008 00:14:24 +0200
parents c9b8d2565b92
children 4386a7706828
files hgext/convert/hg.py tests/test-convert-cvs tests/test-convert-cvs-builtincvsps tests/test-convert-cvs-builtincvsps.out tests/test-convert-cvs.out
diffstat 5 files changed, 70 insertions(+), 77 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/convert/hg.py	Thu Jun 19 00:14:24 2008 +0200
+++ b/hgext/convert/hg.py	Thu Jun 19 00:14:24 2008 +0200
@@ -17,7 +17,7 @@
 from mercurial.i18n import _
 from mercurial.repo import RepoError
 from mercurial.node import bin, hex, nullid
-from mercurial import hg, revlog, util
+from mercurial import hg, revlog, util, context
 
 from common import NoRepo, commit, converter_source, converter_sink
 
@@ -54,11 +54,9 @@
         self.ui.debug(_('run hg sink pre-conversion action\n'))
         self.wlock = self.repo.wlock()
         self.lock = self.repo.lock()
-        self.repo.dirstate.clear()
 
     def after(self):
         self.ui.debug(_('run hg sink post-conversion action\n'))
-        self.repo.dirstate.invalidate()
         self.lock = None
         self.wlock = None
 
@@ -111,30 +109,18 @@
             self.before()
 
     def putcommit(self, files, copies, parents, commit, source):
-        # Apply changes to working copy
-        for f, v in files:
-            try:
-                data = source.getfile(f, v)
-            except IOError, inst:
-                try:
-                    util.unlink(self.repo.wjoin(f))
-                except OSError:
-                    pass
-            else:
-                e = source.getmode(f, v)
-                self.repo.wwrite(f, data, e)
-                if f not in self.repo.dirstate:
-                    self.repo.dirstate.normallookup(f)
-                if f in copies:
-                    self.repo.copy(copies[f], f)
-        files = [f[0] for f in files]
 
-        seen = {}
+        files = dict(files)
+        def getfilectx(repo, memctx, f):
+            v = files[f]
+            data = source.getfile(f, v)
+            e = source.getmode(f, v)
+            return context.memfilectx(f, data, 'l' in e, 'x' in e, copies.get(f))
+
         pl = []
         for p in parents:
-            if p not in seen:
+            if p not in pl:
                 pl.append(p)
-                seen[p] = 1
         parents = pl
         nparents = len(parents)
         if self.filemapmode and nparents == 1:
@@ -155,9 +141,9 @@
         while parents:
             p1 = p2
             p2 = parents.pop(0)
-            a = self.repo.rawcommit(files, text, commit.author, commit.date,
-                                    bin(p1), bin(p2), extra=extra)
-            self.repo.dirstate.clear()
+            ctx = context.memctx(self.repo, (p1, p2), text, files.keys(), getfilectx,
+                                 commit.author, commit.date, extra)
+            a = self.repo.commitctx(ctx)
             text = "(octopus merge fixup)\n"
             p2 = hex(self.repo.changelog.tip())
 
@@ -166,43 +152,42 @@
             mnode = self.repo.changelog.read(bin(p2))[0]
             if not man.cmp(m1node, man.revision(mnode)):
                 self.repo.rollback()
-                self.repo.dirstate.clear()
                 return parent
         return p2
 
     def puttags(self, tags):
-        try:
-            old = self.repo.wfile(".hgtags").read()
-            oldlines = old.splitlines(1)
-            oldlines.sort()
-        except:
-            oldlines = []
+         try:
+             parentctx = self.repo.changectx(self.tagsbranch)
+             tagparent = parentctx.node()
+         except RepoError, inst:
+             parentctx = None
+             tagparent = nullid
 
-        k = tags.keys()
-        k.sort()
-        newlines = []
-        for tag in k:
-            newlines.append("%s %s\n" % (tags[tag], tag))
-
-        newlines.sort()
+         try:
+             old = parentctx.filectx(".hgtags").data()
+             oldlines = old.splitlines(1)
+             oldlines.sort()
+         except:
+             oldlines = []
 
-        if newlines != oldlines:
-            self.ui.status("updating tags\n")
-            f = self.repo.wfile(".hgtags", "w")
-            f.write("".join(newlines))
-            f.close()
-            if not oldlines: self.repo.add([".hgtags"])
-            date = "%s 0" % int(time.mktime(time.gmtime()))
-            extra = {}
-            if self.tagsbranch != 'default':
-                extra['branch'] = self.tagsbranch
-            try:
-                tagparent = self.repo.changectx(self.tagsbranch).node()
-            except RepoError, inst:
-                tagparent = nullid
-            self.repo.rawcommit([".hgtags"], "update tags", "convert-repo",
-                                date, tagparent, nullid, extra=extra)
-            return hex(self.repo.changelog.tip())
+         newlines = [("%s %s\n" % (tags[tag], tag)) for tag in tags.keys()]
+         newlines.sort()
+
+         if newlines == oldlines:
+             return None
+         data = "".join(newlines)
+
+         def getfilectx(repo, memctx, f):
+            return context.memfilectx(f, data, False, False, None)
+
+         self.ui.status("updating tags\n")
+         date = "%s 0" % int(time.mktime(time.gmtime()))
+         extra = {'branch': self.tagsbranch}
+         ctx = context.memctx(self.repo, (tagparent, None), "update tags",
+                              [".hgtags"], getfilectx, "convert-repo", date,
+                              extra)
+         self.repo.commitctx(ctx)
+         return hex(self.repo.changelog.tip())
 
     def setfilemapmode(self, active):
         self.filemapmode = active
--- a/tests/test-convert-cvs	Thu Jun 19 00:14:24 2008 +0200
+++ b/tests/test-convert-cvs	Thu Jun 19 00:14:24 2008 +0200
@@ -7,6 +7,11 @@
     cvs -f $@
 }
 
+hgcat()
+{
+    hg --cwd src-hg cat -r tip "$1"
+}
+
 echo "[extensions]" >> $HGRCPATH
 echo "convert = " >> $HGRCPATH
 
@@ -45,13 +50,13 @@
 
 echo % convert fresh repo
 hg convert src src-hg | sed -e 's/connecting to.*cvsrepo/connecting to cvsrepo/g'
-cat src-hg/a
-cat src-hg/b/c
+hgcat a
+hgcat b/c
 
 echo % convert fresh repo with --filemap
 echo include b/c > filemap
 hg convert --filemap filemap src src-filemap | sed -e 's/connecting to.*cvsrepo/connecting to cvsrepo/g'
-cat src-hg/b/c
+hgcat b/c
 hg -R src-filemap log --template '#rev# #desc# files: #files#\n'
 
 echo % commit new file revisions
@@ -64,12 +69,12 @@
 
 echo % convert again
 hg convert src src-hg | sed -e 's/connecting to.*cvsrepo/connecting to cvsrepo/g'
-cat src-hg/a
-cat src-hg/b/c
+hgcat a
+hgcat b/c
 
 echo % convert again with --filemap
 hg convert --filemap filemap src src-filemap | sed -e 's/connecting to.*cvsrepo/connecting to cvsrepo/g'
-cat src-hg/b/c
+hgcat b/c
 hg -R src-filemap log --template '#rev# #desc# files: #files#\n'
 
 echo % commit branch
@@ -84,12 +89,12 @@
 
 echo % convert again
 hg convert src src-hg | sed -e 's/connecting to.*cvsrepo/connecting to cvsrepo/g'
-cat src-hg/a
-cat src-hg/b/c
+hgcat a
+hgcat b/c
 
 echo % convert again with --filemap
 hg convert --filemap filemap src src-filemap | sed -e 's/connecting to.*cvsrepo/connecting to cvsrepo/g'
-cat src-hg/b/c
+hgcat b/c
 hg -R src-filemap log --template '#rev# #desc# files: #files#\n'
 
 echo "graphlog = " >> $HGRCPATH
--- a/tests/test-convert-cvs-builtincvsps	Thu Jun 19 00:14:24 2008 +0200
+++ b/tests/test-convert-cvs-builtincvsps	Thu Jun 19 00:14:24 2008 +0200
@@ -7,6 +7,11 @@
     cvs -f "$@"
 }
 
+hgcat()
+{
+    hg --cwd src-hg cat -r tip "$1"
+}
+
 echo "[extensions]" >> $HGRCPATH
 echo "convert = " >> $HGRCPATH
 echo "graphlog = " >> $HGRCPATH
@@ -48,13 +53,13 @@
 
 echo % convert fresh repo
 hg convert src src-hg | sed -e 's/connecting to.*cvsrepo/connecting to cvsrepo/g'
-cat src-hg/a
-cat src-hg/b/c
+hgcat a
+hgcat b/c
 
 echo % convert fresh repo with --filemap
 echo include b/c > filemap
 hg convert --filemap filemap src src-filemap | sed -e 's/connecting to.*cvsrepo/connecting to cvsrepo/g'
-cat src-hg/b/c
+hgcat b/c
 hg -R src-filemap log --template '#rev# #desc# files: #files#\n'
 
 echo % commit new file revisions
@@ -67,12 +72,12 @@
 
 echo % convert again
 hg convert src src-hg | sed -e 's/connecting to.*cvsrepo/connecting to cvsrepo/g'
-cat src-hg/a
-cat src-hg/b/c
+hgcat a
+hgcat b/c
 
 echo % convert again with --filemap
 hg convert --filemap filemap src src-filemap | sed -e 's/connecting to.*cvsrepo/connecting to cvsrepo/g'
-cat src-hg/b/c
+hgcat b/c
 hg -R src-filemap log --template '#rev# #desc# files: #files#\n'
 
 echo % commit branch
@@ -87,12 +92,12 @@
 
 echo % convert again
 hg convert src src-hg | sed -e 's/connecting to.*cvsrepo/connecting to cvsrepo/g'
-cat src-hg/a
-cat src-hg/b/c
+hgcat a
+hgcat b/c
 
 echo % convert again with --filemap
 hg convert --filemap filemap src src-filemap | sed -e 's/connecting to.*cvsrepo/connecting to cvsrepo/g'
-cat src-hg/b/c
+hgcat b/c
 hg -R src-filemap log --template '#rev# #desc# files: #files#\n'
 
 echo "graphlog = " >> $HGRCPATH
--- a/tests/test-convert-cvs-builtincvsps.out	Thu Jun 19 00:14:24 2008 +0200
+++ b/tests/test-convert-cvs-builtincvsps.out	Thu Jun 19 00:14:24 2008 +0200
@@ -104,7 +104,6 @@
 converting...
 0 ci2
 a
-a
 c
 d
 % convert again with --filemap
--- a/tests/test-convert-cvs.out	Thu Jun 19 00:14:24 2008 +0200
+++ b/tests/test-convert-cvs.out	Thu Jun 19 00:14:24 2008 +0200
@@ -79,7 +79,6 @@
 converting...
 0 ci2
 a
-a
 c
 d
 % convert again with --filemap