2 from __future__ import absolute_import
5 from bup import git, options
6 from bup.compat import argv_bytes
7 from bup.helpers import debug1, log
8 from bup.io import byte_stream, path_msg
11 # FIXME: review for safe writes.
15 bup tag [-f] <tag name> <commit>
16 bup tag [-f] -d <tag name>
18 d,delete= Delete a tag
19 f,force Overwrite existing tag, or ignore missing tag when deleting
23 o = options.Options(optspec)
24 opt, flags, extra = o.parse_bytes(argv[1:])
26 git.check_repo_or_die()
28 tags = [t for sublist in git.tags().values() for t in sublist]
31 # git.delete_ref() doesn't complain if a ref doesn't exist. We
32 # could implement this verification but we'd need to read in the
33 # contents of the tag file and pass the hash, and we already know
34 # about the tag's existance via "tags".
35 tag_name = argv_bytes(opt.delete)
36 if not opt.force and tag_name not in tags:
37 log("error: tag '%s' doesn't exist\n" % path_msg(tag_name))
39 tag_file = b'refs/tags/%s' % tag_name
40 git.delete_ref(tag_file)
46 out = byte_stream(sys.stdout)
51 o.fatal('expected commit ref and hash')
53 tag_name, commit = map(argv_bytes, extra[:2])
55 o.fatal("tag name must not be empty.")
56 debug1("args: tag name = %s; commit = %s\n"
57 % (path_msg(tag_name), commit.decode('ascii')))
59 if tag_name in tags and not opt.force:
60 log("bup: error: tag '%s' already exists\n" % path_msg(tag_name))
63 if tag_name.startswith(b'.'):
64 o.fatal("'%s' is not a valid tag name." % path_msg(tag_name))
67 hash = git.rev_parse(commit)
68 except git.GitError as e:
69 log("bup: error: %s" % e)
73 log("bup: error: commit %s not found.\n" % commit.decode('ascii'))
76 with git.PackIdxList(git.repo(b'objects/pack')) as pL:
77 if not pL.exists(hash):
78 log("bup: error: commit %s not found.\n" % commit.decode('ascii'))
81 git.update_ref(b'refs/tags/' + tag_name, hash, None, force=True)