]> arthur.barton.de Git - bup.git/blob - cmd/tag-cmd.py
add a tag command
[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" % e)
39         sys.exit(1)
40
41     sys.exit(0)
42
43 tags = []
44 for (t, dummy) in git.list_refs():
45     if t.startswith('refs/tags/'):
46         tags.append(t[10:])
47
48 if not extra:
49     for t in tags:
50         log("%s\n" % t)
51     sys.exit(0)
52 elif len(extra) != 2:
53     log('bup: error: no ref or hash given.')
54     sys.exit(1)
55
56 tag_name = extra[0]
57 commit = extra[1]
58 debug1("from args: tag name = %s; commit = %s\n" % (tag_name, commit))
59
60 if tag_name in tags:
61     log("bup: error: tag '%s' already exists" % tag_name)
62     sys.exit(1)
63
64 hash = git.rev_parse(commit)
65 if not hash:
66     log("bup: error: commit %s not found." % commit)
67     sys.exit(2)
68
69 pL = git.PackIdxList(git.repo('objects/pack'))
70 if not pL.exists(hash):
71     log("bup: error: commit %s not found." % commit)
72     sys.exit(2)
73
74 tag_file = git.repo('refs/tags/%s' % tag_name)
75 try:
76     tag = file(tag_file, 'w')
77 except OSError, e:
78     log('bup: error: could not create tag %s: %s' % (tag_name, e))
79     sys.exit(3)
80
81 tag.write(hash.encode('hex'))
82 tag.close()