]> arthur.barton.de Git - bup.git/commitdiff
Add "bup index --clear" to clear the index.
authorZoran Zaric <zz@zoranzaric.de>
Wed, 13 Feb 2013 02:29:52 +0000 (03:29 +0100)
committerRob Browning <rlb@defaultvalue.org>
Wed, 13 Feb 2013 02:40:29 +0000 (20:40 -0600)
Signed-off-by: Zoran Zaric <zz@zoranzaric.de>
Reviewed-by: Rob Browning <rlb@defaultvalue.org>
Documentation/bup-index.md
cmd/index-cmd.py
t/test.sh

index 1280068ecec6f6368f146b2b5bd020ae0790b5f6..e25c9a21f493d379a0d4a5d6650b50db70dd419d 100644 (file)
@@ -9,7 +9,7 @@ bup-index - print and/or update the bup filesystem index
 # SYNOPSIS
 
 bup index \<-p|-m|-s|-u\> [-H] [-l] [-x] [\--fake-valid]
-[\--fake-invalid] [\--check] [-f *indexfile*] [\--exclude *path*]
+[\--fake-invalid] [\--check] [\--clear] [-f *indexfile*] [\--exclude *path*]
 [\--exclude-from *filename*] [-v] \<filenames...\>
 
 # DESCRIPTION
@@ -138,6 +138,9 @@ does, due to the accommodations described above.
 :   carefully check index file integrity before and after
     updating.  Mostly useful for automated tests.
 
+\--clear
+:   clear the index.
+
 -f, \--indexfile=*indexfile*
 :   use a different index filename instead of
     `~/.bup/bupindex`.
index 0c40ea90fec9be8b5964be9c4c6b014399f564b2..cb358809a4caa6312f2dc53d4c0bdc990a992163 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 
-import sys, stat, time, os
+import sys, stat, time, os, errno
 from bup import metadata, options, git, index, drecurse, hlinkdb
 from bup.helpers import *
 from bup.hashsplit import GIT_MODE_TREE, GIT_MODE_FILE
@@ -49,6 +49,21 @@ def check_index(reader):
     log('check: passed.\n')
 
 
+def clear_index(indexfile):
+    indexfiles = [indexfile, indexfile + '.meta', indexfile + '.hlink']
+    cleared = False
+    for indexfile in indexfiles:
+        path = git.repo(indexfile)
+        try:
+            os.remove(path)
+            if opt.verbose:
+                log('clear: removed %s\n' % path)
+            cleared = True
+        except OSError, e:
+            if e.errno != errno.ENOENT:
+                raise
+
+
 def update_index(top, excluded_paths):
     # tmax and start must be epoch nanoseconds.
     tmax = (time.time() - 1) * 10**9
@@ -162,6 +177,7 @@ m,modified print only added/deleted/modified files (implies -p)
 s,status   print each filename with a status char (A/M/D) (implies -p)
 u,update   recursively update the index entries for the given file/dir names (default if no mode is specified)
 check      carefully check index file integrity
+clear      clear the index
  Options:
 H,hash     print the hash for each object next to its name
 l,long     print more information about each file
@@ -176,7 +192,12 @@ x,xdev,one-file-system  don't cross filesystem boundaries
 o = options.Options(optspec)
 (opt, flags, extra) = o.parse(sys.argv[1:])
 
-if not (opt.modified or opt['print'] or opt.status or opt.update or opt.check):
+if not (opt.modified or \
+        opt['print'] or \
+        opt.status or \
+        opt.update or \
+        opt.check or \
+        opt.clear):
     opt.update = 1
 if (opt.fake_valid or opt.fake_invalid) and not opt.update:
     o.fatal('--fake-{in,}valid are meaningless without -u')
@@ -199,6 +220,10 @@ if opt.check:
     log('check: starting initial check.\n')
     check_index(index.Reader(indexfile))
 
+if opt.clear:
+    log('clear: clearing index.\n')
+    clear_index(indexfile)
+
 excluded_paths = drecurse.parse_excludes(flags)
 
 paths = index.reduce_paths(extra)
index 5a036090028e5e52818526c066ce926fa51b7d4a..797291b2a83103cf0ca0d504d4407bca81d377c5 100755 (executable)
--- a/t/test.sh
+++ b/t/test.sh
@@ -665,3 +665,24 @@ WVSTART "save disjoint top-level directories"
     # For now, assume that "ls -a" and "sort" use the same order.
     WVPASSEQ "$(bup ls -a src/latest)" "$(echo -e "$top_dir/\ntmp/" | sort)"
 ) || WVFAIL
+
+
+WVSTART "clear-index"
+D=clear-index.tmp
+export BUP_DIR="$TOP/$D/.bup"
+rm -rf $TOP/$D
+mkdir $TOP/$D
+WVPASS bup init
+touch $TOP/$D/foo
+touch $TOP/$D/bar
+bup index -u $D
+WVPASSEQ "$(bup index -p)" "$D/foo
+$D/bar
+$D/
+./"
+rm $TOP/$D/foo
+WVPASS bup index --clear
+bup index -u $TOP/$D
+WVPASSEQ "$(bup index -p)" "$D/bar
+$D/
+./"