Skip to content

Commit 72fd13f

Browse files
pcloudsgitster
authored andcommitted
revision: add --grep-reflog to filter commits by reflog messages
Similar to --author/--committer which filters commits by author and committer header fields. --grep-reflog adds a fake "reflog" header to commit and a grep filter to search on that line. All rules to --author/--committer apply except no timestamp stripping. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent ad4813b commit 72fd13f

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

Documentation/rev-list-options.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ endif::git-rev-list[]
5151
commits whose author matches any of the given patterns are
5252
chosen (similarly for multiple `--committer=<pattern>`).
5353

54+
--grep-reflog=<pattern>::
55+
56+
Limit the commits output to ones with reflog entries that
57+
match the specified pattern (regular expression). With
58+
more than one `--grep-reflog`, commits whose reflog message
59+
matches any of the given patterns are chosen. Ignored unless
60+
`--walk-reflogs` is given.
61+
5462
--grep=<pattern>::
5563

5664
Limit the commits output to ones with log message that

grep.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ static struct {
697697
} header_field[] = {
698698
{ "author ", 7 },
699699
{ "committer ", 10 },
700+
{ "reflog ", 7 },
700701
};
701702

702703
static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,

grep.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ enum grep_context {
3030
enum grep_header_field {
3131
GREP_HEADER_AUTHOR = 0,
3232
GREP_HEADER_COMMITTER,
33+
GREP_HEADER_REFLOG,
3334

3435
/* Must be at the end of the enum */
3536
GREP_HEADER_FIELD_MAX

revision.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,9 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
15951595
} else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
15961596
add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
15971597
return argcount;
1598+
} else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
1599+
add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
1600+
return argcount;
15981601
} else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
15991602
add_message_grep(revs, optarg);
16001603
return argcount;
@@ -2210,10 +2213,23 @@ static int rewrite_parents(struct rev_info *revs, struct commit *commit)
22102213

22112214
static int commit_match(struct commit *commit, struct rev_info *opt)
22122215
{
2216+
int retval;
2217+
struct strbuf buf = STRBUF_INIT;
22132218
if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
22142219
return 1;
2215-
return grep_buffer(&opt->grep_filter,
2216-
commit->buffer, strlen(commit->buffer));
2220+
if (opt->reflog_info) {
2221+
strbuf_addstr(&buf, "reflog ");
2222+
get_reflog_message(&buf, opt->reflog_info);
2223+
strbuf_addch(&buf, '\n');
2224+
strbuf_addstr(&buf, commit->buffer);
2225+
}
2226+
if (buf.len)
2227+
retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
2228+
else
2229+
retval = grep_buffer(&opt->grep_filter,
2230+
commit->buffer, strlen(commit->buffer));
2231+
strbuf_release(&buf);
2232+
return retval;
22172233
}
22182234

22192235
static inline int want_ancestry(struct rev_info *revs)

t/t7810-grep.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,32 @@ test_expect_success 'log grep (6)' '
546546
test_cmp expect actual
547547
'
548548

549+
test_expect_success 'log grep (7)' '
550+
git log -g --grep-reflog="commit: third" --pretty=tformat:%s >actual &&
551+
echo third >expect &&
552+
test_cmp expect actual
553+
'
554+
555+
test_expect_success 'log grep (8)' '
556+
git log -g --grep-reflog="commit: third" --grep-reflog="commit: second" --pretty=tformat:%s >actual &&
557+
{
558+
echo third && echo second
559+
} >expect &&
560+
test_cmp expect actual
561+
'
562+
563+
test_expect_success 'log grep (9)' '
564+
git log -g --grep-reflog="commit: third" --author="Thor" --pretty=tformat:%s >actual &&
565+
echo third >expect &&
566+
test_cmp expect actual
567+
'
568+
569+
test_expect_success 'log grep (9)' '
570+
git log -g --grep-reflog="commit: third" --author="non-existant" --pretty=tformat:%s >actual &&
571+
: >expect &&
572+
test_cmp expect actual
573+
'
574+
549575
test_expect_success 'log with multiple --grep uses union' '
550576
git log --grep=i --grep=r --format=%s >actual &&
551577
{

0 commit comments

Comments
 (0)