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