4

Assume I want to refer to a the parent of youngest commit whose commit message contains 'foo'.

HEAD^{/foo}^ will do the job.
This can be slightly shortened to @^{/foo}^ (I think).

The <rev>^{/<text>} construction has a simplified form, though: :/<text>.

Is there any way to use the short form and still refer to the parent of the resulting commit?

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
Alex R
  • 2,201
  • 1
  • 19
  • 32
  • 1
    Not directly in a single command. i assume you found the minimal way of doing it. with several commands you can use the git log + grep – CodeWizard Apr 29 '16 at 20:48
  • He did not, did he? `^{/}` is different from `:/`, as the first works only on ``, the second on all revs. Or did I miss something? I'd also like to know how to do that. – Vampire Apr 30 '16 at 09:55

1 Answers1

5

Following the git rev-parse command, you can combine two of those in order to get the parent of a commit whose message match the regexp.

In a git bash, type:

git rev-parse $(git rev-parse :/<text>)^

Those are two commands:

  • first getting the commit with the right message: git rev-parse :/<text>
  • then getting its parent git rev-parse $(...)^

The $() will execute the first command git rev-parse and give its result to the second command git rev-parse.

That will get you the parent of the commit with a commit message matching <text>.


This differ from using only one command with:

git rev-parse HEAD^{/<text>}^

Or in Windows CMD:

git rev-parse "@^{/<text>}^"
# or, more complex, as ^ is the windows escape sign:
git rev-parse ^@^^{/<text>}^^
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • `@` would just be an alias for `HEAD` which are both not the topic of the question as it would only search in `HEAD`, not in all refs. Is there no way to get this in just one rev-parse syntax? There might be places outside Bash where such a syntax would be handy. – Vampire May 03 '16 at 12:54
  • @BjörnKautler I agree, but I did not find a way to get the same result with *one* rev-parse syntax. – VonC May 03 '16 at 12:55
  • Me neither. Maybe this is scope for a feature request or patch to Git. :-) Maybe something like `:{/}^`. If there is really no other way to specify it already. – Vampire May 03 '16 at 12:56
  • @BjörnKautler good idea. Note: I did know about `@` being an alias for `HEAD` (I reported it first in July 2013 for git 1.8.4: http://stackoverflow.com/a/964927/6309). I just mentioned `@` was not working well when used directly with the `^{/}` notation. – VonC May 03 '16 at 12:58
  • Works fine for me here – Vampire May 03 '16 at 13:00
  • @BjörnKautler Are you using git on Windows or directly in a Linux environment? – VonC May 03 '16 at 13:51
  • I'm using it on linux, in cygwin and I even tried with Git for Windows from cmd.exe. The only caveat in the last case was, that you have to enclose the argument in double-quotes, or the cmd.exe will not handle the `@`, `^`, `{` and `}` correctly, meaning not handle it but give it to git. – Vampire May 03 '16 at 13:59
  • @BjörnKautler double-quotes! Of course. I have edited the answer to reflect that syntax, or even another alternative one. But regarding the original question, when using `^{/}`, you would still need two commands instead of one. – VonC May 03 '16 at 14:19
  • How would these two commands look like? With `^{/}` you cannot get the youngest matching commit of all refs like you do with `:/` or the proposed `:{/}^`. With `^{/}` you just get an error as you must not leave out the ref to be searched in front of it – Vampire May 03 '16 at 15:25
  • @BjörnKautler I have edited the answer to make those two commands more visible. – VonC May 03 '16 at 15:31
  • I will accept this answer until/unless someone posts a solution which uses rev-parse syntax directly (avoiding the subprocess call), possibly after git is patched to allow this. – Alex R May 03 '16 at 15:55
  • @VonC I still don't get what you meant in the comment "But regarding the original question, when using `^{/}`, you would still need two commands instead of one.". I don't see how you get that effect with `^{/}` at all. – Vampire May 03 '16 at 18:17
  • @BjörnKautler sorry, I got mixed up: I meant with `:/`, you would need two commands. – VonC May 03 '16 at 18:22
  • Ah, ok, I see. Yeah, it seems there is no direct way to express this as long as my proposal or similar does not make it into Git. – Vampire May 03 '16 at 18:49