Merge branch 'nd/grep-reflog'
authorJunio C Hamano <gitster@pobox.com>
Wed, 3 Oct 2012 04:13:39 +0000 (21:13 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 3 Oct 2012 04:13:39 +0000 (21:13 -0700)
Teach the commands from the "log" family the "--grep-reflog" option
to limit output by string that appears in the reflog entry when the
"--walk-reflogs" option is in effect.

* nd/grep-reflog:
revision: make --grep search in notes too if shown
log --grep-reflog: reject the option without -g
revision: add --grep-reflog to filter commits by reflog messages
grep: prepare for new header field filter

Documentation/rev-list-options.txt
grep.c
grep.h
revision.c
t/t7810-grep.sh
index 1fc2a18404bd34a2e1ac9f5fe21d64c1827f13a6..ee497430cb0f8dc0859b90b8773e6b8e28d5e361 100644 (file)
@@ -51,6 +51,14 @@ endif::git-rev-list[]
        commits whose author matches any of the given patterns are
        chosen (similarly for multiple `--committer=<pattern>`).
 
+--grep-reflog=<pattern>::
+
+       Limit the commits output to ones with reflog entries that
+       match the specified pattern (regular expression). With
+       more than one `--grep-reflog`, commits whose reflog message
+       matches any of the given patterns are chosen.  It is an
+       error to use this option unless `--walk-reflogs` is in use.
+
 --grep=<pattern>::
 
        Limit the commits output to ones with log message that
@@ -58,6 +66,9 @@ endif::git-rev-list[]
        more than one `--grep=<pattern>`, commits whose message
        matches any of the given patterns are chosen (but see
        `--all-match`).
++
+When `--show-notes` is in effect, the message from the notes as
+if it is part of the log message.
 
 --all-match::
        Limit the commits output to ones that match all given --grep,
diff --git a/grep.c b/grep.c
index 898be6ebfaf2a4247ee3a64993253237b4dcc706..edc7776677ed201dbe117c207a680e1f832737e3 100644 (file)
--- a/grep.c
+++ b/grep.c
@@ -64,6 +64,8 @@ void append_header_grep_pattern(struct grep_opt *opt,
 {
        struct grep_pat *p = create_grep_pat(pat, strlen(pat), "header", 0,
                                             GREP_PATTERN_HEAD, field);
+       if (field == GREP_HEADER_REFLOG)
+               opt->use_reflog_filter = 1;
        do_append_grep_pat(&opt->header_tail, p);
 }
 
@@ -697,6 +699,7 @@ static struct {
 } header_field[] = {
        { "author ", 7 },
        { "committer ", 10 },
+       { "reflog ", 7 },
 };
 
 static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
@@ -720,7 +723,14 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
                if (strncmp(bol, field, len))
                        return 0;
                bol += len;
-               saved_ch = strip_timestamp(bol, &eol);
+               switch (p->field) {
+               case GREP_HEADER_AUTHOR:
+               case GREP_HEADER_COMMITTER:
+                       saved_ch = strip_timestamp(bol, &eol);
+                       break;
+               default:
+                       break;
+               }
        }
 
  again:
diff --git a/grep.h b/grep.h
index 8a28a676fc3b16a0857e3f4b80824d929b6b444d..c256ac6e15b22e153623ae3395fb6ffc732b20b9 100644 (file)
--- a/grep.h
+++ b/grep.h
@@ -29,9 +29,12 @@ enum grep_context {
 
 enum grep_header_field {
        GREP_HEADER_AUTHOR = 0,
-       GREP_HEADER_COMMITTER
+       GREP_HEADER_COMMITTER,
+       GREP_HEADER_REFLOG,
+
+       /* Must be at the end of the enum */
+       GREP_HEADER_FIELD_MAX
 };
-#define GREP_HEADER_FIELD_MAX (GREP_HEADER_COMMITTER + 1)
 
 struct grep_pat {
        struct grep_pat *next;
@@ -104,6 +107,7 @@ struct grep_opt {
 #define GREP_BINARY_TEXT       2
        int binary;
        int extended;
+       int use_reflog_filter;
        int pcre;
        int relative;
        int pathname;
index ae12e11fb74dcc51bfddd2acf94601b2bfba7811..a09e60bedbbbf2c6bdeeacc3d1032bcc1be9cb55 100644 (file)
@@ -1595,6 +1595,9 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
        } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
                add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
                return argcount;
+       } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
+               add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
+               return argcount;
        } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
                add_message_grep(revs, optarg);
                return argcount;
@@ -1905,6 +1908,8 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
 
        if (revs->reflog_info && revs->graph)
                die("cannot combine --walk-reflogs with --graph");
+       if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
+               die("cannot use --grep-reflog without --walk-reflogs");
 
        return left;
 }
@@ -2210,10 +2215,38 @@ static int rewrite_parents(struct rev_info *revs, struct commit *commit)
 
 static int commit_match(struct commit *commit, struct rev_info *opt)
 {
+       int retval;
+       struct strbuf buf = STRBUF_INIT;
        if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
                return 1;
-       return grep_buffer(&opt->grep_filter,
-                          commit->buffer, strlen(commit->buffer));
+
+       /* Prepend "fake" headers as needed */
+       if (opt->grep_filter.use_reflog_filter) {
+               strbuf_addstr(&buf, "reflog ");
+               get_reflog_message(&buf, opt->reflog_info);
+               strbuf_addch(&buf, '\n');
+       }
+
+       /* Copy the commit to temporary if we are using "fake" headers */
+       if (buf.len)
+               strbuf_addstr(&buf, commit->buffer);
+
+       /* Append "fake" message parts as needed */
+       if (opt->show_notes) {
+               if (!buf.len)
+                       strbuf_addstr(&buf, commit->buffer);
+               format_display_notes(commit->object.sha1, &buf,
+                                    get_log_output_encoding(), 0);
+       }
+
+       /* Find either in the commit object, or in the temporary */
+       if (buf.len)
+               retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
+       else
+               retval = grep_buffer(&opt->grep_filter,
+                                    commit->buffer, strlen(commit->buffer));
+       strbuf_release(&buf);
+       return retval;
 }
 
 static inline int want_ancestry(struct rev_info *revs)
index 91db352cc7ed5faa24eb27542d5ce8c4fe622303..f698001c996ea20847c574f04dad9a65684f324f 100755 (executable)
@@ -546,6 +546,36 @@ test_expect_success 'log grep (6)' '
        test_cmp expect actual
 '
 
+test_expect_success 'log grep (7)' '
+       git log -g --grep-reflog="commit: third" --pretty=tformat:%s >actual &&
+       echo third >expect &&
+       test_cmp expect actual
+'
+
+test_expect_success 'log grep (8)' '
+       git log -g --grep-reflog="commit: third" --grep-reflog="commit: second" --pretty=tformat:%s >actual &&
+       {
+               echo third && echo second
+       } >expect &&
+       test_cmp expect actual
+'
+
+test_expect_success 'log grep (9)' '
+       git log -g --grep-reflog="commit: third" --author="Thor" --pretty=tformat:%s >actual &&
+       echo third >expect &&
+       test_cmp expect actual
+'
+
+test_expect_success 'log grep (9)' '
+       git log -g --grep-reflog="commit: third" --author="non-existant" --pretty=tformat:%s >actual &&
+       : >expect &&
+       test_cmp expect actual
+'
+
+test_expect_success 'log --grep-reflog can only be used under -g' '
+       test_must_fail git log --grep-reflog="commit: third"
+'
+
 test_expect_success 'log with multiple --grep uses union' '
        git log --grep=i --grep=r --format=%s >actual &&
        {
@@ -628,6 +658,18 @@ test_expect_success 'log --all-match --grep --grep --author takes intersection'
        test_cmp expect actual
 '
 
+test_expect_success 'log --author does not search in timestamp' '
+       : >expect &&
+       git log --author="$GIT_AUTHOR_DATE" >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'log --committer does not search in timestamp' '
+       : >expect &&
+       git log --committer="$GIT_COMMITTER_DATE" >actual &&
+       test_cmp expect actual
+'
+
 test_expect_success 'grep with CE_VALID file' '
        git update-index --assume-unchanged t/t &&
        rm t/t &&