git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>, "Jeff King" <peff@peff.net>,
	"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH 2/3] revision: add --grep-reflog to filter commits by reflog messages
Date: Fri, 28 Sep 2012 14:01:21 +0700	[thread overview]
Message-ID: <1348815682-18696-2-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <1348815682-18696-1-git-send-email-pclouds@gmail.com>

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.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 On Fri, Sep 28, 2012 at 12:09 AM, Junio C Hamano <gitster@pobox.com>
 wrote:
 > I am debating myself if it is sane for this option to have no hint
 > that it is about "limiting" in its name.  "--author/--committer"
 > don't and it is clear from the context of the command that they are
 > not about setting author/committer, so "--reflog-message" may be
 > interpreted the same, perhaps.
 >
 > The entry in the context above talks about multiple occurrence of
 > that option. Shouldn't this new one also say what happens when it
 > is
 > given twice?

 Fixed.

 > I think I wrote prep_header_patterns() and compile_grep_patterns()
 > carefully enough not to assume the headers are only the author and
 > committer names, so the various combinations i.e. all-match,
 > author(s), committer(s), grep(s), and reflog-message(s), should
 > work
 > out of the box, but have you actually tested them?

 I did not. I do now, tests are also added.

 On Fri, Sep 28, 2012 at 12:28 AM, Jeff King <peff@peff.net> wrote:
 > I also found the name confusing on first-read. While "--author" is
 > an
 > example in one direction, the fact that "--grep" is not called
 > "--body"
 > is a counter-example.
 >
 > I'd much rather see it as "--grep-reflog" or something. You could
 > also
 > do "--grep-reflog-message", which would match a later
 > "--grep-reflog-author", but I am not sure anybody would want the
 > latter,
 > and it makes the current name a lot longer.

 Changed it to --grep-reflog. I was tempted to add --grep-* but I
 can't error out if user gives an invalid header. So --grep-reflog
 only for now.

 Documentation/rev-list-options.txt |  8 ++++++++
 revision.c                         | 20 ++++++++++++++++++--
 t/t7810-grep.sh                    | 26 ++++++++++++++++++++++++++
 3 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/Documentation/rev-list-options.txt b/Documentation/rev-list-options.txt
index 1fc2a18..aa7cd9d 100644
--- a/Documentation/rev-list-options.txt
+++ b/Documentation/rev-list-options.txt
@@ -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. Ignored unless
+	`--walk-reflogs` is given.
+
 --grep=<pattern>::
 
 	Limit the commits output to ones with log message that
diff --git a/revision.c b/revision.c
index f7cf385..cfa0e2e 100644
--- a/revision.c
+++ b/revision.c
@@ -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, "committer", optarg);
 		return argcount;
+	} else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
+		add_header_grep(revs, "reflog", optarg);
+		return argcount;
 	} else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
 		add_message_grep(revs, optarg);
 		return argcount;
@@ -2210,10 +2213,23 @@ 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));
+	if (opt->reflog_info) {
+		strbuf_addstr(&buf, "reflog ");
+		get_reflog_message(&buf, opt->reflog_info);
+		strbuf_addch(&buf, '\n');
+		strbuf_addstr(&buf, commit->buffer);
+	}
+	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)
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index 91db352..f42a605 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -546,6 +546,32 @@ 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 with multiple --grep uses union' '
 	git log --grep=i --grep=r --format=%s >actual &&
 	{
-- 
1.7.12.1.405.gb727dc9

  reply	other threads:[~2012-09-28  7:02 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-26 12:12 [PATCH] revision: add --reflog-message=<pattern> to grep reflog messages Nguyễn Thái Ngọc Duy
2012-09-26 14:07 ` Junio C Hamano
2012-09-26 14:15   ` Nguyen Thai Ngoc Duy
2012-09-26 19:28     ` Junio C Hamano
2012-09-27 11:36       ` [PATCH] revision: add --reflog-message " Nguyễn Thái Ngọc Duy
2012-09-27 17:09         ` Junio C Hamano
2012-09-27 17:28           ` Jeff King
2012-09-27 18:16             ` Junio C Hamano
2012-09-28  7:01         ` [PATCH 1/3] grep: generalize header grep code to accept arbitrary headers Nguyễn Thái Ngọc Duy
2012-09-28  7:01           ` Nguyễn Thái Ngọc Duy [this message]
2012-09-28 17:55             ` [PATCH 2/3] revision: add --grep-reflog to filter commits by reflog messages Junio C Hamano
2012-09-29  4:41               ` Nguyễn Thái Ngọc Duy
2012-09-29  4:41                 ` [PATCH 1/3] grep: prepare for new header field filter Nguyễn Thái Ngọc Duy
2012-09-29  5:22                   ` Jeff King
2012-09-29  4:41                 ` [PATCH 2/3] revision: add --grep-reflog to filter commits by reflog messages Nguyễn Thái Ngọc Duy
2012-09-29  5:30                   ` Jeff King
2012-09-29  5:54                     ` Junio C Hamano
2012-09-29  6:13                       ` Nguyen Thai Ngoc Duy
2012-09-29 19:11                         ` Junio C Hamano
2012-09-30  3:45                           ` Nguyen Thai Ngoc Duy
2012-09-29  6:16                       ` Jeff King
2012-09-29  4:41                 ` [PATCH 3/3] revision: make --grep search in notes too if shown Nguyễn Thái Ngọc Duy
2012-09-29  5:35                 ` [PATCH 2/3] revision: add --grep-reflog to filter commits by reflog messages Junio C Hamano
2012-09-28  7:01           ` [PATCH 3/3] revision: make --grep search in notes too if shown Nguyễn Thái Ngọc Duy
2012-09-28 17:55             ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1348815682-18696-2-git-send-email-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).