Fix "grep -w"
We used to find the first match of the pattern and then if the match is not for the entire word, declared that the whole line does not match. But that is wrong. The command "git grep -w -e mmap" should find that a line "foo_mmap bar mmap baz" matches, by tring the second instance of pattern "mmap" on the same line. Problems an earlier round of "fix" had were pointed out by Morten Welinder, which have been incorporated in the t7002 tests. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Aug 4, 2006 at 22:16 UTC
f25b79397c9775df9eeef3d59d0cc3b1f913bc60
2 files changed
+110
-10
builtin-grep.c
+25
-10
@@ -410,8 +410,10 @@ static int fixmatch(const char *pattern, char *line, regmatch_t *match)
410
static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol, char *eol)
411
{
412
int hit = 0;
413
+ int at_true_bol = 1;
414
regmatch_t pmatch[10];
415
416
+ again:
417
if (!opt->fixed) {
418
regex_t *exp = &p->regexp;
419
hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
@@ -422,22 +424,35 @@ static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol
424
}
425
426
if (hit && opt->word_regexp) {
425
- /* Match beginning must be either
426
- * beginning of the line, or at word
427
- * boundary (i.e. the last char must
428
- * not be alnum or underscore).
429
- */
427
if ((pmatch[0].rm_so < 0) ||
428
(eol - bol) <= pmatch[0].rm_so ||
429
(pmatch[0].rm_eo < 0) ||
430
(eol - bol) < pmatch[0].rm_eo)
431
die("regexp returned nonsense");
435
- if (pmatch[0].rm_so != 0 &&
436
- word_char(bol[pmatch[0].rm_so-1]))
437
- hit = 0;
438
- if (pmatch[0].rm_eo != (eol-bol) &&
439
- word_char(bol[pmatch[0].rm_eo]))
432
+
433
+ /* Match beginning must be either beginning of the
434
+ * line, or at word boundary (i.e. the last char must
435
+ * not be a word char). Similarly, match end must be
436
+ * either end of the line, or at word boundary
437
+ * (i.e. the next char must not be a word char).
438
+ */
439
+ if ( ((pmatch[0].rm_so == 0 && at_true_bol) ||
440
+ !word_char(bol[pmatch[0].rm_so-1])) &&
441
+ ((pmatch[0].rm_eo == (eol-bol)) ||
442
+ !word_char(bol[pmatch[0].rm_eo])) )
443
+ ;
444
+ else
445
hit = 0;
446
+
447
+ if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
448
+ /* There could be more than one match on the
449
+ * line, and the first match might not be
450
+ * strict word match. But later ones could be!
451
+ */
452
+ bol = pmatch[0].rm_so + bol + 1;
453
+ at_true_bol = 0;
454
+ goto again;
455
+ }
456
}
457
return hit;
458
}
t/t7002-grep.sh
new
+85
@@ -0,0 +1,85 @@
1
+#!/bin/sh
2
+#
3
+# Copyright (c) 2006 Junio C Hamano
4
+#
5
+
6
+test_description='git grep -w
7
+'
8
+
9
+. ./test-lib.sh
10
+
11
+test_expect_success setup '
12
+ {
13
+ echo foo mmap bar
14
+ echo foo_mmap bar
15
+ echo foo_mmap bar mmap
16
+ echo foo mmap bar_mmap
17
+ echo foo_mmap bar mmap baz
18
+ } >file &&
19
+ echo x x xx x >x &&
20
+ echo y yy >y &&
21
+ echo zzz > z &&
22
+ git add file x y z &&
23
+ git commit -m initial
24
+'
25
+
26
+for H in HEAD ''
27
+do
28
+ case "$H" in
29
+ HEAD) HC='HEAD:' L='HEAD' ;;
30
+ '') HC= L='in working tree' ;;
31
+ esac
32
+
33
+ test_expect_success "grep -w $L" '
34
+ {
35
+ echo ${HC}file:1:foo mmap bar
36
+ echo ${HC}file:3:foo_mmap bar mmap
37
+ echo ${HC}file:4:foo mmap bar_mmap
38
+ echo ${HC}file:5:foo_mmap bar mmap baz
39
+ } >expected &&
40
+ git grep -n -w -e mmap $H >actual &&
41
+ diff expected actual
42
+ '
43
+
44
+ test_expect_success "grep -w $L (x)" '
45
+ {
46
+ echo ${HC}x:1:x x xx x
47
+ } >expected &&
48
+ git grep -n -w -e "x xx* x" $H >actual &&
49
+ diff expected actual
50
+ '
51
+
52
+ test_expect_success "grep -w $L (y-1)" '
53
+ {
54
+ echo ${HC}y:1:y yy
55
+ } >expected &&
56
+ git grep -n -w -e "^y" $H >actual &&
57
+ diff expected actual
58
+ '
59
+
60
+ test_expect_success "grep -w $L (y-2)" '
61
+ : >expected &&
62
+ if git grep -n -w -e "^y y" $H >actual
63
+ then
64
+ echo should not have matched
65
+ cat actual
66
+ false
67
+ else
68
+ diff expected actual
69
+ fi
70
+ '
71
+
72
+ test_expect_success "grep -w $L (z)" '
73
+ : >expected &&
74
+ if git grep -n -w -e "^z" $H >actual
75
+ then
76
+ echo should not have matched
77
+ cat actual
78
+ false
79
+ else
80
+ diff expected actual
81
+ fi
82
+ '
83
+done
84
+
85
+test_done