]> thyrsus.com Git - src.git/commitdiff
A bit more robust against errors.
authorEric S. Raymond <esr@thyrsus.com>
Fri, 7 Nov 2014 02:30:44 +0000 (21:30 -0500)
committerEric S. Raymond <esr@thyrsus.com>
Fri, 7 Nov 2014 02:30:44 +0000 (21:30 -0500)
src

diff --git a/src b/src
index 4227db3f076ff7b7c6555696b8ccc91ccdb70ac9..8d25094a9a1b48789635cd2c5dd7b73ee5a48160 100755 (executable)
--- a/src
+++ b/src
@@ -260,6 +260,18 @@ def add_method(*args):
            do_or_die("rcs -q -kb -i {0} </dev/null && mv {0},v .src".format(arg))
            announce("%s: 0" % arg) 
 
+class lifted:
+    "Temporatily lift a master to the working directory."
+    def __init__(self, name):
+        self.name = name
+    def __enter__(self):
+        do_or_die("mv .src/{0},v .".format(self.name))
+    def __exit__(self, extype, value, traceback_unused):
+        do_or_die("mv {0},v .src".format(self.name))
+        if extype:
+            raise extype(value)
+        return True
+
 def commit_method(*args):
     "Commit changes to files."
     (revspec, args) = preamble("commit", args)
@@ -276,7 +288,8 @@ def commit_method(*args):
            do_or_die(editor + " " + scratchfile)
            logcomment = open(scratchfile).read()
            logcomment = "." + logcomment.replace("\n", "\n.")[:-1]
-           do_or_die("mv .src/{0},v . && ci -l -m'{1}' {0} && mv {0},v .src".format(arg, logcomment))
+           with lifted(arg):
+               do_or_die("ci -l -m'{1}' {0}".format(arg, logcomment))
            announce("%s -> %d" % (arg, len(RCSLog(arg)))) 
 
 def list_method(*args):
@@ -323,7 +336,8 @@ def checkout_method(*args):
             croak("%s has only %d revisions" % (arg, len(revlist)))
         else:
             revision = revlist[len(revlist) - revspec[0]].revision
-        do_or_die("mv .src/{0},v . && rm -f {0} && rcs -q -u {0},v && co -q -l{1} {0} ; mv {0},v .src".format(arg, revision))
+        with lifted(arg):
+            do_or_die("rm -f {0} && rcs -q -u {0},v && co -q -l{1} {0}".format(arg, revision))
         announce("%s <- %d" % (arg, revspec[0])) 
 
 def cat_method(*args):
@@ -333,11 +347,10 @@ def cat_method(*args):
         revlist = RCSLog(arg)
         if revspec is None:
             revspec = range(1, len(revlist)+1)
-        do_or_die("mv .src/{0},v .".format(arg))
-        for item in revlist:
-            if item.revno in revspec:
-                do_or_die("co -q -p -r{1} {0}".format(arg, item.revision), loud=True)
-        do_or_die("mv {0},v .src".format(arg))
+        with lifted(arg):
+            for item in revlist:
+                if item.revno in revspec:
+                    do_or_die("co -q -p -r{1} {0}".format(arg, item.revision), loud=True)
 
 def diff_method(*args):
     "Dump diffs between revisions to standard output."
@@ -346,16 +359,15 @@ def diff_method(*args):
     while args and args[0].startswith("-"):
         diffopts += " " + args.pop(0)
     for arg in args:
-        do_or_die("mv .src/{0},v .".format(arg))
         diffspec = ""
         if revspec is not None:
             state = RCSLog(arg)
             diffspec += "-r" + state.revno_to_rcs(revspec[0])
             if len(revspec) > 1:
                 diffspec += " -r" + state.revno_to_rcs(revspec[1])
-        # The || : suppresses the error return from diff(1)
-        do_or_die("rcsdiff -q {0} {1} {2} || :".format(diffspec, diffopts, arg),loud=True)
-        do_or_die("mv {0},v .src".format(arg))
+        with lifted(arg):
+            # The || : suppresses the error return from diff(1)
+            do_or_die("rcsdiff -q {0} {1} {2} || :".format(diffspec, diffopts, arg),loud=True)
 
 dispatch = {
     "help":     help_method,