]> git.michaelhowe.org Git - packages/b/bup.git/commitdiff
'bup split' can now update a git ref if you give it the -n option.
authorAvery Pennarun <apenwarr@gmail.com>
Thu, 31 Dec 2009 23:29:34 +0000 (18:29 -0500)
committerAvery Pennarun <apenwarr@gmail.com>
Thu, 31 Dec 2009 23:29:34 +0000 (18:29 -0500)
cmd-split.py
git.py

index 345b896bd0d5ac8c0c97939ef6357cd442047b5e..5a2f9670ecde373b7a4326f269823a18e8babeea 100755 (executable)
@@ -79,6 +79,7 @@ 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
+n,name=    name of backup set to update (if any)
 bench      print benchmark timings to stderr
 """
 (opt, flags, extra) = options.Options('bup split', optspec).parse(sys.argv[1:])
@@ -95,10 +96,9 @@ for (ofs, size, sha) in hashsplit_iter(sys.stdin):
 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)
+    ref = opt.name and ('refs/heads/%s' % opt.name) or None
+    commit = git.gen_commit_easy(ref, tree, msg)
     print commit
 elif opt.tree:
     print tree
diff --git a/git.py b/git.py
index e5b6165808fded3aab61b54fcf58e6e3712eb288..d30dd0a19827338483bfab166106dfa0bb470807 100644 (file)
--- a/git.py
+++ b/git.py
@@ -1,4 +1,5 @@
-import os, errno, zlib, time, sha
+import os, errno, zlib, time, sha, subprocess
+from helpers import *
 
 
 def hash_raw(type, s):
@@ -43,6 +44,31 @@ def _git_date(date):
     return time.strftime('%s %z', time.localtime(date))
 
 
+def _gitenv(repo):
+    os.environ['GIT_DIR'] = os.path.abspath(repo)
+
+
+def _read_ref(repo, refname):
+    p = subprocess.Popen(['git', 'show-ref', '--', refname],
+                         preexec_fn = lambda: _gitenv(repo),
+                         stdout = subprocess.PIPE)
+    out = p.stdout.read().strip()
+    p.wait()
+    if out:
+        return out.split()[0]
+    else:
+        return None
+
+
+def _update_ref(repo, refname, newval, oldval):
+    if not oldval:
+        oldval = ''
+    p = subprocess.Popen(['git', 'update-ref', '--', refname, newval, oldval],
+                         preexec_fn = lambda: _gitenv(repo))
+    p.wait()
+    return newval
+
+
 def gen_commit(tree, parent, author, adate, committer, cdate, msg):
     l = []
     if tree: l.append('tree %s' % tree)
@@ -52,3 +78,13 @@ def gen_commit(tree, parent, author, adate, committer, cdate, msg):
     l.append('')
     l.append(msg)
     return hash_raw('commit', '\n'.join(l))
+
+
+def gen_commit_easy(ref, tree, msg):
+    now = time.time()
+    userline = '%s <%s@%s>' % (userfullname(), username(), hostname())
+    oldref = ref and _read_ref('.git', ref) or None
+    commit = gen_commit(tree, oldref, userline, now, userline, now, msg)
+    if ref:
+        _update_ref('.git', ref, commit, oldref)
+    return commit