]> arthur.barton.de Git - bup.git/blob - cmd/tag-cmd.py
f33855ec7e293b8b98409464a6cb85edf2284e36
[bup.git] / cmd / tag-cmd.py
1 #!/usr/bin/env python
2
3 import sys
4 import os
5
6 from bup import git, options
7 from bup.helpers import *
8
9 # FIXME: review for safe writes.
10
11 handle_ctrl_c()
12
13 optspec = """
14 bup tag
15 bup tag [-f] <tag name> <commit>
16 bup tag [-f] -d <tag name>
17 --
18 d,delete=   Delete a tag
19 f,force     Overwrite existing tag, or ignore missing tag when deleting
20 """
21
22 o = options.Options(optspec)
23 (opt, flags, extra) = o.parse(sys.argv[1:])
24
25 git.check_repo_or_die()
26
27 tags = [t for sublist in git.tags().values() for t in sublist]
28
29 if opt.delete:
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)
36         sys.exit(1)
37     tag_file = 'refs/tags/%s' % opt.delete
38     git.delete_ref(tag_file)
39     sys.exit(0)
40
41 if not extra:
42     for t in tags:
43         print t
44     sys.exit(0)
45 elif len(extra) < 2:
46     o.fatal('no commit ref or hash given.')
47
48 (tag_name, commit) = extra[:2]
49 if not tag_name:
50     o.fatal("tag name must not be empty.")
51 debug1("args: tag name = %s; commit = %s\n" % (tag_name, commit))
52
53 if tag_name in tags and not opt.force:
54     log("bup: error: tag '%s' already exists\n" % tag_name)
55     sys.exit(1)
56
57 if tag_name.startswith('.'):
58     o.fatal("'%s' is not a valid tag name." % tag_name)
59
60 try:
61     hash = git.rev_parse(commit)
62 except git.GitError, e:
63     log("bup: error: %s" % e)
64     sys.exit(2)
65
66 if not hash:
67     log("bup: error: commit %s not found.\n" % commit)
68     sys.exit(2)
69
70 pL = git.PackIdxList(git.repo('objects/pack'))
71 if not pL.exists(hash):
72     log("bup: error: commit %s not found.\n" % commit)
73     sys.exit(2)
74
75 tag_file = git.repo('refs/tags/%s' % tag_name)
76 try:
77     tag = file(tag_file, 'w')
78 except OSError, e:
79     log("bup: error: could not create tag '%s': %s" % (tag_name, e))
80     sys.exit(3)
81
82 tag.write(hash.encode('hex'))
83 tag.close()