Skip to content

Commit ad4813b

Browse files
pcloudsgitster
authored andcommitted
grep: prepare for new header field filter
grep supports only author and committer headers, which have the same special treatment that later headers may or may not have. Check for field type and only strip_timestamp() when the field is either author or committer. GREP_HEADER_FIELD_MAX is put in the grep_header_field enum to be calculated automatically, correctly, as long as it's at the end of the enum. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 652398a commit ad4813b

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

grep.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,14 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
720720
if (strncmp(bol, field, len))
721721
return 0;
722722
bol += len;
723-
saved_ch = strip_timestamp(bol, &eol);
723+
switch (p->field) {
724+
case GREP_HEADER_AUTHOR:
725+
case GREP_HEADER_COMMITTER:
726+
saved_ch = strip_timestamp(bol, &eol);
727+
break;
728+
default:
729+
break;
730+
}
724731
}
725732

726733
again:

grep.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ enum grep_context {
2929

3030
enum grep_header_field {
3131
GREP_HEADER_AUTHOR = 0,
32-
GREP_HEADER_COMMITTER
32+
GREP_HEADER_COMMITTER,
33+
34+
/* Must be at the end of the enum */
35+
GREP_HEADER_FIELD_MAX
3336
};
34-
#define GREP_HEADER_FIELD_MAX (GREP_HEADER_COMMITTER + 1)
3537

3638
struct grep_pat {
3739
struct grep_pat *next;

t/t7810-grep.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,18 @@ test_expect_success 'log --all-match --grep --grep --author takes intersection'
628628
test_cmp expect actual
629629
'
630630

631+
test_expect_success 'log --author does not search in timestamp' '
632+
: >expect &&
633+
git log --author="$GIT_AUTHOR_DATE" >actual &&
634+
test_cmp expect actual
635+
'
636+
637+
test_expect_success 'log --committer does not search in timestamp' '
638+
: >expect &&
639+
git log --committer="$GIT_COMMITTER_DATE" >actual &&
640+
test_cmp expect actual
641+
'
642+
631643
test_expect_success 'grep with CE_VALID file' '
632644
git update-index --assume-unchanged t/t &&
633645
rm t/t &&

0 commit comments

Comments
 (0)