]> arthur.barton.de Git - bup.git/blob - cmd/tag-cmd.py
Improve formatting of error and warning messages.
[bup.git] / cmd / tag-cmd.py
1 #!/usr/bin/env python
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.
5 """
6 import sys
7 import os
8
9 from bup import git, options
10 from bup.helpers import *
11
12
13 handle_ctrl_c()
14
15 optspec = """
16 bup tag
17 bup tag <tag name> <commit>
18 bup tag -d <tag name>
19 --
20 d,delete=   Delete a tag
21 """
22
23 o = options.Options(optspec)
24 (opt, flags, extra) = o.parse(sys.argv[1:])
25
26 git.check_repo_or_die()
27
28 if opt.delete:
29     tag_file = git.repo('refs/tags/%s' % opt.delete)
30     debug1("tag file: %s\n" % tag_file)
31     if not os.path.exists(tag_file):
32         log("bup: error: tag '%s' not found.\n" % opt.delete)
33         sys.exit(1)
34
35     try:
36         os.unlink(tag_file)
37     except OSError, e:
38         log("bup: error: unable to delete tag '%s': %s" % (opt.delete, e))
39         sys.exit(1)
40
41     sys.exit(0)
42
43 tags = [t for sublist in git.tags().values() for t in sublist]
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:
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, 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, 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()