Skip to content

Commit ae3caf4

Browse files
aspiersgitster
authored andcommitted
check-ignore: add -n / --non-matching option
If `-n` or `--non-matching` are specified, non-matching pathnames will also be output, in which case all fields in each output record except for <pathname> will be empty. This can be useful when running check-ignore as a background process, so that files can be incrementally streamed to STDIN, and for each of these files, STDOUT will indicate whether that file matched a pattern or not. (Without this option, it would be impossible to tell whether the absence of output for a given file meant that it didn't match any pattern, or that the result simply hadn't been flushed to STDOUT yet.) Signed-off-by: Adam Spiers <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent eef39f8 commit ae3caf4

File tree

3 files changed

+134
-49
lines changed

3 files changed

+134
-49
lines changed

Documentation/git-check-ignore.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ OPTIONS
3939
below). If `--stdin` is also given, input paths are separated
4040
with a NUL character instead of a linefeed character.
4141

42+
-n, --non-matching::
43+
Show given paths which don't match any pattern. This only
44+
makes sense when `--verbose` is enabled, otherwise it would
45+
not be possible to distinguish between paths which match a
46+
pattern and those which don't.
47+
4248
OUTPUT
4349
------
4450

@@ -65,6 +71,15 @@ are also used instead of colons and hard tabs:
6571

6672
<source> <NULL> <linenum> <NULL> <pattern> <NULL> <pathname> <NULL>
6773

74+
If `-n` or `--non-matching` are specified, non-matching pathnames will
75+
also be output, in which case all fields in each output record except
76+
for <pathname> will be empty. This can be useful when running
77+
non-interactively, so that files can be incrementally streamed to
78+
STDIN of a long-running check-ignore process, and for each of these
79+
files, STDOUT will indicate whether that file matched a pattern or
80+
not. (Without this option, it would be impossible to tell whether the
81+
absence of output for a given file meant that it didn't match any
82+
pattern, or that the output hadn't been generated yet.)
6883

6984
EXIT STATUS
7085
-----------

builtin/check-ignore.c

Lines changed: 29 additions & 17 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;
8+
static int quiet, verbose, stdin_paths, show_non_matching;
99
static const char * const check_ignore_usage[] = {
1010
"git check-ignore [options] pathname...",
1111
"git check-ignore [options] --stdin < <list-of-paths>",
@@ -22,33 +22,43 @@ static const struct option check_ignore_options[] = {
2222
N_("read file names from stdin")),
2323
OPT_BOOLEAN('z', NULL, &null_term_line,
2424
N_("input paths are terminated by a null character")),
25+
OPT_BOOLEAN('n', "non-matching", &show_non_matching,
26+
N_("show non-matching input paths")),
2527
OPT_END()
2628
};
2729

2830
static void output_exclude(const char *path, struct exclude *exclude)
2931
{
30-
char *bang = exclude->flags & EXC_FLAG_NEGATIVE ? "!" : "";
31-
char *slash = exclude->flags & EXC_FLAG_MUSTBEDIR ? "/" : "";
32+
char *bang = (exclude && exclude->flags & EXC_FLAG_NEGATIVE) ? "!" : "";
33+
char *slash = (exclude && exclude->flags & EXC_FLAG_MUSTBEDIR) ? "/" : "";
3234
if (!null_term_line) {
3335
if (!verbose) {
3436
write_name_quoted(path, stdout, '\n');
3537
} else {
36-
quote_c_style(exclude->el->src, NULL, stdout, 0);
37-
printf(":%d:%s%s%s\t",
38-
exclude->srcpos,
39-
bang, exclude->pattern, slash);
38+
if (exclude) {
39+
quote_c_style(exclude->el->src, NULL, stdout, 0);
40+
printf(":%d:%s%s%s\t",
41+
exclude->srcpos,
42+
bang, exclude->pattern, slash);
43+
}
44+
else {
45+
printf("::\t");
46+
}
4047
quote_c_style(path, NULL, stdout, 0);
4148
fputc('\n', stdout);
4249
}
4350
} else {
4451
if (!verbose) {
4552
printf("%s%c", path, '\0');
4653
} else {
47-
printf("%s%c%d%c%s%s%s%c%s%c",
48-
exclude->el->src, '\0',
49-
exclude->srcpos, '\0',
50-
bang, exclude->pattern, slash, '\0',
51-
path, '\0');
54+
if (exclude)
55+
printf("%s%c%d%c%s%s%s%c%s%c",
56+
exclude->el->src, '\0',
57+
exclude->srcpos, '\0',
58+
bang, exclude->pattern, slash, '\0',
59+
path, '\0');
60+
else
61+
printf("%c%c%c%s%c", '\0', '\0', '\0', path, '\0');
5262
}
5363
}
5464
}
@@ -89,15 +99,15 @@ static int check_ignore(const char *prefix, const char **pathspec)
8999
? strlen(prefix) : 0, path);
90100
full_path = check_path_for_gitlink(full_path);
91101
die_if_path_beyond_symlink(full_path, prefix);
102+
exclude = NULL;
92103
if (!seen[i]) {
93104
exclude = last_exclude_matching_path(&check, full_path,
94105
-1, &dtype);
95-
if (exclude) {
96-
if (!quiet)
97-
output_exclude(path, exclude);
98-
num_ignored++;
99-
}
100106
}
107+
if (!quiet && (exclude || show_non_matching))
108+
output_exclude(path, exclude);
109+
if (exclude)
110+
num_ignored++;
101111
}
102112
free(seen);
103113
clear_directory(&dir);
@@ -161,6 +171,8 @@ int cmd_check_ignore(int argc, const char **argv, const char *prefix)
161171
if (verbose)
162172
die(_("cannot have both --quiet and --verbose"));
163173
}
174+
if (show_non_matching && !verbose)
175+
die(_("--non-matching is only valid with --verbose"));
164176

165177
if (stdin_paths) {
166178
num_ignored = check_ignore_stdin_paths(prefix);

t/t0008-ignores.sh

Lines changed: 90 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,33 @@ 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 $args \
69+
echo git $global_args check-ignore $quiet_opt $verbose_opt $non_matching_opt $args \
7070
>"$HOME/cmd" &&
71+
echo "$expect_code" >"$HOME/expected-exit-code" &&
7172
test_expect_code "$expect_code" \
72-
git $global_args check-ignore $quiet_opt $verbose_opt $args \
73+
git $global_args check-ignore $quiet_opt $verbose_opt $non_matching_opt $args \
7374
>"$HOME/stdout" 2>"$HOME/stderr" &&
7475
test_cmp "$HOME/expected-stdout" "$HOME/stdout" &&
7576
stderr_empty_on_success "$expect_code"
7677
}
7778

78-
# Runs the same code with 3 different levels of output verbosity,
79+
# Runs the same code with 4 different levels of output verbosity:
80+
#
81+
# 1. with -q / --quiet
82+
# 2. with default verbosity
83+
# 3. with -v / --verbose
84+
# 4. with -v / --verbose, *and* -n / --non-matching
85+
#
7986
# expecting success each time. Takes advantage of the fact that
8087
# check-ignore --verbose output is the same as normal output except
8188
# for the extra first column.
8289
#
8390
# Arguments:
8491
# - (optional) prereqs for this test, e.g. 'SYMLINKS'
8592
# - test name
86-
# - output to expect from -v / --verbose mode
93+
# - output to expect from the fourth verbosity mode (the output
94+
# from the other verbosity modes is automatically inferred
95+
# from this value)
8796
# - code to run (should invoke test_check_ignore)
8897
test_expect_success_multi () {
8998
prereq=
@@ -92,32 +101,50 @@ test_expect_success_multi () {
92101
prereq=$1
93102
shift
94103
fi
95-
testname="$1" expect_verbose="$2" code="$3"
104+
testname="$1" expect_all="$2" code="$3"
96105

106+
expect_verbose=$( echo "$expect_all" | grep -v '^:: ' )
97107
expect=$( echo "$expect_verbose" | sed -e 's/.* //' )
98108

99109
test_expect_success $prereq "$testname" '
100110
expect "$expect" &&
101111
eval "$code"
102112
'
103113

104-
for quiet_opt in '-q' '--quiet'
105-
do
106-
test_expect_success $prereq "$testname${quiet_opt:+ with $quiet_opt}" "
114+
# --quiet is only valid when a single pattern is passed
115+
if test $( echo "$expect_all" | wc -l ) = 1
116+
then
117+
for quiet_opt in '-q' '--quiet'
118+
do
119+
test_expect_success $prereq "$testname${quiet_opt:+ with $quiet_opt}" "
107120
expect '' &&
108121
$code
109122
"
110-
done
111-
quiet_opt=
123+
done
124+
quiet_opt=
125+
fi
112126

113127
for verbose_opt in '-v' '--verbose'
114128
do
115-
test_expect_success $prereq "$testname${verbose_opt:+ with $verbose_opt}" "
116-
expect '$expect_verbose' &&
117-
$code
118-
"
129+
for non_matching_opt in '' ' -n' ' --non-matching'
130+
do
131+
if test -n "$non_matching_opt"
132+
then
133+
my_expect="$expect_all"
134+
else
135+
my_expect="$expect_verbose"
136+
fi
137+
138+
test_code="
139+
expect '$my_expect' &&
140+
$code
141+
"
142+
opts="$verbose_opt$non_matching_opt"
143+
test_expect_success $prereq "$testname${opts:+ with $opts}" "$test_code"
144+
done
119145
done
120146
verbose_opt=
147+
non_matching_opt=
121148
}
122149

123150
test_expect_success 'setup' '
@@ -178,7 +205,7 @@ test_expect_success 'setup' '
178205
#
179206
# test invalid inputs
180207

181-
test_expect_success_multi '. corner-case' '' '
208+
test_expect_success_multi '. corner-case' ':: .' '
182209
test_check_ignore . 1
183210
'
184211

@@ -276,27 +303,39 @@ do
276303
where="in subdir $subdir"
277304
fi
278305

279-
test_expect_success_multi "non-existent file $where not ignored" '' "
280-
test_check_ignore '${subdir}non-existent' 1
281-
"
306+
test_expect_success_multi "non-existent file $where not ignored" \
307+
":: ${subdir}non-existent" \
308+
"test_check_ignore '${subdir}non-existent' 1"
282309

283310
test_expect_success_multi "non-existent file $where ignored" \
284-
".gitignore:1:one ${subdir}one" "
285-
test_check_ignore '${subdir}one'
286-
"
311+
".gitignore:1:one ${subdir}one" \
312+
"test_check_ignore '${subdir}one'"
287313

288-
test_expect_success_multi "existing untracked file $where not ignored" '' "
289-
test_check_ignore '${subdir}not-ignored' 1
290-
"
314+
test_expect_success_multi "existing untracked file $where not ignored" \
315+
":: ${subdir}not-ignored" \
316+
"test_check_ignore '${subdir}not-ignored' 1"
291317

292-
test_expect_success_multi "existing tracked file $where not ignored" '' "
293-
test_check_ignore '${subdir}ignored-but-in-index' 1
294-
"
318+
test_expect_success_multi "existing tracked file $where not ignored" \
319+
":: ${subdir}ignored-but-in-index" \
320+
"test_check_ignore '${subdir}ignored-but-in-index' 1"
295321

296322
test_expect_success_multi "existing untracked file $where ignored" \
297-
".gitignore:2:ignored-* ${subdir}ignored-and-untracked" "
298-
test_check_ignore '${subdir}ignored-and-untracked'
299-
"
323+
".gitignore:2:ignored-* ${subdir}ignored-and-untracked" \
324+
"test_check_ignore '${subdir}ignored-and-untracked'"
325+
326+
test_expect_success_multi "mix of file types $where" \
327+
":: ${subdir}non-existent
328+
.gitignore:1:one ${subdir}one
329+
:: ${subdir}not-ignored
330+
:: ${subdir}ignored-but-in-index
331+
.gitignore:2:ignored-* ${subdir}ignored-and-untracked" \
332+
"test_check_ignore '
333+
${subdir}non-existent
334+
${subdir}one
335+
${subdir}not-ignored
336+
${subdir}ignored-but-in-index
337+
${subdir}ignored-and-untracked'
338+
"
300339
done
301340

302341
# Having established the above, from now on we mostly test against
@@ -391,7 +430,7 @@ test_expect_success 'cd to ignored sub-directory with -v' '
391430
#
392431
# test handling of symlinks
393432

394-
test_expect_success_multi SYMLINKS 'symlink' '' '
433+
test_expect_success_multi SYMLINKS 'symlink' ':: a/symlink' '
395434
test_check_ignore "a/symlink" 1
396435
'
397436

@@ -574,22 +613,33 @@ cat <<-\EOF >stdin
574613
globaltwo
575614
b/globaltwo
576615
../b/globaltwo
616+
c/not-ignored
577617
EOF
578-
cat <<-EOF >expected-verbose
618+
# N.B. we deliberately end STDIN with a non-matching pattern in order
619+
# to test that the exit code indicates that one or more of the
620+
# provided paths is ignored - in other words, that it represents an
621+
# aggregation of all the results, not just the final result.
622+
623+
cat <<-EOF >expected-all
579624
.gitignore:1:one ../one
625+
:: ../not-ignored
580626
.gitignore:1:one one
627+
:: not-ignored
581628
a/b/.gitignore:8:!on* b/on
582629
a/b/.gitignore:8:!on* b/one
583630
a/b/.gitignore:8:!on* b/one one
584631
a/b/.gitignore:8:!on* b/one two
585632
a/b/.gitignore:8:!on* "b/one\"three"
586633
a/b/.gitignore:9:!two b/two
634+
:: b/not-ignored
587635
a/.gitignore:1:two* b/twooo
588636
$global_excludes:2:!globaltwo ../globaltwo
589637
$global_excludes:2:!globaltwo globaltwo
590638
$global_excludes:2:!globaltwo b/globaltwo
591639
$global_excludes:2:!globaltwo ../b/globaltwo
640+
:: c/not-ignored
592641
EOF
642+
grep -v '^:: ' expected-all >expected-verbose
593643
sed -e 's/.* //' expected-verbose >expected-default
594644

595645
sed -e 's/^"//' -e 's/\\//' -e 's/"$//' stdin | \
@@ -615,6 +665,14 @@ test_expect_success '--stdin from subdirectory with -v' '
615665
)
616666
'
617667

668+
test_expect_success '--stdin from subdirectory with -v -n' '
669+
expect_from_stdin <expected-all &&
670+
(
671+
cd a &&
672+
test_check_ignore "--stdin -v -n" <../stdin
673+
)
674+
'
675+
618676
for opts in '--stdin -z' '-z --stdin'
619677
do
620678
test_expect_success "$opts from subdirectory" '

0 commit comments

Comments
 (0)