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 -d [-f] <tag name>
21 d,delete= Delete a tag
22 f,force Overwrite existing tag, or 'delete' a tag that doesn't exist
25 o = options.Options(optspec)
26 (opt, flags, extra) = o.parse(sys.argv[1:])
28 git.check_repo_or_die()
31 tag_file = git.repo('refs/tags/%s' % opt.delete)
32 debug1("tag file: %s\n" % tag_file)
33 if not os.path.exists(tag_file):
36 log("bup: error: tag '%s' not found.\n" % opt.delete)
42 log("bup: error: unable to delete tag '%s': %s" % (opt.delete, e))
47 tags = [t for sublist in git.tags().values() for t in sublist]
54 o.fatal('no commit ref or hash given.')
56 (tag_name, commit) = extra[:2]
58 o.fatal("tag name must not be empty.")
59 debug1("args: tag name = %s; commit = %s\n" % (tag_name, commit))
61 if tag_name in tags and not opt.force:
62 log("bup: error: tag '%s' already exists\n" % tag_name)
65 if tag_name.startswith('.'):
66 o.fatal("'%s' is not a valid tag name." % tag_name)
69 hash = git.rev_parse(commit)
70 except git.GitError, e:
71 log("bup: error: %s" % e)
75 log("bup: error: commit %s not found.\n" % commit)
78 pL = git.PackIdxList(git.repo('objects/pack'))
79 if not pL.exists(hash):
80 log("bup: error: commit %s not found.\n" % commit)
83 tag_file = git.repo('refs/tags/%s' % tag_name)
85 tag = file(tag_file, 'w')
87 log("bup: error: could not create tag '%s': %s" % (tag_name, e))
90 tag.write(hash.encode('hex'))