[PATCH 1/9] tag: use git.update_ref()

1 view
Skip to first unread message

Rob Browning

unread,
Jan 16, 2022, 3:51:58 PM1/16/22
to bup-...@googlegroups.com, Johannes Berg
From: Johannes Berg <joha...@sipsolutions.net>

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 <joha...@sipsolutions.net>
Reviewed-by: Rob Browning <r...@defaultvalue.org>
Tested-by: Rob Browning <r...@defaultvalue.org>
---
lib/bup/cmd/tag.py | 11 +----------
lib/bup/git.py | 20 +++++++++++++++-----
2 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/lib/bup/cmd/tag.py b/lib/bup/cmd/tag.py
index 3bb1b409..0da53372 100755
--- a/lib/bup/cmd/tag.py
+++ b/lib/bup/cmd/tag.py
@@ -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)
diff --git a/lib/bup/git.py b/lib/bup/git.py
index 270f1eb4..69134ac3 100644
--- a/lib/bup/git.py
+++ b/lib/bup/git.py
@@ -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)
--
2.30.2

Reply all
Reply to author
Forward
0 new messages