]> arthur.barton.de Git - bup.git/commitdiff
tag: use git.update_ref()
authorJohannes Berg <johannes@sipsolutions.net>
Thu, 30 Dec 2021 20:31:55 +0000 (21:31 +0100)
committerRob Browning <rlb@defaultvalue.org>
Sun, 16 Jan 2022 20:08:00 +0000 (14:08 -0600)
Even tag refs can be packed by git, so writing to a file
might cause issues. Call git update-ref for tags as well.

In order to do that properly (and allow 'bup tag' to keep
its ability to modify tags), change git.update_ref() to
take a 'force' argument that allows updating a ref even
if it already exists with an old value we don't know.

Signed-off-by: Johannes Berg <johannes@sipsolutions.net>
Reviewed-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
lib/bup/cmd/tag.py
lib/bup/git.py

index 3bb1b409a6a05305762a647157fabbdabdeedd49..0da53372141eb6b385420565606edfbddafac29a 100755 (executable)
@@ -1,6 +1,5 @@
 
 from __future__ import absolute_import
-from binascii import hexlify
 import sys
 
 from bup import git, options
@@ -79,12 +78,4 @@ def main(argv):
             log("bup: error: commit %s not found.\n" % commit.decode('ascii'))
             sys.exit(2)
 
-    tag_file = git.repo(b'refs/tags/' + tag_name)
-    try:
-        tag = open(tag_file, 'wb')
-    except OSError as e:
-        log("bup: error: could not create tag '%s': %s" % (path_msg(tag_name), e))
-        sys.exit(3)
-    with tag as tag:
-        tag.write(hexlify(hash))
-        tag.write(b'\n')
+    git.update_ref(b'refs/tags/' + tag_name, hash, None, force=True)
index 270f1eb41e5fbf01e4efdc0d7d47f296f420f4be..69134ac33ae4ba0df4eb6bb691b51bbf6e7572e7 100644 (file)
@@ -1156,14 +1156,24 @@ def rev_parse(committish, repo_dir=None):
     return None
 
 
-def update_ref(refname, newval, oldval, repo_dir=None):
-    """Update a repository reference."""
-    if not oldval:
-        oldval = b''
+def update_ref(refname, newval, oldval, repo_dir=None, force=False):
+    """Update a repository reference.
+
+    With force=True, don't care about the previous ref (oldval);
+    with force=False oldval must be either a sha1 or None (for an
+    entirely new branch)
+    """
+    if force:
+        assert oldval is None
+        oldarg = []
+    elif not oldval:
+        oldarg = [b'']
+    else:
+        oldarg = [hexlify(oldval)]
     assert refname.startswith(b'refs/heads/') \
         or refname.startswith(b'refs/tags/')
     p = subprocess.Popen([b'git', b'update-ref', refname,
-                          hexlify(newval), hexlify(oldval)],
+                          hexlify(newval)] + oldarg,
                          env=_gitenv(repo_dir),
                          close_fds=True)
     _git_wait(b'git update-ref', p)