]> arthur.barton.de Git - bup.git/blobdiff - cmd/tag-cmd.py
web: remove unneeded __name__ == '__main__' guard
[bup.git] / cmd / tag-cmd.py
index 28fa328b6d5e5048687d571854f12e31b9e79707..492c3baad833631f7248553be60d1e48ea427f16 100755 (executable)
@@ -1,13 +1,14 @@
-#!/usr/bin/env python
-"""Tag a commit in the bup repository.
-Creating a tag on a commit can be used for avoiding automatic cleanup from
-removing this commit due to old age.
+#!/bin/sh
+"""": # -*-python-*-
+bup_python="$(dirname "$0")/bup-python" || exit $?
+exec "$bup_python" "$0" ${1+"$@"}
 """
-import sys
-import os
+# end of bup preamble
+
+import os, sys
 
 from bup import git, options
-from bup.helpers import *
+from bup.helpers import debug1, handle_ctrl_c, log
 
 # FIXME: review for safe writes.
 
@@ -16,10 +17,10 @@ handle_ctrl_c()
 optspec = """
 bup tag
 bup tag [-f] <tag name> <commit>
-bup tag -d [-f] <tag name>
+bup tag [-f] -d <tag name>
 --
 d,delete=   Delete a tag
-f,force     Overwrite existing tag, or 'delete' a tag that doesn't exist
+f,force     Overwrite existing tag, or ignore missing tag when deleting
 """
 
 o = options.Options(optspec)
@@ -27,25 +28,20 @@ o = options.Options(optspec)
 
 git.check_repo_or_die()
 
-if opt.delete:
-    tag_file = git.repo('refs/tags/%s' % opt.delete)
-    debug1("tag file: %s\n" % tag_file)
-    if not os.path.exists(tag_file):
-        if opt.force:
-            sys.exit(0)
-        log("bup: error: tag '%s' not found.\n" % opt.delete)
-        sys.exit(1)
+tags = [t for sublist in git.tags().values() for t in sublist]
 
-    try:
-        os.unlink(tag_file)
-    except OSError, e:
-        log("bup: error: unable to delete tag '%s': %s" % (opt.delete, e))
+if opt.delete:
+    # git.delete_ref() doesn't complain if a ref doesn't exist.  We
+    # could implement this verification but we'd need to read in the
+    # contents of the tag file and pass the hash, and we already know
+    # about the tag's existance via "tags".
+    if not opt.force and opt.delete not in tags:
+        log("error: tag '%s' doesn't exist\n" % opt.delete)
         sys.exit(1)
-
+    tag_file = 'refs/tags/%s' % opt.delete
+    git.delete_ref(tag_file)
     sys.exit(0)
 
-tags = [t for sublist in git.tags().values() for t in sublist]
-
 if not extra:
     for t in tags:
         print t
@@ -67,7 +63,7 @@ if tag_name.startswith('.'):
 
 try:
     hash = git.rev_parse(commit)
-except git.GitError, e:
+except git.GitError as e:
     log("bup: error: %s" % e)
     sys.exit(2)
 
@@ -83,7 +79,7 @@ if not pL.exists(hash):
 tag_file = git.repo('refs/tags/%s' % tag_name)
 try:
     tag = file(tag_file, 'w')
-except OSError, e:
+except OSError as e:
     log("bup: error: could not create tag '%s': %s" % (tag_name, e))
     sys.exit(3)