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 <git@adamspiers.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Adam Spiers committed Apr 11, 2013 at 13:05 UTC 0c8e8c080b08632d6dbee33d093b5df648196b49
2 files changed +28 -15
builtin/check-ignore.c
+5 -10
@@ -107,10 +107,9 @@ static int check_ignore(struct path_exclude_check *check,
107 static int check_ignore_stdin_paths(struct path_exclude_check *check, const char *prefix)
108 {
109 struct strbuf buf, nbuf;
110 - char **pathspec = NULL;
111 - size_t nr = 0, alloc = 0;
110 + char *pathspec[2] = { NULL, NULL };
111 int line_termination = null_term_line ? 0 : '\n';
113 - int num_ignored;
112 + int num_ignored = 0;
113
114 strbuf_init(&buf, 0);
115 strbuf_init(&nbuf, 0);
@@ -121,14 +120,10 @@ static int check_ignore_stdin_paths(struct path_exclude_check *check, const char
120 die("line is badly quoted");
121 strbuf_swap(&buf, &nbuf);
122 }
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");
126 }
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");
127 strbuf_release(&buf);
128 strbuf_release(&nbuf);
129 return num_ignored;
t/t0008-ignores.sh
+23 -5
@@ -216,11 +216,7 @@ test_expect_success_multi 'empty command line' '' '
216
217 test_expect_success_multi '--stdin with empty STDIN' '' '
218 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 ""
220 '
221
222 test_expect_success '-q with multiple args' '
@@ -692,5 +688,27 @@ do
688 '
689 done
690
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 +'
713
714 test_done