]> thyrsus.com Git - rcs-fast-import.git/commitdiff
Refactoring step.
authorEric S. Raymond <esr@thyrsus.com>
Thu, 11 Nov 2010 03:24:57 +0000 (22:24 -0500)
committerEric S. Raymond <esr@thyrsus.com>
Thu, 11 Nov 2010 03:24:57 +0000 (22:24 -0500)
rcs-fast-import

index 446caba4db5a6a85d4ad71209f7c89975263aa1b..8127b6d213fc52523cd28594be8e6adb98c72332 100755 (executable)
@@ -236,6 +236,10 @@ class Repository:
         self.import_line = 0
         self.inline_count = 0
         self.basedir = os.getcwd()
+    def __enter__(self):        
+        return self
+    def __exit__(self, type, value, traceback):
+        self.cleanup()
     def cleanup(self):
         nuke(self.subdir(), "rcs-fast-import: cleaning up %s" % self.subdir())
     def subdir(self, name=None):
@@ -609,57 +613,54 @@ if __name__ == '__main__':
        elif (switch == '-V'):
            print "rcs-fast-import version %s" % version
 
-    repo = Repository()
-    repo.fast_import(sys.stdin)
     vcs = RCS()
-
     try:
-        for event in repo.events:
-            if isinstance(event, Blob) or isinstance(event, Reset) or isinstance(event, Passthrough):
-                continue
-            elif isinstance(event, Tag):
-                if verbose:
-                    announce("Tagging %s with %s" % (event.committish, event.name))
-                vcs.tag(event)
-            elif isinstance(event, Commit):
-                committables = []
-                for op in event.fileops:
-                    if op.op == "M":
-                        if verbose:
-                            announce("%5s M %s" % (event.mark, op.path))
-                        committables.append((op.path, op.mode, op.copyname))
-                    elif op.op in ("D", "deleteall"):
-                        # If we delete masters, we lose ability to
-                        # retrieve old versions.
-                        if do_deletes:
+        with Repository() as repo:
+            repo.fast_import(sys.stdin)
+            for event in repo.events:
+                if isinstance(event, Blob) or isinstance(event, Reset) or isinstance(event, Passthrough):
+                    continue
+                elif isinstance(event, Tag):
+                    if verbose:
+                        announce("Tagging %s with %s" % (event.committish, event.name))
+                    vcs.tag(event)
+                elif isinstance(event, Commit):
+                    committables = []
+                    for op in event.fileops:
+                        if op.op == "M":
+                            if verbose:
+                                announce("%5s M %s" % (event.mark, op.path))
+                            committables.append((op.path, op.mode, op.copyname))
+                        elif op.op in ("D", "deleteall"):
+                            # If we delete masters, we lose ability to
+                            # retrieve old versions.
+                            if do_deletes:
+                                if verbose:
+                                    announce("%5s D %s" % (event.mark, op.path))
+                                if not dryrun:
+                                    vcs.remove(op.path)
+                            elif verbose:
+                                announce("%5s D %s ignored" % (event.mark, op.path))
+                        elif op.op == "R":
                             if verbose:
-                                announce("%5s D %s" % (event.mark, op.path))
+                                announce("%5s R '%s' '%s'" % (event.mark, op.source, op.target))
                             if not dryrun:
-                                vcs.remove(op.path)
-                        elif verbose:
-                            announce("%5s D %s ignored" % (event.mark, op.path))
-                    elif op.op == "R":
-                        if verbose:
-                            announce("%5s R '%s' '%s'" % (event.mark, op.source, op.target))
-                        if not dryrun:
-                            vcs.rename(op.source, op.target)
-                    elif op.op == "C":
-                        if verbose:
-                            announce("%5s C '%s' '%s'" % (event.mark, op.source, op.target))
-                        if not dryrun:
-                            vcs.copy(ops.source, op.target)
-                    else:
-                        raise FatalException("unknown op type %s" % op.op)
-                vcs.commit(event, committables)
-        vcs.checkout(checkout_mode)
+                                vcs.rename(op.source, op.target)
+                        elif op.op == "C":
+                            if verbose:
+                                announce("%5s C '%s' '%s'" % (event.mark, op.source, op.target))
+                            if not dryrun:
+                                vcs.copy(ops.source, op.target)
+                        else:
+                            raise FatalException("unknown op type %s" % op.op)
+                    vcs.commit(event, committables)
+            vcs.checkout(checkout_mode)
         raise SystemExit, 0
     except FatalException, e:
         complain(e.msg)
-        repo.cleanup()
         raise SystemExit, 1
     except OSError, e:
         complain(e)
-        repo.cleanup()
         raise SystemExit, 1
 
 # end