2 """Tag a commit in the bup repository.
3 Creating a tag on a commit can be used for avoiding automatic cleanup from
4 removing this commit due to old age.
9 from bup import git, options
10 from bup.helpers import *
12 # FIXME: review for safe writes.
18 bup tag [-f] <tag name> <commit>
19 bup tag [-f] -d <tag name>
21 d,delete= Delete a tag
22 f,force Overwrite existing tag, or ignore missing tag when deleting
25 o = options.Options(optspec)
26 (opt, flags, extra) = o.parse(sys.argv[1:])
28 git.check_repo_or_die()
30 tags = [t for sublist in git.tags().values() for t in sublist]
33 # git.delete_ref() doesn't complain if a ref doesn't exist. We
34 # could implement this verification but we'd need to read in the
35 # contents of the tag file and pass the hash, and we already know
36 # about the tag's existance via "tags".
37 if not opt.force and opt.delete not in tags:
38 log("error: tag '%s' doesn't exist\n" % opt.delete)
40 tag_file = 'refs/tags/%s' % opt.delete
41 git.delete_ref(tag_file)
49 o.fatal('no commit ref or hash given.')
51 (tag_name, commit) = extra[:2]
53 o.fatal("tag name must not be empty.")
54 debug1("args: tag name = %s; commit = %s\n" % (tag_name, commit))
56 if tag_name in tags and not opt.force:
57 log("bup: error: tag '%s' already exists\n" % tag_name)
60 if tag_name.startswith('.'):
61 o.fatal("'%s' is not a valid tag name." % tag_name)
64 hash = git.rev_parse(commit)
65 except git.GitError, e:
66 log("bup: error: %s" % e)
70 log("bup: error: commit %s not found.\n" % commit)
73 pL = git.PackIdxList(git.repo('objects/pack'))
74 if not pL.exists(hash):
75 log("bup: error: commit %s not found.\n" % commit)
78 tag_file = git.repo('refs/tags/%s' % tag_name)
80 tag = file(tag_file, 'w')
82 log("bup: error: could not create tag '%s': %s" % (tag_name, e))
85 tag.write(hash.encode('hex'))