]> arthur.barton.de Git - bup.git/blob - cmd/tag-cmd.py
tag-cmd: Some fixups
[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('bup tag', 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." % 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" % tag_name)
59     sys.exit(1)
60
61 try:
62     hash = git.rev_parse(commit)
63 except git.GitError, e:
64     log("bup: error: %s" % e)
65     sys.exit(2)
66
67 if not hash:
68     log("bup: error: commit %s not found." % commit)
69     sys.exit(2)
70
71 pL = git.PackIdxList(git.repo('objects/pack'))
72 if not pL.exists(hash):
73     log("bup: error: commit %s not found." % commit)
74     sys.exit(2)
75
76 tag_file = git.repo('refs/tags/%s' % tag_name)
77 try:
78     tag = file(tag_file, 'w')
79 except OSError, e:
80     log("bup: error: could not create tag '%s': %s" % (tag_name, e))
81     sys.exit(3)
82
83 tag.write(hash.encode('hex'))
84 tag.close()