grep: fix builds with with no thread support

Commit 0281e487fd91 ("grep: optionally recurse into submodules") added functions grep_submodule() and grep_submodule_launch() which use "struct work_item" which is defined only when thread support is available. The original implementation of grep_submodule() used the "struct work_item" in order to gain access to a strbuf to store its output which was to be printed at a later point in time. This differs from how both grep_file() and grep_sha1() handle their output. This patch eliminates the reliance on the "struct work_item" and instead opts to use the output function stored in the output field of the "struct grep_opt" object directly, making it behave similarly to both grep_file() and grep_sha1(). Reported-by: Rahul Bedarkar <rahul.bedarkar@imgtec.com> Signed-off-by: Brandon Williams <bmwill@google.com> Reviewed-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Williams committed Mar 17, 2017 at 11:41 UTC 2225e1ea20481a7c0da526891470abf9ece623e7
1 file changed +9 -12
builtin/grep.c
+9 -12
@@ -538,7 +538,7 @@ static int grep_submodule_launch(struct grep_opt *opt,
538 int status, i;
539 const char *end_of_base;
540 const char *name;
541 - struct work_item *w = opt->output_priv;
541 + struct strbuf child_output = STRBUF_INIT;
542
543 end_of_base = strchr(gs->name, ':');
544 if (gs->identifier && end_of_base)
@@ -593,14 +593,16 @@ static int grep_submodule_launch(struct grep_opt *opt,
593 * child process. A '0' indicates a hit, a '1' indicates no hit and
594 * anything else is an error.
595 */
596 - status = capture_command(&cp, &w->out, 0);
596 + status = capture_command(&cp, &child_output, 0);
597 if (status && (status != 1)) {
598 /* flush the buffer */
599 - write_or_die(1, w->out.buf, w->out.len);
599 + write_or_die(1, child_output.buf, child_output.len);
600 die("process for submodule '%s' failed with exit code: %d",
601 gs->name, status);
602 }
603
604 + opt->output(opt, child_output.buf, child_output.len);
605 + strbuf_release(&child_output);
606 /* invert the return code to make a hit equal to 1 */
607 return !status;
608 }
@@ -641,19 +643,14 @@ static int grep_submodule(struct grep_opt *opt, const unsigned char *sha1,
643 } else
644 #endif
645 {
644 - struct work_item w;
646 + struct grep_source gs;
647 int hit;
648
647 - grep_source_init(&w.source, GREP_SOURCE_SUBMODULE,
649 + grep_source_init(&gs, GREP_SOURCE_SUBMODULE,
650 filename, path, sha1);
649 - strbuf_init(&w.out, 0);
650 - opt->output_priv = &w;
651 - hit = grep_submodule_launch(opt, &w.source);
651 + hit = grep_submodule_launch(opt, &gs);
652
653 - write_or_die(1, w.out.buf, w.out.len);
654 -
655 - grep_source_clear(&w.source);
656 - strbuf_release(&w.out);
653 + grep_source_clear(&gs);
654 return hit;
655 }
656 }