]> arthur.barton.de Git - bup.git/blobdiff - cmd/tag-cmd.py
cleanup-mounts-under: Don't fail when /proc/mounts isn't readable
[bup.git] / cmd / tag-cmd.py
index bedd97c3504e5d9561cfa08e3f8ca8a7bb027ea8..f33855ec7e293b8b98409464a6cb85edf2284e36 100755 (executable)
@@ -1,23 +1,22 @@
 #!/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.
-"""
+
 import sys
 import os
 
 from bup import git, options
 from bup.helpers import *
 
+# FIXME: review for safe writes.
 
 handle_ctrl_c()
 
 optspec = """
 bup tag
-bup tag <tag name> <commit>
-bup tag -d <tag name>
+bup tag [-f] <tag name> <commit>
+bup tag [-f] -d <tag name>
 --
 d,delete=   Delete a tag
+f,force     Overwrite existing tag, or ignore missing tag when deleting
 """
 
 o = options.Options(optspec)
@@ -25,23 +24,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):
-        log("bup: error: tag '%s' not found." % 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
@@ -54,8 +50,8 @@ if not tag_name:
     o.fatal("tag name must not be empty.")
 debug1("args: tag name = %s; commit = %s\n" % (tag_name, commit))
 
-if tag_name in tags:
-    log("bup: error: tag '%s' already exists" % tag_name)
+if tag_name in tags and not opt.force:
+    log("bup: error: tag '%s' already exists\n" % tag_name)
     sys.exit(1)
 
 if tag_name.startswith('.'):
@@ -68,12 +64,12 @@ except git.GitError, e:
     sys.exit(2)
 
 if not hash:
-    log("bup: error: commit %s not found." % commit)
+    log("bup: error: commit %s not found.\n" % commit)
     sys.exit(2)
 
 pL = git.PackIdxList(git.repo('objects/pack'))
 if not pL.exists(hash):
-    log("bup: error: commit %s not found." % commit)
+    log("bup: error: commit %s not found.\n" % commit)
     sys.exit(2)
 
 tag_file = git.repo('refs/tags/%s' % tag_name)