add: ignore only ignored files

"git add foo bar" adds neither foo nor bar when bar is ignored, but dies to let the user recheck their command invocation. This becomes less helpful when "git add foo.*" is subject to shell expansion and some of the expanded files are ignored. "git add --ignore-errors" is supposed to ignore errors when indexing some files and adds the others. It does ignore errors from actual indexing attempts, but does not ignore the error "file is ignored" as outlined above. This is unexpected. Change "git add foo bar" to add foo when bar is ignored, but issue a warning and return a failure code as before the change. That is, in the case of trying to add ignored files we now act the same way (with or without "--ignore-errors") in which we act for more severe indexing errors when "--ignore-errors" is specified. Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net> Reviewed-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael J Gruber committed Nov 21, 2014 at 17:08 UTC 1d31e5a2cd9f90799330d5095783e7b972357f0e
2 files changed +8 -2
builtin/add.c
+1 -1
@@ -284,7 +284,7 @@ static int add_files(struct dir_struct *dir, int flags)
284 for (i = 0; i < dir->ignored_nr; i++)
285 fprintf(stderr, "%s\n", dir->ignored[i]->name);
286 fprintf(stderr, _("Use -f if you really want to add them.\n"));
287 - die(_("no files added"));
287 + exit_status = 1;
288 }
289
290 for (i = 0; i < dir->nr; i++)
t/t3700-add.sh
+7 -1
@@ -91,6 +91,13 @@ test_expect_success 'error out when attempting to add ignored ones without -f' '
91 ! (git ls-files | grep "\\.ig")
92 '
93
94 +test_expect_success 'error out when attempting to add ignored ones but add others' '
95 + touch a.if &&
96 + test_must_fail git add a.?? &&
97 + ! (git ls-files | grep "\\.ig") &&
98 + (git ls-files | grep a.if)
99 +'
100 +
101 test_expect_success 'add ignored ones with -f' '
102 git add -f a.?? &&
103 git ls-files --error-unmatch a.ig
@@ -311,7 +318,6 @@ cat >expect.err <<\EOF
318 The following paths are ignored by one of your .gitignore files:
319 ignored-file
320 Use -f if you really want to add them.
314 -fatal: no files added
321 EOF
322 cat >expect.out <<\EOF
323 add 'track-this'