Skip to contentSkip to conversation

revision traversal: --author, --committer, and --grep.

PATCH In review Junio C Hamano 3 participants 5 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 3/3 revision traversal: --author, --committer, and --grep.

This adds three options to setup_revisions(), which lets you filter resulting commits by the author name, the committer name and the log message with regexp.

---
 Documentation/git-rev-list.txt |   11 ++++++
 revision.c                     |   76 +++++++++++++++++++++++++++++++++++++++-
 2 files changed, 86 insertions(+), 1 deletions(-)
Documentation/git-rev-list.txt +11 -0
index 28966ad..00a95e2 100644
@@ -20,6 +20,7 @@ SYNOPSIS
 	     [ \--stdin ]
 	     [ \--topo-order ]
 	     [ \--parents ]
+	     [ \--(author|committer|grep)=<pattern> ]
 	     [ [\--objects | \--objects-edge] [ \--unpacked ] ]
 	     [ \--pretty | \--header ]
 	     [ \--bisect ]
@@ -154,6 +155,16 @@ limiting may be applied.
 
 	Limit the commits output to specified time range.
 
+--author='pattern', --committer='pattern'::
+
+	Limit the commits output to ones with author/committer
+	header lines that match the specified pattern.
+
+--grep='pattern'::
+
+	Limit the commits output to ones with log message that
+	matches the specified pattern.
+
 --remove-empty::
 
 	Stop when a given path disappears from the tree.
revision.c +75 -1

103 lines not on this page · raw message

Signature · 1 line
1.4.2.1.g414e5
#1 lore raw
Quoted 3 lines · Junio C Hamano

This adds three options to setup_revisions(), which lets you filter resulting commits by the author name, the committer name and the log message with regexp.

First of all, thanks for implementing this; I tried to use it the other day (remembering the discussion and patches a few weeks ago) and was disappointed to find it absent.

That being said, I find the matching style completely unintuitive. :)

To find --author=foo, your strategy is to stringify the header and grep for "^author foo". As a user, my expectation was that you would stringify the author field and grep for "foo".

The important difference is that your approach means that the user's regex is implicitly anchored at the beginning of the field. Thus, searching by email address does not work with --author=junkio, but rather requires --author='.*junkio'.

Possible fixes:
1. Match against "^<field>.*<regex>" (I haven't looked closely at the builtin grep implementation, but presumably '.' as usual does not include newline).
2. Find <field>, and then feed grep_buffer only the contents of that line.
The second is what I feel that users will expect (at least what I expected!), but is probably slightly less efficient (two greps instead of one, but I doubt the difference would be significant). However, I don't think there is a way with the first approach to explicitly request a beginning-of-string anchor (i.e., "^Junio" in the second approach).

A few other thoughts:
1. Case sensitivity? For convenience sake, it seems reasonable to match these fields without case sensitivity (what was the capitalization of A Large Angry SCM again? von Brand or Von Brand? etc). Should it be optional, and if so, how to specify it (a global command line option is probably not desired, as you might want case-sensitive --grep but case-insensitive --author). So we either need a "-i means the rest of the arguments are insensitive, +i means they are sensitive" option, or some syntax to specify it in the regex (perl uses (?i)).
2. Is there any use to exposing the "header_grep" functionality with --grep-header? Is there anything worth grepping for besides author/committer? The general consensus on non-core headers in commit objects seemed to be "don't do it".
3. An alias (--who=foo?) for --author=foo --committer=foo. I believe this doesn't require boolean magic, since we default to OR.

I'm happy to work on implementing any of the above if there's interest.

-Peff

#2 lore raw
Quoted 4 lines · Jeff King

The important difference is that your approach means that the user's regex is implicitly anchored at the beginning of the field. Thus, searching by email address does not work with --author=junkio, but rather requires --author='.*junkio'.

I wanted to default it to left anchored, so this was somewhat deliberate, but this is probably subject to taste.

I actually wanted to do only --grep-header and --grep-message internally with --author and --committer as simple shorthands, and that is why --author=<pattern> is just an internal synonym for "author <pattern>". I even considered doing without distinction between header and body (meaning, a line in the commit log message that happens to begin with "author " would match --author string), which is technically incorrect but would be more efficient. I do not think occasional false hits would matter much in practice.

Not making the match case insensitive was probably a mistake. Looking for "--author=linus" would be easier to type.

One thing that I know is broken is the match at the tail; I am actually thinking of doing something like this:

	sprintf(pat, "^%s %s [0-9][0-9]* [+-][0-9][0-9][0-9][0-9]$",
		field, pattern);

to avoid matching the timestamps.

Quoted 2 lines

A few other thoughts:
1. Case sensitivity?

I do not think much is lost if we make the match always case insensitive for this application. It might be reasonable to to default to case insensitive match but if there is a pattern that has an uppercase use case sensitive match.

Quoted 2 lines

2. Is there any use to exposing the "header_grep" functionality with

    --grep-header?

I considered it but did not think of any. We obviously would need to revisit this if we do "note " header in the future, but not for now. On the other hand, it might be useful to allow

	--grep-header='^\(author\|committer\) Junio'
Quoted 2 lines

3. An alias (--who=foo?) for --author=foo --committer=foo. I believe this doesn't require boolean magic, since we default to OR.

This should be trivial to implement (two calls to add_header_grep instead of one), but would it be useful? I dunno.

#3 lore raw
Quoted 3 lines · Junio C Hamano

I wanted to default it to left anchored, so this was somewhat deliberate, but this is probably subject to taste.

I know that I'd prefer a rule where

"--author=^Junio"

would result in the grep-pattern being "^author Junio", but without the initial '^' it would be "^author .*Junio".

So something like this, perhaps? It allows the regular left anchoring syntax ('^' at the start of a pattern), but defaults to the default grep behaviour ("anywhere in the line").

Linus

---

revision.c +8 -2
index 26dd418..bca1229 100644
@@ -677,6 +677,7 @@ int handle_revision_arg(const char *arg,
 static void add_header_grep(struct rev_info *revs, const char *field, const char *pattern)
 {
 	char *pat;
+	const char *prefix;
 	int patlen, fldlen;
 
 	if (!revs->header_filter) {
@@ -689,8 +690,13 @@ static void add_header_grep(struct rev_i
 
 	fldlen = strlen(field);
 	patlen = strlen(pattern);
-	pat = xmalloc(patlen + fldlen + 3);
-	sprintf(pat, "^%s %s", field, pattern);
+	pat = xmalloc(patlen + fldlen + 10);
+	prefix = ".*";
+	if (*pattern == '^') {
+		prefix = "";
+		pattern++;
+	}
+	sprintf(pat, "^%s %s%s", field, prefix, pattern);
 	append_grep_pattern(revs->header_filter, pat,
 			    "command line", 0, GREP_PATTERN);
 }
#4 lore raw

PATCH 1/1 rev-list: fix segfault with --{author,committer,grep}

In reply to Junio C Hamano

We need to save the commit buffer if we're going to match against it.

---
 builtin-rev-list.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
builtin-rev-list.c +3 -1
index 1f3333d..dbfee75 100644
@@ -269,7 +269,9 @@ int cmd_rev_list(int argc, const char **
 	    revs.diff)
 		usage(rev_list_usage);
 
-	save_commit_buffer = revs.verbose_header;
+	save_commit_buffer = revs.verbose_header ||
+		revs.header_filter ||
+		revs.message_filter;
 	track_object_refs = 0;
 	if (bisect_list)
 		revs.limited = 1;
Signature · 1 line
1.4.2.1.g9f1c-dirty