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