check-ignore: allow incremental streaming of queries via --stdin
authorAdam Spiers <git@adamspiers.org>
Thu, 11 Apr 2013 12:05:12 +0000 (13:05 +0100)
committerJunio C Hamano <gitster@pobox.com>
Thu, 11 Apr 2013 18:01:59 +0000 (11:01 -0700)
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>
builtin/check-ignore.c
t/t0008-ignores.sh
index e2d300646431fbd7a2b3f53ed4450b9af7a52f96..c00a7d6bb9d239e2b0fef97c8157d109c6073bfd 100644 (file)
@@ -107,10 +107,9 @@ static int check_ignore(struct path_exclude_check *check,
 static int check_ignore_stdin_paths(struct path_exclude_check *check, const char *prefix)
 {
        struct strbuf buf, nbuf;
-       char **pathspec = NULL;
-       size_t nr = 0, alloc = 0;
+       char *pathspec[2] = { NULL, NULL };
        int line_termination = null_term_line ? 0 : '\n';
-       int num_ignored;
+       int num_ignored = 0;
 
        strbuf_init(&buf, 0);
        strbuf_init(&nbuf, 0);
@@ -121,14 +120,10 @@ static int check_ignore_stdin_paths(struct path_exclude_check *check, const char
                                die("line is badly quoted");
                        strbuf_swap(&buf, &nbuf);
                }
-               ALLOC_GROW(pathspec, nr + 1, alloc);
-               pathspec[nr] = xcalloc(strlen(buf.buf) + 1, sizeof(*buf.buf));
-               strcpy(pathspec[nr++], buf.buf);
+               pathspec[0] = buf.buf;
+               num_ignored += check_ignore(check, prefix, (const char **)pathspec);
+               maybe_flush_or_die(stdout, "check-ignore to stdout");
        }
-       ALLOC_GROW(pathspec, nr + 1, alloc);
-       pathspec[nr] = NULL;
-       num_ignored = check_ignore(check, prefix, (const char **)pathspec);
-       maybe_flush_or_die(stdout, "attribute to stdout");
        strbuf_release(&buf);
        strbuf_release(&nbuf);
        return num_ignored;
index 7af93ba928ece85265509537b521764827194955..fbf12ae4e2fc3f8e864af0dcdc026ac82782f11f 100755 (executable)
@@ -216,11 +216,7 @@ test_expect_success_multi 'empty command line' '' '
 
 test_expect_success_multi '--stdin with empty STDIN' '' '
        test_check_ignore "--stdin" 1 </dev/null &&
-       if test -n "$quiet_opt"; then
-               test_stderr ""
-       else
-               test_stderr "no pathspec given."
-       fi
+       test_stderr ""
 '
 
 test_expect_success '-q with multiple args' '
@@ -692,5 +688,27 @@ do
        '
 done
 
+test_expect_success 'setup: have stdbuf?' '
+       if which stdbuf >/dev/null 2>&1
+       then
+               test_set_prereq STDBUF
+       fi
+'
+
+test_expect_success STDBUF 'streaming support for --stdin' '
+       (
+               echo one
+               sleep 2
+               echo two
+       ) | stdbuf -oL git check-ignore -v -n --stdin >out &
+       pid=$! &&
+       sleep 1 &&
+       grep "^\.gitignore:1:one        one" out &&
+       test $( wc -l <out ) = 1 &&
+       sleep 2 &&
+       grep "^::       two" out &&
+       test $( wc -l <out ) = 2 &&
+       ( wait $pid || kill $pid || : ) 2>/dev/null
+'
 
 test_done