Skip to content

Commit 8231fa6

Browse files
flashydavegitster
authored andcommitted
check-ignore: Add option to ignore index contents
check-ignore currently shows how .gitignore rules would treat untracked paths. Tracked paths do not generate useful output. This prevents debugging of why a path became tracked unexpectedly unless that path is first removed from the index with `git rm --cached <path>`. The option --no-index tells the command to bypass the check for the path being in the index and hence allows tracked paths to be checked too. Whilst this behaviour deviates from the characteristics of `git add` and `git status` its use case is unlikely to cause any user confusion. Test scripts are augmented to check this option against the standard ignores to ensure correct behaviour. Signed-off-by: Dave Williams <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 57e4c17 commit 8231fa6

File tree

3 files changed

+74
-11
lines changed

3 files changed

+74
-11
lines changed

Documentation/git-check-ignore.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ OPTIONS
4545
not be possible to distinguish between paths which match a
4646
pattern and those which don't.
4747

48+
--no-index::
49+
Don't look in the index when undertaking the checks. This can
50+
be used to debug why a path became tracked by e.g. `git add .`
51+
and was not ignored by the rules as expected by the user or when
52+
developing patterns including negation to match a path previously
53+
added with `git add -f`.
54+
4855
OUTPUT
4956
------
5057

builtin/check-ignore.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "pathspec.h"
66
#include "parse-options.h"
77

8-
static int quiet, verbose, stdin_paths, show_non_matching;
8+
static int quiet, verbose, stdin_paths, show_non_matching, no_index;
99
static const char * const check_ignore_usage[] = {
1010
"git check-ignore [options] pathname...",
1111
"git check-ignore [options] --stdin < <list-of-paths>",
@@ -24,6 +24,8 @@ static const struct option check_ignore_options[] = {
2424
N_("terminate input and output records by a NUL character")),
2525
OPT_BOOL('n', "non-matching", &show_non_matching,
2626
N_("show non-matching input paths")),
27+
OPT_BOOL(0, "no-index", &no_index,
28+
N_("ignore index when checking")),
2729
OPT_END()
2830
};
2931

@@ -157,7 +159,7 @@ int cmd_check_ignore(int argc, const char **argv, const char *prefix)
157159
die(_("--non-matching is only valid with --verbose"));
158160

159161
/* read_cache() is only necessary so we can watch out for submodules. */
160-
if (read_cache() < 0)
162+
if (!no_index && read_cache() < 0)
161163
die(_("index file corrupt"));
162164

163165
memset(&dir, 0, sizeof(dir));

t/t0008-ignores.sh

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ test_check_ignore () {
6666

6767
init_vars &&
6868
rm -f "$HOME/stdout" "$HOME/stderr" "$HOME/cmd" &&
69-
echo git $global_args check-ignore $quiet_opt $verbose_opt $non_matching_opt $args \
69+
echo git $global_args check-ignore $quiet_opt $verbose_opt $non_matching_opt $no_index_opt $args \
7070
>"$HOME/cmd" &&
7171
echo "$expect_code" >"$HOME/expected-exit-code" &&
7272
test_expect_code "$expect_code" \
73-
git $global_args check-ignore $quiet_opt $verbose_opt $non_matching_opt $args \
73+
git $global_args check-ignore $quiet_opt $verbose_opt $non_matching_opt $no_index_opt $args \
7474
>"$HOME/stdout" 2>"$HOME/stderr" &&
7575
test_cmp "$HOME/expected-stdout" "$HOME/stdout" &&
7676
stderr_empty_on_success "$expect_code"
@@ -87,26 +87,36 @@ test_check_ignore () {
8787
# check-ignore --verbose output is the same as normal output except
8888
# for the extra first column.
8989
#
90+
# A parameter is used to determine if the tests are run with the
91+
# normal case (using the index), or with the --no-index option.
92+
#
9093
# Arguments:
9194
# - (optional) prereqs for this test, e.g. 'SYMLINKS'
9295
# - test name
9396
# - output to expect from the fourth verbosity mode (the output
9497
# from the other verbosity modes is automatically inferred
9598
# from this value)
9699
# - code to run (should invoke test_check_ignore)
97-
test_expect_success_multi () {
100+
# - index option: --index or --no-index
101+
test_expect_success_multiple () {
98102
prereq=
99-
if test $# -eq 4
103+
if test $# -eq 5
100104
then
101105
prereq=$1
102106
shift
103107
fi
108+
if test "$4" = "--index"
109+
then
110+
no_index_opt=
111+
else
112+
no_index_opt=$4
113+
fi
104114
testname="$1" expect_all="$2" code="$3"
105115

106116
expect_verbose=$( echo "$expect_all" | grep -v '^:: ' )
107117
expect=$( echo "$expect_verbose" | sed -e 's/.* //' )
108118

109-
test_expect_success $prereq "$testname" '
119+
test_expect_success $prereq "$testname${no_index_opt:+ with $no_index_opt}" '
110120
expect "$expect" &&
111121
eval "$code"
112122
'
@@ -116,7 +126,8 @@ test_expect_success_multi () {
116126
then
117127
for quiet_opt in '-q' '--quiet'
118128
do
119-
test_expect_success $prereq "$testname${quiet_opt:+ with $quiet_opt}" "
129+
opts="${no_index_opt:+$no_index_opt }$quiet_opt"
130+
test_expect_success $prereq "$testname${opts:+ with $opts}" "
120131
expect '' &&
121132
$code
122133
"
@@ -126,7 +137,7 @@ test_expect_success_multi () {
126137

127138
for verbose_opt in '-v' '--verbose'
128139
do
129-
for non_matching_opt in '' ' -n' ' --non-matching'
140+
for non_matching_opt in '' '-n' '--non-matching'
130141
do
131142
if test -n "$non_matching_opt"
132143
then
@@ -139,12 +150,21 @@ test_expect_success_multi () {
139150
expect '$my_expect' &&
140151
$code
141152
"
142-
opts="$verbose_opt$non_matching_opt"
153+
opts="${no_index_opt:+$no_index_opt }$verbose_opt${non_matching_opt:+ $non_matching_opt}"
143154
test_expect_success $prereq "$testname${opts:+ with $opts}" "$test_code"
144155
done
145156
done
146157
verbose_opt=
147158
non_matching_opt=
159+
no_index_opt=
160+
}
161+
162+
test_expect_success_multi () {
163+
test_expect_success_multiple "$@" "--index"
164+
}
165+
166+
test_expect_success_no_index_multi () {
167+
test_expect_success_multiple "$@" "--no-index"
148168
}
149169

150170
test_expect_success 'setup' '
@@ -288,7 +308,7 @@ test_expect_success_multi 'needs work tree' '' '
288308

289309
# First make sure that the presence of a file in the working tree
290310
# does not impact results, but that the presence of a file in the
291-
# index does.
311+
# index does unless the --no-index option is used.
292312

293313
for subdir in '' 'a/'
294314
do
@@ -303,27 +323,61 @@ do
303323
":: ${subdir}non-existent" \
304324
"test_check_ignore '${subdir}non-existent' 1"
305325

326+
test_expect_success_no_index_multi "non-existent file $where not ignored" \
327+
":: ${subdir}non-existent" \
328+
"test_check_ignore '${subdir}non-existent' 1"
329+
306330
test_expect_success_multi "non-existent file $where ignored" \
307331
".gitignore:1:one ${subdir}one" \
308332
"test_check_ignore '${subdir}one'"
309333

334+
test_expect_success_no_index_multi "non-existent file $where ignored" \
335+
".gitignore:1:one ${subdir}one" \
336+
"test_check_ignore '${subdir}one'"
337+
310338
test_expect_success_multi "existing untracked file $where not ignored" \
311339
":: ${subdir}not-ignored" \
312340
"test_check_ignore '${subdir}not-ignored' 1"
313341

342+
test_expect_success_no_index_multi "existing untracked file $where not ignored" \
343+
":: ${subdir}not-ignored" \
344+
"test_check_ignore '${subdir}not-ignored' 1"
345+
314346
test_expect_success_multi "existing tracked file $where not ignored" \
315347
":: ${subdir}ignored-but-in-index" \
316348
"test_check_ignore '${subdir}ignored-but-in-index' 1"
317349

350+
test_expect_success_no_index_multi "existing tracked file $where shown as ignored" \
351+
".gitignore:2:ignored-* ${subdir}ignored-but-in-index" \
352+
"test_check_ignore '${subdir}ignored-but-in-index'"
353+
318354
test_expect_success_multi "existing untracked file $where ignored" \
319355
".gitignore:2:ignored-* ${subdir}ignored-and-untracked" \
320356
"test_check_ignore '${subdir}ignored-and-untracked'"
321357

358+
test_expect_success_no_index_multi "existing untracked file $where ignored" \
359+
".gitignore:2:ignored-* ${subdir}ignored-and-untracked" \
360+
"test_check_ignore '${subdir}ignored-and-untracked'"
361+
322362
test_expect_success_multi "mix of file types $where" \
323363
":: ${subdir}non-existent
324364
.gitignore:1:one ${subdir}one
325365
:: ${subdir}not-ignored
326366
:: ${subdir}ignored-but-in-index
367+
.gitignore:2:ignored-* ${subdir}ignored-and-untracked" \
368+
"test_check_ignore '
369+
${subdir}non-existent
370+
${subdir}one
371+
${subdir}not-ignored
372+
${subdir}ignored-but-in-index
373+
${subdir}ignored-and-untracked'
374+
"
375+
376+
test_expect_success_no_index_multi "mix of file types $where" \
377+
":: ${subdir}non-existent
378+
.gitignore:1:one ${subdir}one
379+
:: ${subdir}not-ignored
380+
.gitignore:2:ignored-* ${subdir}ignored-but-in-index
327381
.gitignore:2:ignored-* ${subdir}ignored-and-untracked" \
328382
"test_check_ignore '
329383
${subdir}non-existent

0 commit comments

Comments
 (0)