grep --all-match
This lets you say: git grep --all-match -e A -e B -e C to find lines that match A or B or C but limit the matches from the files that have all of A, B and C. This is different from git grep -e A --and -e B --and -e C in that the latter looks for a single line that has all of these at the same time. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Sep 27, 2006 at 17:50 UTC
0ab7befa31d07fe3ffb51a6cc626d4c09ded1c92
4 files changed
+112
-19
Documentation/git-grep.txt
+10
-1
@@ -14,7 +14,7 @@ SYNOPSIS
14
[-v | --invert-match] [-h|-H] [--full-name]
15
[-E | --extended-regexp] [-G | --basic-regexp] [-F | --fixed-strings]
16
[-n] [-l | --files-with-matches] [-L | --files-without-match]
17
- [-c | --count]
17
+ [-c | --count] [--all-match]
18
[-A <post-context>] [-B <pre-context>] [-C <context>]
19
[-f <file>] [-e] <pattern> [--and|--or|--not|(|)|-e <pattern>...]
20
[<tree>...]
@@ -96,6 +96,11 @@ OPTIONS
96
higher precedence than `--or`. `-e` has to be used for all
97
patterns.
98
99
+--all-match::
100
+ When giving multiple pattern expressions combined with `--or`,
101
+ this flag is specified to limit the match to files that
102
+ have lines to match all of them.
103
+
104
`<tree>...`::
105
Search blobs in the trees for specified patterns.
106
@@ -111,6 +116,10 @@ git grep -e \'#define\' --and \( -e MAX_PATH -e PATH_MAX \)::
116
Looks for a line that has `#define` and either `MAX_PATH` or
117
`PATH_MAX`.
118
119
+git grep --all-match -e NODE -e Unexpected::
120
+ Looks for a line that has `NODE` or `Unexpected` in
121
+ files that have lines that match both.
122
+
123
Author
124
------
125
Originally written by Linus Torvalds <torvalds@osdl.org>, later
builtin-grep.c
+4
@@ -596,6 +596,10 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
596
GREP_CLOSE_PAREN);
597
continue;
598
}
599
+ if (!strcmp("--all-match", arg)) {
600
+ opt.all_match = 1;
601
+ continue;
602
+ }
603
if (!strcmp("-e", arg)) {
604
if (1 < argc) {
605
append_grep_pattern(&opt, argv[1],
grep.c
+96
-18
@@ -34,7 +34,7 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
34
}
35
}
36
37
-static struct grep_expr *compile_pattern_expr(struct grep_pat **);
37
+static struct grep_expr *compile_pattern_or(struct grep_pat **);
38
static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
39
{
40
struct grep_pat *p;
@@ -52,7 +52,7 @@ static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
52
return x;
53
case GREP_OPEN_PAREN:
54
*list = p->next;
55
- x = compile_pattern_expr(list);
55
+ x = compile_pattern_or(list);
56
if (!x)
57
return NULL;
58
if (!*list || (*list)->token != GREP_CLOSE_PAREN)
@@ -138,6 +138,9 @@ void compile_grep_patterns(struct grep_opt *opt)
138
{
139
struct grep_pat *p;
140
141
+ if (opt->all_match)
142
+ opt->extended = 1;
143
+
144
for (p = opt->pattern_list; p; p = p->next) {
145
switch (p->token) {
146
case GREP_PATTERN: /* atom */
@@ -309,40 +312,63 @@ static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol
312
return hit;
313
}
314
312
-static int match_expr_eval(struct grep_opt *opt,
315
+static int match_expr_eval(struct grep_opt *o,
316
struct grep_expr *x,
317
char *bol, char *eol,
315
- enum grep_context ctx)
318
+ enum grep_context ctx,
319
+ int collect_hits)
320
{
321
+ int h = 0;
322
+
323
switch (x->node) {
324
case GREP_NODE_ATOM:
319
- return match_one_pattern(opt, x->u.atom, bol, eol, ctx);
325
+ h = match_one_pattern(o, x->u.atom, bol, eol, ctx);
326
break;
327
case GREP_NODE_NOT:
322
- return !match_expr_eval(opt, x->u.unary, bol, eol, ctx);
328
+ h = !match_expr_eval(o, x->u.unary, bol, eol, ctx, 0);
329
+ break;
330
case GREP_NODE_AND:
324
- return (match_expr_eval(opt, x->u.binary.left, bol, eol, ctx) &&
325
- match_expr_eval(opt, x->u.binary.right, bol, eol, ctx));
331
+ if (!collect_hits)
332
+ return (match_expr_eval(o, x->u.binary.left,
333
+ bol, eol, ctx, 0) &&
334
+ match_expr_eval(o, x->u.binary.right,
335
+ bol, eol, ctx, 0));
336
+ h = match_expr_eval(o, x->u.binary.left, bol, eol, ctx, 0);
337
+ h &= match_expr_eval(o, x->u.binary.right, bol, eol, ctx, 0);
338
+ break;
339
case GREP_NODE_OR:
327
- return (match_expr_eval(opt, x->u.binary.left, bol, eol, ctx) ||
328
- match_expr_eval(opt, x->u.binary.right, bol, eol, ctx));
340
+ if (!collect_hits)
341
+ return (match_expr_eval(o, x->u.binary.left,
342
+ bol, eol, ctx, 0) ||
343
+ match_expr_eval(o, x->u.binary.right,
344
+ bol, eol, ctx, 0));
345
+ h = match_expr_eval(o, x->u.binary.left, bol, eol, ctx, 0);
346
+ x->u.binary.left->hit |= h;
347
+ h |= match_expr_eval(o, x->u.binary.right, bol, eol, ctx, 1);
348
+ break;
349
+ default:
350
+ die("Unexpected node type (internal error) %d\n", x->node);
351
}
330
- die("Unexpected node type (internal error) %d\n", x->node);
352
+ if (collect_hits)
353
+ x->hit |= h;
354
+ return h;
355
}
356
357
static int match_expr(struct grep_opt *opt, char *bol, char *eol,
334
- enum grep_context ctx)
358
+ enum grep_context ctx, int collect_hits)
359
{
360
struct grep_expr *x = opt->pattern_expression;
337
- return match_expr_eval(opt, x, bol, eol, ctx);
361
+ return match_expr_eval(opt, x, bol, eol, ctx, collect_hits);
362
}
363
364
static int match_line(struct grep_opt *opt, char *bol, char *eol,
341
- enum grep_context ctx)
365
+ enum grep_context ctx, int collect_hits)
366
{
367
struct grep_pat *p;
368
if (opt->extended)
345
- return match_expr(opt, bol, eol, ctx);
369
+ return match_expr(opt, bol, eol, ctx, collect_hits);
370
+
371
+ /* we do not call with collect_hits without being extended */
372
for (p = opt->pattern_list; p; p = p->next) {
373
if (match_one_pattern(opt, p, bol, eol, ctx))
374
return 1;
@@ -350,7 +376,8 @@ static int match_line(struct grep_opt *opt, char *bol, char *eol,
376
return 0;
377
}
378
353
-int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size)
379
+static int grep_buffer_1(struct grep_opt *opt, const char *name,
380
+ char *buf, unsigned long size, int collect_hits)
381
{
382
char *bol = buf;
383
unsigned long left = size;
@@ -386,7 +413,7 @@ int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long
413
414
while (left) {
415
char *eol, ch;
389
- int hit = 0;
416
+ int hit;
417
418
eol = end_of_line(bol, &left);
419
ch = *eol;
@@ -395,9 +422,12 @@ int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long
422
if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
423
ctx = GREP_CONTEXT_BODY;
424
398
- hit = match_line(opt, bol, eol, ctx);
425
+ hit = match_line(opt, bol, eol, ctx, collect_hits);
426
*eol = ch;
427
428
+ if (collect_hits)
429
+ goto next_line;
430
+
431
/* "grep -v -e foo -e bla" should list lines
432
* that do not have either, so inversion should
433
* be done outside.
@@ -477,6 +507,8 @@ int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long
507
}
508
509
free(prev);
510
+ if (collect_hits)
511
+ return 0;
512
513
if (opt->status_only)
514
return 0;
@@ -496,3 +528,49 @@ int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long
528
return !!last_hit;
529
}
530
531
+static void clr_hit_marker(struct grep_expr *x)
532
+{
533
+ /* All-hit markers are meaningful only at the very top level
534
+ * OR node.
535
+ */
536
+ while (1) {
537
+ x->hit = 0;
538
+ if (x->node != GREP_NODE_OR)
539
+ return;
540
+ x->u.binary.left->hit = 0;
541
+ x = x->u.binary.right;
542
+ }
543
+}
544
+
545
+static int chk_hit_marker(struct grep_expr *x)
546
+{
547
+ /* Top level nodes have hit markers. See if they all are hits */
548
+ while (1) {
549
+ if (x->node != GREP_NODE_OR)
550
+ return x->hit;
551
+ if (!x->u.binary.left->hit)
552
+ return 0;
553
+ x = x->u.binary.right;
554
+ }
555
+}
556
+
557
+int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size)
558
+{
559
+ /*
560
+ * we do not have to do the two-pass grep when we do not check
561
+ * buffer-wide "all-match".
562
+ */
563
+ if (!opt->all_match)
564
+ return grep_buffer_1(opt, name, buf, size, 0);
565
+
566
+ /* Otherwise the toplevel "or" terms hit a bit differently.
567
+ * We first clear hit markers from them.
568
+ */
569
+ clr_hit_marker(opt->pattern_expression);
570
+ grep_buffer_1(opt, name, buf, size, 1);
571
+
572
+ if (!chk_hit_marker(opt->pattern_expression))
573
+ return 0;
574
+
575
+ return grep_buffer_1(opt, name, buf, size, 0);
576
+}
grep.h
+2
@@ -35,6 +35,7 @@ enum grep_expr_node {
35
36
struct grep_expr {
37
enum grep_expr_node node;
38
+ unsigned hit;
39
union {
40
struct grep_pat *atom;
41
struct grep_expr *unary;
@@ -59,6 +60,7 @@ struct grep_opt {
60
unsigned count:1;
61
unsigned word_regexp:1;
62
unsigned fixed:1;
63
+ unsigned all_match:1;
64
#define GREP_BINARY_DEFAULT 0
65
#define GREP_BINARY_NOMATCH 1
66
#define GREP_BINARY_TEXT 2