All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Add -q option to "git rm" to suppress output when there aren't errors.
@ 2007-04-16  0:04 Steven Grimm
  2007-04-16  0:13 ` Junio C Hamano
  0 siblings, 1 reply; 14+ messages in thread
From: Steven Grimm @ 2007-04-16  0:04 UTC (permalink / raw)
  To: git

This suppresses the output of "rm" commands, and also exits with a zero
exit code when no files match.  This allows "git rm" (and more importantly,
"git rm -r") to be used as an index filter with cg-admin-rewritehist.

Signed-off-by: Steven Grimm <koreth@midwinter.com>
---
 Documentation/git-rm.txt |    4 ++++
 builtin-rm.c             |   26 ++++++++++++++++++++------
 t/t3600-rm.sh            |   28 ++++++++++++++++++++++++++++
 3 files changed, 52 insertions(+), 6 deletions(-)

diff --git a/Documentation/git-rm.txt b/Documentation/git-rm.txt
index 6feebc0..c354134 100644
--- a/Documentation/git-rm.txt
+++ b/Documentation/git-rm.txt
@@ -33,6 +33,10 @@ OPTIONS
         Don't actually remove the file(s), just show if they exist in
         the index.
 
+-q::
+	Don't output the names of the files being removed, and exit
+	with a zero status even if no files matched.
+
 -r::
         Allow recursive removal when a leading directory name is
         given.
diff --git a/builtin-rm.c b/builtin-rm.c
index 8a0738f..3d438de 100644
--- a/builtin-rm.c
+++ b/builtin-rm.c
@@ -10,7 +10,7 @@
 #include "tree-walk.h"
 
 static const char builtin_rm_usage[] =
-"git-rm [-f] [-n] [-r] [--cached] [--] <file>...";
+"git-rm [-f] [-n] [-q] [-r] [--cached] [--] <file>...";
 
 static struct {
 	int nr, alloc;
@@ -104,7 +104,7 @@ static struct lock_file lock_file;
 int cmd_rm(int argc, const char **argv, const char *prefix)
 {
 	int i, newfd;
-	int show_only = 0, force = 0, index_only = 0, recursive = 0;
+	int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0;
 	const char **pathspec;
 	char *seen;
 
@@ -130,6 +130,8 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 			index_only = 1;
 		else if (!strcmp(arg, "-f"))
 			force = 1;
+		else if (!strcmp(arg, "-q"))
+			quiet = 1;
 		else if (!strcmp(arg, "-r"))
 			recursive = 1;
 		else
@@ -153,14 +155,24 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 
 	if (pathspec) {
 		const char *match;
+		int seen_any = 0;
 		for (i = 0; (match = pathspec[i]) != NULL ; i++) {
-			if (!seen[i])
-				die("pathspec '%s' did not match any files",
-				    match);
+			if (!seen[i]) {
+				if (! quiet) {
+					die("pathspec '%s' did not match any files",
+					    match);
+				}
+			}
+			else {
+				seen_any = 1;
+			}
 			if (!recursive && seen[i] == MATCHED_RECURSIVELY)
 				die("not removing '%s' recursively without -r",
 				    *match ? match : ".");
 		}
+
+		if (! seen_any)
+			exit(0);
 	}
 
 	/*
@@ -187,7 +199,9 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 	 */
 	for (i = 0; i < list.nr; i++) {
 		const char *path = list.name[i];
-		printf("rm '%s'\n", path);
+		if (!quiet) {
+			printf("rm '%s'\n", path);
+		}
 
 		if (remove_file_from_cache(path))
 			die("git-rm: unable to remove %s", path);
diff --git a/t/t3600-rm.sh b/t/t3600-rm.sh
index e31cf93..bd72feb 100755
--- a/t/t3600-rm.sh
+++ b/t/t3600-rm.sh
@@ -84,6 +84,30 @@ test_expect_success \
     'When the rm in "git-rm -f" fails, it should not remove the file from the index' \
     'git-ls-files --error-unmatch baz'
 
+test_expect_success 'Remove nonexistent file with -q returns zero exit status' '
+	git rm -q nonexistent
+'
+
+test_expect_success '"rm" command printed' '
+	echo frotz > test-file &&
+	git add test-file &&
+	git commit -m "add file for rm test" &&
+	git rm test-file > rm-output &&
+	test `egrep "^rm " rm-output | wc -l` = 1 &&
+	rm -f test-file rm-output &&
+	git commit -m "remove file from rm test"
+'
+
+test_expect_success '"rm" command suppressed with -q' '
+	echo frotz > test-file &&
+	git add test-file &&
+	git commit -m "add file for rm -q test" &&
+	git rm -q test-file > rm-output &&
+	test `wc -l < rm-output` = 0 &&
+	rm -f test-file rm-output &&
+	git commit -m "remove file from rm test"
+'
+
 # Now, failure cases.
 test_expect_success 'Re-add foo and baz' '
 	git add foo baz &&
@@ -154,4 +178,8 @@ test_expect_success 'Recursive with -r -f' '
 	! test -d frotz
 '
 
+test_expect_failure 'Remove nonexistent file returns nonzero exit status' '
+	git rm nonexistent
+'
+
 test_done
-- 
1.5.1.1.99.g0ea98

^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2007-04-16 18:29 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-04-16  0:04 [PATCH] Add -q option to "git rm" to suppress output when there aren't errors Steven Grimm
2007-04-16  0:13 ` Junio C Hamano
2007-04-16  0:17   ` Steven Grimm
2007-04-16  1:14     ` Junio C Hamano
2007-04-16  7:46       ` [PATCH 1/2] Add --quiet option to suppress output of "rm" commands for removed files Steven Grimm
2007-04-16  7:53         ` [PATCH 2/2] Add --ignore-notfound option to exit with zero status when no files are removed Steven Grimm
2007-04-16  7:59           ` Junio C Hamano
2007-04-16  8:13             ` Steven Grimm
2007-04-16  8:50             ` Jeff King
2007-04-16  8:53               ` Junio C Hamano
2007-04-16  9:04                 ` Jeff King
2007-04-16 18:29                   ` Josef Weidendorfer
2007-04-16  8:12           ` Steven Grimm
2007-04-16  7:54       ` [PATCH] Add -q option to "git rm" to suppress output when there aren't errors Alex Riesen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.