Skip to content

Commit 431d6e7

Browse files
René Scharfegitster
authored andcommitted
grep: enable threading for context line printing
If context lines are to be printed, grep separates them with hunk marks ("--\n"). These marks are printed between matches from different files, too. They are not printed before the first file, though. Threading was disabled when context line printing was enabled because avoiding to print the mark before the first line was an unsolved synchronisation problem. This patch separates the code for printing hunk marks for the threaded and the unthreaded case, allowing threading to be turned on together with the common -ABC options. ->show_hunk_mark, which controls printing of hunk marks between files in show_line(), is now set in grep_buffer_1(), but only if some results have already been printed and threading is disabled. The threaded case is handled in work_done(). Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent c24138b commit 431d6e7

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

builtin/grep.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ static pthread_cond_t cond_write;
9696
/* Signalled when we are finished with everything. */
9797
static pthread_cond_t cond_result;
9898

99+
static int print_hunk_marks_between_files;
100+
static int printed_something;
101+
99102
static void add_work(enum work_type type, char *name, void *id)
100103
{
101104
grep_lock();
@@ -159,7 +162,12 @@ static void work_done(struct work_item *w)
159162
for(; todo[todo_done].done && todo_done != todo_start;
160163
todo_done = (todo_done+1) % ARRAY_SIZE(todo)) {
161164
w = &todo[todo_done];
162-
write_or_die(1, w->out.buf, w->out.len);
165+
if (w->out.len) {
166+
if (print_hunk_marks_between_files && printed_something)
167+
write_or_die(1, "--\n", 3);
168+
write_or_die(1, w->out.buf, w->out.len);
169+
printed_something = 1;
170+
}
163171
free(w->name);
164172
free(w->identifier);
165173
}
@@ -926,8 +934,11 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
926934
if (online_cpus() == 1 || !grep_threads_ok(&opt))
927935
use_threads = 0;
928936

929-
if (use_threads)
937+
if (use_threads) {
938+
if (opt.pre_context || opt.post_context)
939+
print_hunk_marks_between_files = 1;
930940
start_threads(&opt);
941+
}
931942
#else
932943
use_threads = 0;
933944
#endif

grep.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,6 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
551551
if (opt->last_shown == 0) {
552552
if (opt->show_hunk_mark)
553553
opt->output(opt, "--\n", 3);
554-
else
555-
opt->show_hunk_mark = 1;
556554
} else if (lno > opt->last_shown + 1)
557555
opt->output(opt, "--\n", 3);
558556
}
@@ -750,14 +748,6 @@ int grep_threads_ok(const struct grep_opt *opt)
750748
!opt->name_only)
751749
return 0;
752750

753-
/* If we are showing hunk marks, we should not do it for the
754-
* first match. The synchronization problem we get for this
755-
* constraint is not yet solved, so we disable threading in
756-
* this case.
757-
*/
758-
if (opt->pre_context || opt->post_context)
759-
return 0;
760-
761751
return 1;
762752
}
763753

@@ -779,11 +769,14 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
779769
enum grep_context ctx = GREP_CONTEXT_HEAD;
780770
xdemitconf_t xecfg;
781771

782-
opt->last_shown = 0;
783-
784772
if (!opt->output)
785773
opt->output = std_output;
786774

775+
if (opt->last_shown && (opt->pre_context || opt->post_context) &&
776+
opt->output == std_output)
777+
opt->show_hunk_mark = 1;
778+
opt->last_shown = 0;
779+
787780
if (buffer_is_binary(buf, size)) {
788781
switch (opt->binary) {
789782
case GREP_BINARY_DEFAULT:

0 commit comments

Comments
 (0)