3 bup_python="$(dirname "$0")/bup-python" || exit $?
4 exec "$bup_python" "$0" ${1+"$@"}
8 from __future__ import absolute_import
9 from binascii import hexlify
12 from bup import git, options
13 from bup.compat import argv_bytes
14 from bup.helpers import debug1, handle_ctrl_c, log
15 from bup.io import byte_stream, path_msg
17 # FIXME: review for safe writes.
23 bup tag [-f] <tag name> <commit>
24 bup tag [-f] -d <tag name>
26 d,delete= Delete a tag
27 f,force Overwrite existing tag, or ignore missing tag when deleting
30 o = options.Options(optspec)
31 (opt, flags, extra) = o.parse(sys.argv[1:])
33 git.check_repo_or_die()
35 tags = [t for sublist in git.tags().values() for t in sublist]
38 # git.delete_ref() doesn't complain if a ref doesn't exist. We
39 # could implement this verification but we'd need to read in the
40 # contents of the tag file and pass the hash, and we already know
41 # about the tag's existance via "tags".
42 tag_name = argv_bytes(opt.delete)
43 if not opt.force and tag_name not in tags:
44 log("error: tag '%s' doesn't exist\n" % path_msg(tag_name))
46 tag_file = b'refs/tags/%s' % tag_name
47 git.delete_ref(tag_file)
53 out = byte_stream(sys.stdout)
58 o.fatal('expected commit ref and hash')
60 tag_name, commit = map(argv_bytes, extra[:2])
62 o.fatal("tag name must not be empty.")
63 debug1("args: tag name = %s; commit = %s\n"
64 % (path_msg(tag_name), commit.decode('ascii')))
66 if tag_name in tags and not opt.force:
67 log("bup: error: tag '%s' already exists\n" % path_msg(tag_name))
70 if tag_name.startswith(b'.'):
71 o.fatal("'%s' is not a valid tag name." % path_msg(tag_name))
74 hash = git.rev_parse(commit)
75 except git.GitError as e:
76 log("bup: error: %s" % e)
80 log("bup: error: commit %s not found.\n" % commit.decode('ascii'))
83 pL = git.PackIdxList(git.repo(b'objects/pack'))
84 if not pL.exists(hash):
85 log("bup: error: commit %s not found.\n" % commit.decode('ascii'))
88 tag_file = git.repo(b'refs/tags/' + tag_name)
90 tag = open(tag_file, 'wb')
92 log("bup: error: could not create tag '%s': %s" % (path_msg(tag_name), e))
95 tag.write(hexlify(hash))