Skip to content

Commit 0c8e8c0

Browse files
aspiersgitster
authored andcommitted
check-ignore: allow incremental streaming of queries via --stdin
Some callers, such as the git-annex web assistant, find it useful to invoke git check-ignore as a persistent background process, which can then have queries fed to its STDIN at any point, and the corresponding response consumed from its STDOUT. For this we need to invoke check_ignore() once per line of standard input, and flush standard output after each result. The above use case suggests that empty STDIN is actually a reasonable scenario (e.g. when the caller doesn't know in advance whether any queries need to be fed to the background process until after it's already started), so we make the minor behavioural change that "no pathspec given." is no longer emitted in when STDIN is empty. Even though check_ignore() could now be changed to operate on a single pathspec, we keep it operating on an array of pathspecs since that is a more convenient way of consuming the existing pathspec API. Signed-off-by: Adam Spiers <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0006d85 commit 0c8e8c0

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

builtin/check-ignore.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,9 @@ static int check_ignore(struct path_exclude_check *check,
107107
static int check_ignore_stdin_paths(struct path_exclude_check *check, const char *prefix)
108108
{
109109
struct strbuf buf, nbuf;
110-
char **pathspec = NULL;
111-
size_t nr = 0, alloc = 0;
110+
char *pathspec[2] = { NULL, NULL };
112111
int line_termination = null_term_line ? 0 : '\n';
113-
int num_ignored;
112+
int num_ignored = 0;
114113

115114
strbuf_init(&buf, 0);
116115
strbuf_init(&nbuf, 0);
@@ -121,14 +120,10 @@ static int check_ignore_stdin_paths(struct path_exclude_check *check, const char
121120
die("line is badly quoted");
122121
strbuf_swap(&buf, &nbuf);
123122
}
124-
ALLOC_GROW(pathspec, nr + 1, alloc);
125-
pathspec[nr] = xcalloc(strlen(buf.buf) + 1, sizeof(*buf.buf));
126-
strcpy(pathspec[nr++], buf.buf);
123+
pathspec[0] = buf.buf;
124+
num_ignored += check_ignore(check, prefix, (const char **)pathspec);
125+
maybe_flush_or_die(stdout, "check-ignore to stdout");
127126
}
128-
ALLOC_GROW(pathspec, nr + 1, alloc);
129-
pathspec[nr] = NULL;
130-
num_ignored = check_ignore(check, prefix, (const char **)pathspec);
131-
maybe_flush_or_die(stdout, "attribute to stdout");
132127
strbuf_release(&buf);
133128
strbuf_release(&nbuf);
134129
return num_ignored;

t/t0008-ignores.sh

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,7 @@ test_expect_success_multi 'empty command line' '' '
216216

217217
test_expect_success_multi '--stdin with empty STDIN' '' '
218218
test_check_ignore "--stdin" 1 </dev/null &&
219-
if test -n "$quiet_opt"; then
220-
test_stderr ""
221-
else
222-
test_stderr "no pathspec given."
223-
fi
219+
test_stderr ""
224220
'
225221

226222
test_expect_success '-q with multiple args' '
@@ -692,5 +688,27 @@ do
692688
'
693689
done
694690

691+
test_expect_success 'setup: have stdbuf?' '
692+
if which stdbuf >/dev/null 2>&1
693+
then
694+
test_set_prereq STDBUF
695+
fi
696+
'
697+
698+
test_expect_success STDBUF 'streaming support for --stdin' '
699+
(
700+
echo one
701+
sleep 2
702+
echo two
703+
) | stdbuf -oL git check-ignore -v -n --stdin >out &
704+
pid=$! &&
705+
sleep 1 &&
706+
grep "^\.gitignore:1:one one" out &&
707+
test $( wc -l <out ) = 1 &&
708+
sleep 2 &&
709+
grep "^:: two" out &&
710+
test $( wc -l <out ) = 2 &&
711+
( wait $pid || kill $pid || : ) 2>/dev/null
712+
'
695713

696714
test_done

0 commit comments

Comments
 (0)