threads / rfc / 23802

RFC patchMake :/ accept a regex rather than a fixed pattern

Subject: [RFC PATCH] Make :/ accept a regex rather than a fixed pattern

## tl;dr

2 messages between May 13, 2010 and Jun 18, 2010. Diffs are folded; open one to read it.

replies: 1people: 2as markdown or json

Linus Torvalds· May 13, 2010, 22:08 UTC · lore
From: Linus Torvalds <torvalds@linux-foundation.org>
Date: Fri, 23 Apr 2010 08:20:20 -0700
Subject: [PATCH] Make :/ accept a regex rather than a fixed pattern

This also makes it trigger anywhere in the commit message, rather than just at the beginning. Which tends to be a lot more useful.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
---

I'm re-sending, because after having this in my tree for several weeks now, I actually end up still using it.

I agree that it's still not a wonderful thing, and it's entirely possible that we should strive to use just HEAD in :/ commit finding rather than all refs we can find, but for at least the kernel, I don't care (since my tree tends to have just one main branch anyway, apart from some random testing stuff I keep around).

But even if we want to limit it to HEAD, that would be a separate patch.
I've personally used it for things like
	git show :/mqueue	# did I apply that 'mqueue' patch?
	git show :/akpm		# what was the last patch I got from Andrew?

and while in all cases I could admittedly have done exactly the same thing with something like "git log -p -1 --grep=mqueue" instead, I've used it as a nice shortcut.

Of course, I'm still not convinced that :/ was a good feature to begin with. If we had a short form of "--grep" ("-/"?), I would probably happily have written "git log -/akpm" instead.

So take this patch with a large pinch of salt. It's just not very important.

 sha1_name.c |   12 +++++++++---
 1 files changed, 9 insertions(+), 3 deletions(-)
Show changes to sha1_name.c +9 −3
diff --git a/sha1_name.c b/sha1_name.c
index bf92417..8cf635a 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -679,8 +679,8 @@ static int handle_one_ref(const char *path,
 
 /*
  * This interprets names like ':/Initial revision of "git"' by searching
- * through history and returning the first commit whose message starts
- * with the given string.
+ * through history and returning the first commit whose message matches
+ * the given regular expression.
  *
  * For future extension, ':/!' is reserved. If you want to match a message
  * beginning with a '!', you have to repeat the exclamation mark.
@@ -692,12 +692,17 @@ static int get_sha1_oneline(const char *prefix, unsigned char *sha1)
 	struct commit_list *list = NULL, *backup = NULL, *l;
 	int retval = -1;
 	char *temp_commit_buffer = NULL;
+	regex_t regex;
 
 	if (prefix[0] == '!') {
 		if (prefix[1] != '!')
 			die ("Invalid search pattern: %s", prefix);
 		prefix++;
 	}
+
+	if (regcomp(&regex, prefix, REG_EXTENDED))
+		die("Invalid search pattern: %s", prefix);
+
 	for_each_ref(handle_one_ref, &list);
 	for (l = list; l; l = l->next)
 		commit_list_insert(l->item, &backup);
@@ -721,12 +726,13 @@ static int get_sha1_oneline(const char *prefix, unsigned char *sha1)
 		}
 		if (!(p = strstr(p, "\n\n")))
 			continue;
-		if (!prefixcmp(p + 2, prefix)) {
+		if (!regexec(&regex, p + 2, 0, NULL, 0)) {
 			hashcpy(sha1, commit->object.sha1);
 			retval = 0;
 			break;
 		}
 	}
+	regfree(&regex);
 	free(temp_commit_buffer);
 	free_commit_list(list);
 	for (l = backup; l; l = l->next)
-- 
1.7.1.83.g9d02
Junio C Hamano· Jun 18, 2010, 17:03 UTC · re: Linus Torvalds · lore

Re: [RFC PATCH] Make :/ accept a regex rather than a fixed pattern

Linus Torvalds <torvalds@linux-foundation.org> writes:
Show 29 quoted lines
> From: Linus Torvalds <torvalds@linux-foundation.org>
> Date: Fri, 23 Apr 2010 08:20:20 -0700
> Subject: [PATCH] Make :/ accept a regex rather than a fixed pattern
>
> This also makes it trigger anywhere in the commit message, rather than
> just at the beginning. Which tends to be a lot more useful.
>
> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
> ---
>
> I'm re-sending, because after having this in my tree for several weeks 
> now, I actually end up still using it. 
>
> I agree that it's still not a wonderful thing, and it's entirely possible 
> that we should strive to use just HEAD in :/ commit finding rather than 
> all refs we can find, but for at least the kernel, I don't care (since my 
> tree tends to have just one main branch anyway, apart from some random 
> testing stuff I keep around).
>
> But even if we want to limit it to HEAD, that would be a separate patch.
>
> I've personally used it for things like
>
> 	git show :/mqueue	# did I apply that 'mqueue' patch?
> 	git show :/akpm		# what was the last patch I got from Andrew?
>
> and while in all cases I could admittedly have done exactly the same thing 
> with something like "git log -p -1 --grep=mqueue" instead, I've used it as 
> a nice shortcut.

I already took this but I actually started hating it. As I have merge.summary on,

    git show ':/diff: add configuration option'

almost never hits the commit I am looking for---instead it ends up finding a merge that merges it.

I however suspect that I would hate it much less after retraining my fingers to anchor the pattern at the beginning of the line. But not yet...

← back to recent threads