2 from __future__ import absolute_import
3 from binascii import hexlify
6 from bup import git, options
7 from bup.compat import argv_bytes
8 from bup.helpers import debug1, log
9 from bup.io import byte_stream, path_msg
12 # FIXME: review for safe writes.
16 bup tag [-f] <tag name> <commit>
17 bup tag [-f] -d <tag name>
19 d,delete= Delete a tag
20 f,force Overwrite existing tag, or ignore missing tag when deleting
24 o = options.Options(optspec)
25 opt, flags, extra = o.parse_bytes(argv[1:])
27 git.check_repo_or_die()
29 tags = [t for sublist in git.tags().values() for t in sublist]
32 # git.delete_ref() doesn't complain if a ref doesn't exist. We
33 # could implement this verification but we'd need to read in the
34 # contents of the tag file and pass the hash, and we already know
35 # about the tag's existance via "tags".
36 tag_name = argv_bytes(opt.delete)
37 if not opt.force and tag_name not in tags:
38 log("error: tag '%s' doesn't exist\n" % path_msg(tag_name))
40 tag_file = b'refs/tags/%s' % tag_name
41 git.delete_ref(tag_file)
47 out = byte_stream(sys.stdout)
52 o.fatal('expected commit ref and hash')
54 tag_name, commit = map(argv_bytes, extra[:2])
56 o.fatal("tag name must not be empty.")
57 debug1("args: tag name = %s; commit = %s\n"
58 % (path_msg(tag_name), commit.decode('ascii')))
60 if tag_name in tags and not opt.force:
61 log("bup: error: tag '%s' already exists\n" % path_msg(tag_name))
64 if tag_name.startswith(b'.'):
65 o.fatal("'%s' is not a valid tag name." % path_msg(tag_name))
68 hash = git.rev_parse(commit)
69 except git.GitError as e:
70 log("bup: error: %s" % e)
74 log("bup: error: commit %s not found.\n" % commit.decode('ascii'))
77 with git.PackIdxList(git.repo(b'objects/pack')) as pL:
78 if not pL.exists(hash):
79 log("bup: error: commit %s not found.\n" % commit.decode('ascii'))
82 tag_file = git.repo(b'refs/tags/' + tag_name)
84 tag = open(tag_file, 'wb')
86 log("bup: error: could not create tag '%s': %s" % (path_msg(tag_name), e))
89 tag.write(hexlify(hash))