]> thyrsus.com Git - rcs-fast-import.git/commitdiff
Refactoring almost works - need better way to create null file for delete.
authorEric S. Raymond <esr@thyrsus.com>
Sat, 13 Nov 2010 14:43:27 +0000 (09:43 -0500)
committerEric S. Raymond <esr@thyrsus.com>
Sat, 13 Nov 2010 14:43:27 +0000 (09:43 -0500)
rcs-fast-import

index 5e7b9f9036156b8ded93784349c84b5d95aa358a..68343f76ea10b5425c36cd9069aa8277271eaba4 100755 (executable)
@@ -555,6 +555,17 @@ def do_or_die(cmd, legend=""):
     except OSError, e:
         raise FatalException("execution of %s%s failed: %s" % (cmd, legend, e))
 
+def loud_remove(path):
+    if verbose >= DEBUG_SHUFFLE:
+        announce("removing %s" % (path,))
+    if not dryrun:
+        os.remove(path)
+
+def loud_rename(source, target):
+    if verbose >= DEBUG_SHUFFLE:
+        announce("renaming %s to %s" % (source, target))
+    if not dryrun:
+        os.rename(source, target)
 #
 # All VCS-specific code is beyond this point.  At the moment there's
 # just one VCS class, for RCS, but it would be easy to write another
@@ -609,6 +620,34 @@ class RCS:
                 fp.write(RCS.gethdr(path, "head\t") + " ")
                 fp.write(path)
             fp.write("\n")
+    @staticmethod
+    def instantiate(path, copyfile):
+        rcsdir = os.path.join(os.path.dirname(path), "RCS")
+        if not os.path.exists(rcsdir):
+            try:
+                os.makedirs(rcsdir)
+            except OSError:
+                raise FatalException("can't create %s." % rcsdir)
+        if verbose >= DEBUG_SHUFFLE:
+            announce("linking %s to %s" % (os.path.relpath(copyfile), path))
+        if not dryrun:
+            try:
+                os.link(copyfile, path)
+            except OSError, e:
+                raise FatalException("refusing to step on %s: %s." % (path, `e`))
+    def normal_checkin(self, commit, path, legend, state=None):
+        if self.roundtrip:
+            comment = event.email_out()
+        else:
+            comment = event.comment
+        command = "ci -l"
+        if state:
+            command += " -s" + state
+        command += " -d'%s'" % event.committer.date.rfc822()
+        command += ' -m"%s"' % comment.replace('"', '\\"')
+        command += " " + path + " " + RCS.master(path)
+        do_or_die(command, legend)
+        loud_remove(path)
     def precommit(self, event):
         if verbose:
             RCS.record("commit")
@@ -617,76 +656,38 @@ class RCS:
         if mode == "160000":
             raise FatalException("ignoring a submodule link %s", path)
         else:
-            rcsdir = os.path.join(os.path.dirname(path), "RCS")
-            if not os.path.exists(rcsdir):
-                try:
-                    os.makedirs(rcsdir)
-                except OSError:
-                    raise FatalException("can't create %s." % rcsdir)
-            if verbose >= DEBUG_SHUFFLE:
-                announce("linking %s to %s" % (os.path.relpath(copyfile), path))
-            if not dryrun:
-                try:
-                    os.link(copyfile, path)
-                except OSError:
-                    raise FatalException("refusing to step on %s." % path)
-            if self.roundtrip:
-                comment = event.email_out()
-            else:
-                comment = event.comment
-            command = "ci -l"
-            command += " -d'%s'" % event.committer.date.rfc822()
-            command += ' -m"%s"' % comment.replace('"', '\\"')
-            command += " " + path + " " + RCS.master(path)
-            do_or_die(command, "checkin")
+            RCS.instantiate(path, copyfile)
+            self.normal_checkin(event, path, "modify")
             RCS.record("M", path)
-            if verbose >= DEBUG_SHUFFLE:
-                announce("removing %s" % (path,))
-            if not dryrun:
-                os.remove(path)
-    def postcommit(self, event):
-        # Save annotated tags for round-tripping
-        for tag in event.taglist:
-            with file("ANNOTATED-TAGS", "w") as fp:
-                fp.write(tag.email_out())
-            do_or_die("ci -l -m'Annotated tag %s' -d'%s' ANNOTATED-TAGS,v"
-                      % (tag.name, tag.tagger.date.rfc822()))
-            os.remove("ANNOTATED-TAGS")
-            do_or_die("rcs -n'%s': %s" % (tag.name, " ".join(RCS.rcsfiles())))
-        if event.is_tip():
-            do_or_die("rcs -n'%s': %s" % (event.branch, " ".join(RCS.rcsfiles())))
-    def delete(self, commit, path, legend="Delete"):
+    def delete(self, commit, path, legend="delete"):
         "Mark a path deleted, but don't actually remove the master."
-        file(path, "w").close()
-        command = "ci -l"
-        command += " -s" + legend
-        command += " -d'%s'" % event.committer.date.rfc822()
-        command += ' -m"%s of %s"' % (legend, path)
-        command += " " + path + " " + RCS.master(path)
-        do_or_die(command, legend.lower())
+        RCS.instantiate(path, "/dev/null")
+        self.normal_checkin(commit, path, legend="delete", state="Deleted")
         RCS.record("D", path)
-        os.remove(path)
     def copy(self, commit, source, target, legend="Copy"):
         "Perform RCS copies. Start the copy with no history."
-        uphere = os.path.basename(target)
+        if os.path.exists(RCS.master(target)):
+            raise FatalException("copy to existing file")
         do_or_die("co %s" % RCS.master(source), legend)
-        os.rename(source, uphere)
-        if self.roundtrip:
-            comment = event.email_out()
-        else:
-            comment = event.comment
-        command = "ci -l"
-        command += " -d'%s'" % event.committer.date.rfc822()
-        command += ' -m"%s"' % comment.replace('"', '\\"')
-        command += " " + uphere + " " + RCS.master(uphere)
-        do_or_die(command, legend.lower())
+        loud_rename(source, target)
+        self.normal_checkin(commit, target, legend="copy")
         RCS.record("C", source)
         RCS.record("T", target)
-        os.remove(uphere)
     def rename(self, commit, source, target):
         "Perform RCS renames, carrying history with the rename."
         self.copy(commit, source, target, legend="Rename")
         self.delete(commit, source, legend="Rename")
+    def postcommit(self, event):
+        # Save annotated tags for round-tripping
+        for tag in event.taglist:
+            with file("ANNOTATED-TAGS", "w") as fp:
+                fp.write(tag.email_out())
+            do_or_die("ci -l -m'Annotated tag %s' -d'%s' ANNOTATED-TAGS,v"
+                      % (tag.name, tag.tagger.date.rfc822()))
+            os.remove("ANNOTATED-TAGS")
+            do_or_die("rcs -n'%s': %s" % (tag.name, " ".join(RCS.rcsfiles())))
+        if event.is_tip():
+            do_or_die("rcs -n'%s': %s" % (event.branch, " ".join(RCS.rcsfiles())))
     def checkout(self, switch):
         "Post-conversion actions."
         if switch != '-l':