Skip to content

Commit c51afbb

Browse files
committed
Merge branch 'as/check-ignore'
Enhance "check-ignore" (1.8.2 update) to work more like "check-attr" over bidi-pipes. * as/check-ignore: t0008: use named pipe (FIFO) to test check-ignore streaming Documentation: add caveats about I/O buffering for check-{attr,ignore} check-ignore: allow incremental streaming of queries via --stdin check-ignore: move setup into cmd_check_ignore() check-ignore: add -n / --non-matching option t0008: remove duplicated test fixture data
2 parents 77eb44b + b96114e commit c51afbb

File tree

5 files changed

+188
-96
lines changed

5 files changed

+188
-96
lines changed

Documentation/git-check-attr.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ being queried and <info> can be either:
5656
'set';; when the attribute is defined as true.
5757
<value>;; when a value has been assigned to the attribute.
5858

59+
Buffering happens as documented under the `GIT_FLUSH` option in
60+
linkgit:git[1]. The caller is responsible for avoiding deadlocks
61+
caused by overfilling an input buffer or reading from an empty output
62+
buffer.
63+
5964
EXAMPLES
6065
--------
6166

Documentation/git-check-ignore.txt

Lines changed: 20 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,20 @@ 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.)
83+
84+
Buffering happens as documented under the `GIT_FLUSH` option in
85+
linkgit:git[1]. The caller is responsible for avoiding deadlocks
86+
caused by overfilling an input buffer or reading from an empty output
87+
buffer.
6888

6989
EXIT STATUS
7090
-----------

Documentation/git.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -811,8 +811,9 @@ for further details.
811811
'GIT_FLUSH'::
812812
If this environment variable is set to "1", then commands such
813813
as 'git blame' (in incremental mode), 'git rev-list', 'git log',
814-
and 'git whatchanged' will force a flush of the output stream
815-
after each commit-oriented record have been flushed. If this
814+
'git check-attr', 'git check-ignore', and 'git whatchanged' will
815+
force a flush of the output stream after each record have been
816+
flushed. If this
816817
variable is set to "0", the output of these commands will be done
817818
using completely buffered I/O. If this environment variable is
818819
not set, Git will choose buffered or record-oriented flushing

builtin/check-ignore.c

Lines changed: 50 additions & 42 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,52 +22,55 @@ 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
}
5565

56-
static int check_ignore(const char *prefix, const char **pathspec)
66+
static int check_ignore(struct dir_struct *dir,
67+
const char *prefix, const char **pathspec)
5768
{
58-
struct dir_struct dir;
5969
const char *path, *full_path;
6070
char *seen;
6171
int num_ignored = 0, dtype = DT_UNKNOWN, i;
6272
struct exclude *exclude;
6373

64-
/* read_cache() is only necessary so we can watch out for submodules. */
65-
if (read_cache() < 0)
66-
die(_("index file corrupt"));
67-
68-
memset(&dir, 0, sizeof(dir));
69-
setup_standard_excludes(&dir);
70-
7174
if (!pathspec || !*pathspec) {
7275
if (!quiet)
7376
fprintf(stderr, "no pathspec given.\n");
@@ -86,28 +89,26 @@ static int check_ignore(const char *prefix, const char **pathspec)
8689
? strlen(prefix) : 0, path);
8790
full_path = check_path_for_gitlink(full_path);
8891
die_if_path_beyond_symlink(full_path, prefix);
92+
exclude = NULL;
8993
if (!seen[i]) {
90-
exclude = last_exclude_matching(&dir, full_path, &dtype);
91-
if (exclude) {
92-
if (!quiet)
93-
output_exclude(path, exclude);
94-
num_ignored++;
95-
}
94+
exclude = last_exclude_matching(dir, full_path, &dtype);
9695
}
96+
if (!quiet && (exclude || show_non_matching))
97+
output_exclude(path, exclude);
98+
if (exclude)
99+
num_ignored++;
97100
}
98101
free(seen);
99-
clear_directory(&dir);
100102

101103
return num_ignored;
102104
}
103105

104-
static int check_ignore_stdin_paths(const char *prefix)
106+
static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
105107
{
106108
struct strbuf buf, nbuf;
107-
char **pathspec = NULL;
108-
size_t nr = 0, alloc = 0;
109+
char *pathspec[2] = { NULL, NULL };
109110
int line_termination = null_term_line ? 0 : '\n';
110-
int num_ignored;
111+
int num_ignored = 0;
111112

112113
strbuf_init(&buf, 0);
113114
strbuf_init(&nbuf, 0);
@@ -118,23 +119,19 @@ static int check_ignore_stdin_paths(const char *prefix)
118119
die("line is badly quoted");
119120
strbuf_swap(&buf, &nbuf);
120121
}
121-
ALLOC_GROW(pathspec, nr + 1, alloc);
122-
pathspec[nr] = xcalloc(strlen(buf.buf) + 1, sizeof(*buf.buf));
123-
strcpy(pathspec[nr++], buf.buf);
122+
pathspec[0] = buf.buf;
123+
num_ignored += check_ignore(dir, prefix, (const char **)pathspec);
124+
maybe_flush_or_die(stdout, "check-ignore to stdout");
124125
}
125-
ALLOC_GROW(pathspec, nr + 1, alloc);
126-
pathspec[nr] = NULL;
127-
num_ignored = check_ignore(prefix, (const char **)pathspec);
128-
maybe_flush_or_die(stdout, "attribute to stdout");
129126
strbuf_release(&buf);
130127
strbuf_release(&nbuf);
131-
free(pathspec);
132128
return num_ignored;
133129
}
134130

135131
int cmd_check_ignore(int argc, const char **argv, const char *prefix)
136132
{
137133
int num_ignored;
134+
struct dir_struct dir;
138135

139136
git_config(git_default_config, NULL);
140137

@@ -156,13 +153,24 @@ int cmd_check_ignore(int argc, const char **argv, const char *prefix)
156153
if (verbose)
157154
die(_("cannot have both --quiet and --verbose"));
158155
}
156+
if (show_non_matching && !verbose)
157+
die(_("--non-matching is only valid with --verbose"));
158+
159+
/* read_cache() is only necessary so we can watch out for submodules. */
160+
if (read_cache() < 0)
161+
die(_("index file corrupt"));
162+
163+
memset(&dir, 0, sizeof(dir));
164+
setup_standard_excludes(&dir);
159165

160166
if (stdin_paths) {
161-
num_ignored = check_ignore_stdin_paths(prefix);
167+
num_ignored = check_ignore_stdin_paths(&dir, prefix);
162168
} else {
163-
num_ignored = check_ignore(prefix, argv);
169+
num_ignored = check_ignore(&dir, prefix, argv);
164170
maybe_flush_or_die(stdout, "ignore to stdout");
165171
}
166172

173+
clear_directory(&dir);
174+
167175
return !num_ignored;
168176
}

0 commit comments

Comments
 (0)