# [RFD} Use regex's in :/ revision naming machinery

8 messages from 2010-04-05 to 2010-04-07. Participants: Linus Torvalds, Junio C Hamano, Jeff King, Johannes Sixt, Nazri Ramliy.
Thread: https://gitlist.dev/t/23348

## Linus Torvalds, 2010-04-05 23:00

Subject: [RFD} Use regex's in :/ revision naming machinery
Message-ID: <alpine.LFD.2.00.1004051551180.21411@i5.linux-foundation.org>
URL: https://gitlist.dev/e/alpine.LFD.2.00.1004051551180.21411%40i5.linux-foundation.org

```

So this is very much debatable, but I'm including a patch for the 
discussion to start.

I never use ':/', and part of it is that it's so horribly cumbersome. I'd 
_like_ to use it to do things like 

	gitk :/slabh..

which would (right now) show what happened since I merged the 'slabh' 
branch. But that doesn't work, I would need to write it as

	gitk :/"Merge branch 'slabh'"..

at which point it really isn't all that useful any more - in order to 
figure out the string I need to use, I had to look up the commit, so the 
whole :/ format was useless. I might as well just have used the SHA1 value 
instead.

The thing is, I'm not quite sure _who_ uses :/ as things are now, so maybe 
there is some reason for that incredibly annoying interface.

But with this trivial (LARGELY UNTESTED!) patch, I get the behaviour _I_ 
want, and I can do my simpler version instead.

It also allows me to do silly things like this:

	git show :/'Signed-off-by: Linus'

which shows the last commit that was signed off by me. Is that really 
useful? Not bloody likely. But it shows how much more flexible the whole 
notion of "let's grep the commit message" is than having to match exactly 
the beginning of it.

		Linus

PS. Do note the "largely untested" part. I think this patch is kind of 
cool, but it might be totally broken. I did run the test-suite on it, and 
it still says "failed  0", but that's probably more indicative of limited 
test coverage of :/ than anything else.

---
 sha1_name.c |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)

diff --git a/sha1_name.c b/sha1_name.c
index bf92417..7570f1a 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -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)

```

## Junio C Hamano, 2010-04-06 01:51

Subject: Re: [RFD} Use regex's in :/ revision naming machinery
Message-ID: <7vr5mtl55q.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vr5mtl55q.fsf%40alter.siamese.dyndns.org
In-Reply-To: <alpine.LFD.2.00.1004051551180.21411@i5.linux-foundation.org>

```
Linus Torvalds <torvalds@linux-foundation.org> writes:

> So this is very much debatable, but I'm including a patch for the 
> discussion to start.
>
> I never use ':/', and part of it is that it's so horribly cumbersome. I'd 
> _like_ to use it to do things like 
>
> 	gitk :/slabh..
>
> which would (right now) show what happened since I merged the 'slabh' 
> branch. But that doesn't work, I would need to write it as
>
> 	gitk :/"Merge branch 'slabh'"..

A related but a larger issue is that I suspect this "two-dot" would not
work, as the syntax looks for "Merge branch 'slabh'.." (two-dot taken
literally).

I also happen to think that the current ':/' is more or less useless
because you cannot tell it to start digging from only these branches, and
it becomes dice-throwing which commit it would find.

Compared to these, I don't think anybody has much against using regex.  I
personally am all for it, but I am not sure how much regex makes it more
useful.

```

## Junio C Hamano, 2010-04-06 02:08

Subject: Re: [RFD} Use regex's in :/ revision naming machinery
Message-ID: <7vmxxhl4dy.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vmxxhl4dy.fsf%40alter.siamese.dyndns.org
In-Reply-To: <7vr5mtl55q.fsf@alter.siamese.dyndns.org>

```
Junio C Hamano <gitster@pobox.com> writes:

> A related but a larger issue is that I suspect this "two-dot" would not
> work, as the syntax looks for "Merge branch 'slabh'.." (two-dot taken
> literally).

Heh, what do I know.  This does seem to work.  Sorry for the noise.

```

## Linus Torvalds, 2010-04-06 02:15

Subject: Re: [RFD} Use regex's in :/ revision naming machinery
Message-ID: <alpine.LFD.2.00.1004051911190.11318@i5.linux-foundation.org>
URL: https://gitlist.dev/e/alpine.LFD.2.00.1004051911190.11318%40i5.linux-foundation.org
In-Reply-To: <7vr5mtl55q.fsf@alter.siamese.dyndns.org>

```


On Mon, 5 Apr 2010, Junio C Hamano wrote:
> 
> A related but a larger issue is that I suspect this "two-dot" would not
> work, as the syntax looks for "Merge branch 'slabh'.." (two-dot taken
> literally).

That part works. We do the dot-dot handling first. But:

> I also happen to think that the current ':/' is more or less useless
> because you cannot tell it to start digging from only these branches, and
> it becomes dice-throwing which commit it would find.

I do agree that :/ would be much better if it just started from HEAD, 
rather than from any branch. It doesn't matter too much for me, but that's 
because for the kernel I tend to have just one branch (with random 
occasional topic-branches, but since I do merging rather than real 
development, that's the rare case)

> Compared to these, I don't think anybody has much against using regex.  I
> personally am all for it, but I am not sure how much regex makes it more
> useful.

I have to admit that I never use it. The reason I did that silly patch is 
that today I thought that I _could_ use it, but then my naive

	gitk :/slabh..

thing didn't work, and I wondered why.

Would I actually use it more regularly if it was a regex? And if it had 
saner semantics wrt branches? I dunno. Maybe my try at using it today was 
the last time I'd ever try to use it again, regardless.

			Linus

```

## Jeff King, 2010-04-06 04:31

Subject: Re: [RFD} Use regex's in :/ revision naming machinery
Message-ID: <20100406043133.GA3901@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20100406043133.GA3901%40coredump.intra.peff.net
In-Reply-To: <alpine.LFD.2.00.1004051551180.21411@i5.linux-foundation.org>

```
On Mon, Apr 05, 2010 at 04:00:59PM -0700, Linus Torvalds wrote:

> So this is very much debatable, but I'm including a patch for the 
> discussion to start.

FWIW, this came up quite a while ago and people generally seemed to
think it was a good idea, but the patch from Dscho (cc'd) never made it
upstream.

The old thread is here:

  http://article.gmane.org/gmane.comp.version-control.git/50010

Personally, I think regex matching makes :/ a bit more usable, though
since that thread I have found I don't use :/ all that often (instead
needing to pick through the results of --grep looking for the commit in
question, at which point I just cut-and-paste the commit sha1). I agree
as was suggested elsewhere in the thread that searching from HEAD would
help.

-Peff

```

## Johannes Sixt, 2010-04-06 07:55

Subject: Re: [RFD} Use regex's in :/ revision naming machinery
Message-ID: <4BBAE904.6010105@viscovery.net>
URL: https://gitlist.dev/e/4BBAE904.6010105%40viscovery.net
In-Reply-To: <alpine.LFD.2.00.1004051551180.21411@i5.linux-foundation.org>

```
Am 4/6/2010 1:00, schrieb Linus Torvalds:
> I never use ':/', and part of it is that it's so horribly cumbersome. I'd 
> _like_ to use it to do things like 
> 
> 	gitk :/slabh..

Rhethoric question: Do you mean history _starting_ at the commit that
contains "slabh" or _ending_ at the commit that contains "slabh" followed
by two arbitrary characters?

If :/ is pattern-ized in some way, then IMO pattern matching syntax would
be more use-friendly than (extended) regular expresssions, particluarly
also because the single-character wildcard would be ? and avoid the
otherwise overloaded dot.

-- Hannes

```

## Linus Torvalds, 2010-04-06 14:19

Subject: Re: [RFD} Use regex's in :/ revision naming machinery
Message-ID: <alpine.LFD.2.00.1004060713200.3487@i5.linux-foundation.org>
URL: https://gitlist.dev/e/alpine.LFD.2.00.1004060713200.3487%40i5.linux-foundation.org
In-Reply-To: <4BBAE904.6010105@viscovery.net>

```


On Tue, 6 Apr 2010, Johannes Sixt wrote:
>
> Am 4/6/2010 1:00, schrieb Linus Torvalds:
> > I never use ':/', and part of it is that it's so horribly cumbersome. I'd 
> > _like_ to use it to do things like 
> > 
> > 	gitk :/slabh..
> 
> Rhethoric question: Do you mean history _starting_ at the commit that
> contains "slabh" or _ending_ at the commit that contains "slabh" followed
> by two arbitrary characters?

Ending in the commit that contains "slabh".

The last two dots are removed by the revision handling machinery, _before_ 
we even see the ':/' thing. See 'handle_revision_arg()', and notice how it 
searches for ".." and then does "*dotdot = 0".

So the regex is 'slabh'.

> If :/ is pattern-ized in some way, then IMO pattern matching syntax would
> be more use-friendly than (extended) regular expresssions, particluarly
> also because the single-character wildcard would be ? and avoid the
> otherwise overloaded dot.

I do agree that regex'es might contain the invalid sequence "..", but it's 
fairly unusual, and you can work around it, ie you can do

	gitk :/'slabh(.)(.)of'..

which now that I write it out admittedly looks like you are searching for 
titties, but hey, sex sells, so we can call it a _feature_.

			Linus

```

## Nazri Ramliy, 2010-04-07 01:04

Subject: Re: [RFD} Use regex's in :/ revision naming machinery
Message-ID: <q2p544dda351004061804vf56aa87bo86da6480efdfa4c6@mail.gmail.com>
URL: https://gitlist.dev/e/q2p544dda351004061804vf56aa87bo86da6480efdfa4c6%40mail.gmail.com
In-Reply-To: <alpine.LFD.2.00.1004060713200.3487@i5.linux-foundation.org>

```
On Tue, Apr 6, 2010 at 10:19 PM, Linus Torvalds
<torvalds@linux-foundation.org> wrote:
>        gitk :/'slabh(.)(.)of'..
>
> which now that I write it out admittedly looks like you are searching for
> titties, but hey, sex sells, so we can call it a _feature_.

I knew it! You've had this planned out since the early beginnings!

There's a reason why you chose the name 'blob', no?

git grep blob|sed -e 's/blo/boo/g'|less

Hidden features?

Warning: Don't try this at home. You may never read the source the
same way again!

nazri (I'll go back to my cave)

```
