]> arthur.barton.de Git - bup.git/commitdiff
'bup split' now has a -c option to generate a git commit object.
authorAvery Pennarun <apenwarr@gmail.com>
Thu, 31 Dec 2009 22:55:14 +0000 (17:55 -0500)
committerAvery Pennarun <apenwarr@gmail.com>
Thu, 31 Dec 2009 22:55:14 +0000 (17:55 -0500)
There's no way to set its parent yet, but at least this is all you need if
you want to repack.

cmd-split.py
git.py
helpers.py

index 04b6d7875a2be614999a2e969c758930f1c329a4..345b896bd0d5ac8c0c97939ef6357cd442047b5e 100755 (executable)
@@ -78,6 +78,8 @@ optspec = """
 bup split [-t] <filename
 --
 t,tree     output a tree instead of a series of blobs
+c,commit   output a commit instead of a tree or blobs
+bench      print benchmark timings to stderr
 """
 (opt, flags, extra) = options.Options('bup split', optspec).parse(sys.argv[1:])
 
@@ -87,12 +89,21 @@ shalist = []
 ofs = 0
 for (ofs, size, sha) in hashsplit_iter(sys.stdin):
     #log('SPLIT @ %-8d size=%-8d\n' % (ofs, size))
-    if not opt.tree:
+    if not opt.tree and not opt.commit:
         print sha
     shalist.append(('100644', '%016x.bupchunk' % ofs, sha))
-if opt.tree:
-    print git.gen_tree(shalist)
+if opt.tree or opt.commit:
+    tree = git.gen_tree(shalist)
+if opt.commit:
+    now = time.time()
+    userline = '%s <%s@%s>' % (userfullname(), username(), hostname())
+    msg = 'Generated by command:\n%r' % sys.argv
+    commit = git.gen_commit(tree, None, userline, now, userline, now, msg)
+    print commit
+elif opt.tree:
+    print tree
 
 secs = time.time() - start_time
-log('\n%.2fkbytes in %.2f secs = %.2f kbytes/sec\n'
-    % (ofs/1024., secs, ofs/1024./secs))
+if opt.bench:
+    log('\nbup: %.2fkbytes in %.2f secs = %.2f kbytes/sec\n'
+        % (ofs/1024., secs, ofs/1024./secs))
diff --git a/git.py b/git.py
index 808d1e8f2f1519dbceaa3a57d68a7fd7d838fbc7..e5b6165808fded3aab61b54fcf58e6e3712eb288 100644 (file)
--- a/git.py
+++ b/git.py
@@ -1,10 +1,9 @@
-import os, errno, zlib
-from sha import sha
+import os, errno, zlib, time, sha
 
 
 def hash_raw(type, s):
     header = '%s %d\0' % (type, len(s))
-    sum = sha(header)
+    sum = sha.sha(header)
     sum.update(s)
     hex = sum.hexdigest()
     dir = '.git/objects/%s' % hex[0:2]
@@ -38,3 +37,18 @@ def gen_tree(shalist):
     l = ['%s %s\0%s' % (mode,name,hex.decode('hex')) 
          for (mode,name,hex) in shalist]
     return hash_raw('tree', ''.join(l))
+
+
+def _git_date(date):
+    return time.strftime('%s %z', time.localtime(date))
+
+
+def gen_commit(tree, parent, author, adate, committer, cdate, msg):
+    l = []
+    if tree: l.append('tree %s' % tree)
+    if parent: l.append('parent %s' % parent)
+    if author: l.append('author %s %s' % (author, _git_date(adate)))
+    if committer: l.append('committer %s %s' % (committer, _git_date(cdate)))
+    l.append('')
+    l.append(msg)
+    return hash_raw('commit', '\n'.join(l))
index 7c8f0114ec0265e865014d7023ff3b20b0b3fbe0..7e67b7ac05223527bc1032530d4668039fda8b80 100644 (file)
@@ -1,7 +1,47 @@
-import sys
+import sys, os, pwd, subprocess
 
 
 def log(s):
     sys.stderr.write(s)
 
 
+def readpipe(argv):
+    p = subprocess.Popen(argv, stdout=subprocess.PIPE)
+    r = p.stdout.read()
+    p.wait()
+    return r
+
+
+_username = None
+def username():
+    global _username
+    if not _username:
+        uid = os.getuid()
+        try:
+            _username = pwd.getpwuid(uid)[0]
+        except KeyError:
+            _username = 'user%d' % uid
+    return _username
+
+
+_userfullname = None
+def userfullname():
+    global _userfullname
+    if not _userfullname:
+        uid = os.getuid()
+        try:
+            _userfullname = pwd.getpwuid(uid)[4].split(',')[0]
+        except KeyError:
+            _userfullname = 'user%d' % uid
+    return _userfullname
+
+
+_hostname = None
+def hostname():
+    global _hostname
+    if not _hostname:
+        try:
+            _hostname = readpipe(['hostname', '-f']).strip()
+        except OSError:
+            pass
+    return _hostname or 'localhost'