From: Johannes Berg Date: Thu, 30 Dec 2021 20:31:55 +0000 (+0100) Subject: tag: use git.update_ref() X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=bup.git;a=commitdiff_plain;h=84cb94e1feb584ae7c67d65c27b2ff1a8e01e489 tag: use git.update_ref() 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 Reviewed-by: Rob Browning Tested-by: Rob Browning --- diff --git a/lib/bup/cmd/tag.py b/lib/bup/cmd/tag.py index 3bb1b40..0da5337 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 270f1eb..69134ac 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)