]> thyrsus.com Git - src.git/commitdiff
Refactor.
authorEric S. Raymond <esr@thyrsus.com>
Fri, 7 Nov 2014 01:13:08 +0000 (20:13 -0500)
committerEric S. Raymond <esr@thyrsus.com>
Fri, 7 Nov 2014 01:13:08 +0000 (20:13 -0500)
src

diff --git a/src b/src
index 04bd99a16ffb784f2c2218af56193216d8ee2214..01739756c9fa0da9495794a61c8697d0dfd338a7 100755 (executable)
--- a/src
+++ b/src
@@ -85,35 +85,46 @@ class RCSRevisionItem:
         self.log = ""
         self.date = None
 
+class RCSLog:
+    "Encapsulate a revision list and some methods on it"
+    def __init__(self, name):
+        self.name = name
+        self.revlist = []
+        with popen_or_die("cd .src >/dev/null; rlog %s; cd ..>/dev/null" % name) as fp:
+            self.revlist = []
+            maxrevno = 0
+            state = "initial"
+            for line in fp:
+                if line.startswith("----------------------------"):
+                    state = "logheader"
+                    self.revlist.append(RCSRevisionItem(maxrevno))
+                    maxrevno += 1
+                elif state == "logheader" and line.startswith("revision"):
+                    fields = line.split()
+                    self.revlist[-1].revision = fields[1]
+                elif state == "logheader" and line.startswith("date"):
+                    fields = line.split()
+                    date = fields[1] + " " + fields[2]
+                    if date.endswith(";"):
+                        date = date[:-1]
+                    date = date.replace("/", "-").replace(" ", "T") + "Z"
+                    self.revlist[-1].date = date
+                elif line.startswith("======================="):
+                    if not self.revlist[-1].log.endswith("\n"):
+                        self.revlist[-1].log += "\n"
+                    break
+                elif self.revlist:
+                    self.revlist[-1].log += line[1:]
+            for item in self.revlist:
+                item.revno = maxrevno - item.revno
+    def __len__(self):
+        return len(self.revlist)
+    def __getitem__(self, i):
+        "Use 1-origin indices, lowest oldest."
+        return self.revlist[len(self.revlist) - i]
+
 def log_capture(fn):
     "Capture and parse the metadata for a specified file."
-    with popen_or_die("cd .src >/dev/null; rlog %s; cd ..>/dev/null" % fn) as fp:
-        revlist = []
-        maxrevno = 0
-        state = "initial"
-        for line in fp:
-            if line.startswith("----------------------------"):
-                state = "logheader"
-                revlist.append(RCSRevisionItem(maxrevno))
-                maxrevno += 1
-            elif state == "logheader" and line.startswith("revision"):
-                fields = line.split()
-                revlist[-1].revision = fields[1]
-            elif state == "logheader" and line.startswith("date"):
-                fields = line.split()
-                date = fields[1] + " " + fields[2]
-                if date.endswith(";"):
-                    date = date[:-1]
-                date = date.replace("/", "-").replace(" ", "T") + "Z"
-                revlist[-1].date = date
-            elif line.startswith("======================="):
-                if not revlist[-1].log.endswith("\n"):
-                    revlist[-1].log += "\n"
-                break
-            elif revlist:
-                revlist[-1].log += line[1:]
-        for item in revlist:
-            item.revno = maxrevno - item.revno
     return revlist
 
 def help_method(*args):
@@ -224,8 +235,7 @@ def commit_method(*args):
            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))
-           revlist = log_capture(arg)
-           announce("%s -> %d" % (arg, len(revlist))) 
+           announce("%s -> %d" % (arg, len(RCSLog(arg)))) 
 
 def list_method(*args):
     "Generate a summary listing of commits, one line per commit."
@@ -233,7 +243,7 @@ def list_method(*args):
     width = screenwidth()
     for arg in args:
         sys.stdout.write("= %s %s\n" % (arg, ((width - len(arg) - 5) * "=")))
-        for item in log_capture(arg):
+        for item in RCSLog(arg):
             if not revspec or item.revno in revspec:
                 # Must allow enough room for revno and date
                 summary = item.log.strip()[:width - 32]
@@ -245,7 +255,7 @@ def log_method(*args):
     width = screenwidth()
     for arg in args:
         sys.stdout.write("= %s %s\n" % (arg, ((width - len(arg) - 5) * "=")))
-        for item in log_capture(arg):
+        for item in RCSLog(arg):
             if not revspec or item.revno in revspec:
                 sys.stdout.write("%-4d | %s\n%s" % (item.revno, item.date, item.log))
                 sys.stdout.write(("-" * (width - 1)) + "\n")
@@ -256,7 +266,7 @@ def checkout_method(*args):
     if revspec and len(revspec) > 1:
         croak("checkout needs an empty or singleton revision spec")
     for arg in args:
-        revlist = log_capture(arg)
+        revlist = RCSLog(arg)
         if revspec is None:
             revspec = [len(revlist)]
             revision = ""
@@ -273,7 +283,7 @@ def cat_method(*args):
     "Dump revision content to standard output."
     (revspec, args) = preamble("cat", args)
     for arg in args:
-        revlist = log_capture(arg)
+        revlist = RCSLog(arg)
         if revspec is None:
             revspec = range(1, len(revlist)+1)
         do_or_die("mv .src/{0},v .".format(arg))
@@ -289,7 +299,7 @@ def diff_method(*args):
     while args and args[0].startswith("-"):
         diffopts += " " + args.pop(0)
     for arg in args:
-        revlist = log_capture(arg)
+        revlist = RCSLog(arg)
         do_or_die("mv .src/{0},v .".format(arg))
         if revspec is None:
             # The || : suppresses the error return from diff(1)