]> ruderich.org/simon Gitweb - config/dotfiles.git/commitdiff
shell: git-evtag-compute-py: port to python 3
authorSimon Ruderich <simon@ruderich.org>
Sun, 4 Dec 2022 08:26:01 +0000 (09:26 +0100)
committerSimon Ruderich <simon@ruderich.org>
Sun, 4 Dec 2022 08:26:01 +0000 (09:26 +0100)
From https://github.com/cgwalters/git-evtag commit 2eaa213
(git-evtag-compute-py: Port to Python 3, 2021-05-05).

shell/bin/git-evtag-compute-py

index 0babc5c32a6e52e420ed1c98d75955f100a00b21..72e86b81584c2e346b1dd993c358216520e656c6 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 #
 # An implementation of Git-EVTag in a mixture of Python and
 # git-as-subprocess.  Slower than C, but easier to understand
@@ -41,17 +41,18 @@ def checksum_bytes(otype, buf):
     return blen
 
 def checksum_object(repo, objid):
+    assert objid is not None
     p = subprocess.Popen(['git', 'cat-file', '--batch'],
                          stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE,
                          close_fds=True,
                          cwd=repo)
-    p.stdin.write(objid + '\n')
+    p.stdin.write(objid.encode('ascii') + b'\n')
     p.stdin.close()
-    (objid,objtype,lenstr) = p.stdout.readline().split(None, 2)
+    (objid,objtype,lenstr) = p.stdout.readline().decode('ascii').split(None, 2)
     olen = int(lenstr)
     lenstr = lenstr.strip()
-    buf = "{0} {1}\000".format(objtype, lenstr)
+    buf = "{0} {1}\000".format(objtype, lenstr).encode('ascii')
     checksum_bytes(objtype, buf)
 
     stats[objtype] += 1
@@ -59,7 +60,7 @@ def checksum_object(repo, objid):
     if objtype == 'commit':
         buf = p.stdout.readline()
         olen -= checksum_bytes(objtype, buf)
-        (treestr, treeobjid) = buf.split(None, 1)
+        (treestr, treeobjid) = buf.decode('ascii').split(None, 1)
         treeobjid = treeobjid.strip()
         assert treestr == 'tree'
     else:
@@ -76,7 +77,7 @@ def checksum_object(repo, objid):
         raise subprocess.CalledProcessError(p.returncode, 'git cat-file')
     return treeobjid
 
-def checksum_tree(repo, objid):
+def checksum_tree(repo, path, objid):
     checksum_object(repo, objid)
     p = subprocess.Popen(['git', 'ls-tree', objid],
                          stdin=DEVNULL,
@@ -84,14 +85,14 @@ def checksum_tree(repo, objid):
                          close_fds=True,
                          cwd=repo)
     for line in p.stdout:
-        (mode, otype, subid, fname) = line.split(None, 3)
+        (mode, otype, subid, fname) = line.decode('ascii').split(None, 3)
         fname = fname.strip()
         if otype == 'blob':
             checksum_object(repo, subid)
         elif otype == 'tree':
-            checksum_tree(repo, subid)
+            checksum_tree(repo, os.path.join(path, fname), subid)
         elif otype == 'commit':
-            checksum_repo(os.path.join(repo, fname), subid)
+            checksum_repo(os.path.join(repo, path, fname), subid)
         else:
             assert False
     p.wait()
@@ -100,7 +101,7 @@ def checksum_tree(repo, objid):
 
 def checksum_repo(repo, objid):
     treeid = checksum_object(repo, objid)
-    checksum_tree(repo, treeid)
+    checksum_tree(repo, '.', treeid)
 
 checksum_repo('.', opts.rev)