6 from bup import git, options
7 from bup.helpers import *
9 # 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
22 o = options.Options(optspec)
23 (opt, flags, extra) = o.parse(sys.argv[1:])
25 git.check_repo_or_die()
27 tags = [t for sublist in git.tags().values() for t in sublist]
30 # git.delete_ref() doesn't complain if a ref doesn't exist. We
31 # could implement this verification but we'd need to read in the
32 # contents of the tag file and pass the hash, and we already know
33 # about the tag's existance via "tags".
34 if not opt.force and opt.delete not in tags:
35 log("error: tag '%s' doesn't exist\n" % opt.delete)
37 tag_file = 'refs/tags/%s' % opt.delete
38 git.delete_ref(tag_file)
46 o.fatal('no commit ref or hash given.')
48 (tag_name, commit) = extra[:2]
50 o.fatal("tag name must not be empty.")
51 debug1("args: tag name = %s; commit = %s\n" % (tag_name, commit))
53 if tag_name in tags and not opt.force:
54 log("bup: error: tag '%s' already exists\n" % tag_name)
57 if tag_name.startswith('.'):
58 o.fatal("'%s' is not a valid tag name." % tag_name)
61 hash = git.rev_parse(commit)
62 except git.GitError, e:
63 log("bup: error: %s" % e)
67 log("bup: error: commit %s not found.\n" % commit)
70 pL = git.PackIdxList(git.repo('objects/pack'))
71 if not pL.exists(hash):
72 log("bup: error: commit %s not found.\n" % commit)
75 tag_file = git.repo('refs/tags/%s' % tag_name)
77 tag = file(tag_file, 'w')
79 log("bup: error: could not create tag '%s': %s" % (tag_name, e))
82 tag.write(hash.encode('hex'))