]> arthur.barton.de Git - bup.git/blob - cmd/tag-cmd.py
492c3baad833631f7248553be60d1e48ea427f16
[bup.git] / cmd / tag-cmd.py
1 #!/bin/sh
2 """": # -*-python-*-
3 bup_python="$(dirname "$0")/bup-python" || exit $?
4 exec "$bup_python" "$0" ${1+"$@"}
5 """
6 # end of bup preamble
7
8 import os, sys
9
10 from bup import git, options
11 from bup.helpers import debug1, handle_ctrl_c, log
12
13 # FIXME: review for safe writes.
14
15 handle_ctrl_c()
16
17 optspec = """
18 bup tag
19 bup tag [-f] <tag name> <commit>
20 bup tag [-f] -d <tag name>
21 --
22 d,delete=   Delete a tag
23 f,force     Overwrite existing tag, or ignore missing tag when deleting
24 """
25
26 o = options.Options(optspec)
27 (opt, flags, extra) = o.parse(sys.argv[1:])
28
29 git.check_repo_or_die()
30
31 tags = [t for sublist in git.tags().values() for t in sublist]
32
33 if opt.delete:
34     # git.delete_ref() doesn't complain if a ref doesn't exist.  We
35     # could implement this verification but we'd need to read in the
36     # contents of the tag file and pass the hash, and we already know
37     # about the tag's existance via "tags".
38     if not opt.force and opt.delete not in tags:
39         log("error: tag '%s' doesn't exist\n" % opt.delete)
40         sys.exit(1)
41     tag_file = 'refs/tags/%s' % opt.delete
42     git.delete_ref(tag_file)
43     sys.exit(0)
44
45 if not extra:
46     for t in tags:
47         print t
48     sys.exit(0)
49 elif len(extra) < 2:
50     o.fatal('no commit ref or hash given.')
51
52 (tag_name, commit) = extra[:2]
53 if not tag_name:
54     o.fatal("tag name must not be empty.")
55 debug1("args: tag name = %s; commit = %s\n" % (tag_name, commit))
56
57 if tag_name in tags and not opt.force:
58     log("bup: error: tag '%s' already exists\n" % tag_name)
59     sys.exit(1)
60
61 if tag_name.startswith('.'):
62     o.fatal("'%s' is not a valid tag name." % tag_name)
63
64 try:
65     hash = git.rev_parse(commit)
66 except git.GitError as e:
67     log("bup: error: %s" % e)
68     sys.exit(2)
69
70 if not hash:
71     log("bup: error: commit %s not found.\n" % commit)
72     sys.exit(2)
73
74 pL = git.PackIdxList(git.repo('objects/pack'))
75 if not pL.exists(hash):
76     log("bup: error: commit %s not found.\n" % commit)
77     sys.exit(2)
78
79 tag_file = git.repo('refs/tags/%s' % tag_name)
80 try:
81     tag = file(tag_file, 'w')
82 except OSError as e:
83     log("bup: error: could not create tag '%s': %s" % (tag_name, e))
84     sys.exit(3)
85
86 tag.write(hash.encode('hex'))
87 tag.close()