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 *
17 bup tag [-f] <tag name> <commit>
18 bup tag -d [-f] <tag name>
20 d,delete= Delete a tag
21 f,force Overwrite existing tag, or 'delete' a tag that doesn't exist
24 o = options.Options(optspec)
25 (opt, flags, extra) = o.parse(sys.argv[1:])
27 git.check_repo_or_die()
30 tag_file = git.repo('refs/tags/%s' % opt.delete)
31 debug1("tag file: %s\n" % tag_file)
32 if not os.path.exists(tag_file):
35 log("bup: error: tag '%s' not found.\n" % opt.delete)
41 log("bup: error: unable to delete tag '%s': %s" % (opt.delete, e))
46 tags = [t for sublist in git.tags().values() for t in sublist]
53 o.fatal('no commit ref or hash given.')
55 (tag_name, commit) = extra[:2]
57 o.fatal("tag name must not be empty.")
58 debug1("args: tag name = %s; commit = %s\n" % (tag_name, commit))
60 if tag_name in tags and not opt.force:
61 log("bup: error: tag '%s' already exists\n" % tag_name)
64 if tag_name.startswith('.'):
65 o.fatal("'%s' is not a valid tag name." % tag_name)
68 hash = git.rev_parse(commit)
69 except git.GitError, e:
70 log("bup: error: %s" % e)
74 log("bup: error: commit %s not found.\n" % commit)
77 pL = git.PackIdxList(git.repo('objects/pack'))
78 if not pL.exists(hash):
79 log("bup: error: commit %s not found.\n" % commit)
82 tag_file = git.repo('refs/tags/%s' % tag_name)
84 tag = file(tag_file, 'w')
86 log("bup: error: could not create tag '%s': %s" % (tag_name, e))
89 tag.write(hash.encode('hex'))