]> arthur.barton.de Git - bup.git/blob - cmd/tag-cmd.py
Add 'tag -f' support.
[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 [-f] <tag name> <commit>
18 bup tag -d [-f] <tag name>
19 --
20 d,delete=   Delete a tag
21 f,force     Overwrite existing tag, or 'delete' a tag that doesn't exist
22 """
23
24 o = options.Options(optspec)
25 (opt, flags, extra) = o.parse(sys.argv[1:])
26
27 git.check_repo_or_die()
28
29 if opt.delete:
30     tag_file = git.repo('refs/tags/%s' % opt.delete)
31     debug1("tag file: %s\n" % tag_file)
32     if not os.path.exists(tag_file):
33         if opt.force:
34             sys.exit(0)
35         log("bup: error: tag '%s' not found.\n" % opt.delete)
36         sys.exit(1)
37
38     try:
39         os.unlink(tag_file)
40     except OSError, e:
41         log("bup: error: unable to delete tag '%s': %s" % (opt.delete, e))
42         sys.exit(1)
43
44     sys.exit(0)
45
46 tags = [t for sublist in git.tags().values() for t in sublist]
47
48 if not extra:
49     for t in tags:
50         print t
51     sys.exit(0)
52 elif len(extra) < 2:
53     o.fatal('no commit ref or hash given.')
54
55 (tag_name, commit) = extra[:2]
56 if not tag_name:
57     o.fatal("tag name must not be empty.")
58 debug1("args: tag name = %s; commit = %s\n" % (tag_name, commit))
59
60 if tag_name in tags and not opt.force:
61     log("bup: error: tag '%s' already exists\n" % tag_name)
62     sys.exit(1)
63
64 if tag_name.startswith('.'):
65     o.fatal("'%s' is not a valid tag name." % tag_name)
66
67 try:
68     hash = git.rev_parse(commit)
69 except git.GitError, e:
70     log("bup: error: %s" % e)
71     sys.exit(2)
72
73 if not hash:
74     log("bup: error: commit %s not found.\n" % commit)
75     sys.exit(2)
76
77 pL = git.PackIdxList(git.repo('objects/pack'))
78 if not pL.exists(hash):
79     log("bup: error: commit %s not found.\n" % commit)
80     sys.exit(2)
81
82 tag_file = git.repo('refs/tags/%s' % tag_name)
83 try:
84     tag = file(tag_file, 'w')
85 except OSError, e:
86     log("bup: error: could not create tag '%s': %s" % (tag_name, e))
87     sys.exit(3)
88
89 tag.write(hash.encode('hex'))
90 tag.close()