Skip to contentSkip to conversation

Testing if a certain commit is in the current branch

DISCUSSION Frans Pop 7 participants 12 messages Jun 15, 2016

lore
Share & tools
lore

Frans Pop

author
#0 lore raw

I'm currently using the following command for this:
git log --pretty=format:%H | grep -q '^<commit id>"

Problem is that with large repos that can be quite slow.

Is there a faster way to do the test?

TIA,
FJP

#1 lore raw
Quoted 6 lines · Frans Pop

I'm currently using the following command for this:

   git log --pretty=format:%H | grep -q '^<commit id>"

Problem is that with large repos that can be quite slow.

Is there a faster way to do the test?

test "$(git merge-base HEAD $commit)" = "$(git rev-parse $commit)"

But this is a literal answer that could be a less than optimal solution if you are having an XY problem.

author
#2 lore raw
Quoted 9 lines · Junio C Hamano
Quoted 6 lines · Frans Pop

I'm currently using the following command for this:

   git log --pretty=format:%H | grep -q "^<commit id>"

Problem is that with large repos that can be quite slow.

Is there a faster way to do the test?

test "$(git merge-base HEAD $commit)" = "$(git rev-parse $commit)"

Great! If the commit ID is not present that only takes 1 sec versus 11 secs for my test. (If the commit _is_ present and fairly recent my test can be faster, but 11 secs delay when it's not present hurts more.)

Quoted 2 lines

But this is a literal answer that could be a less than optimal solution if you are having an XY problem.

The use case is a wrapper script for kernel builds in which some variation are needed (due to changes in the build system) when building older kernels, e.g. during bisections or when building older stable updates. So I test for the presence of commits that introduced relevant changes.

Thanks again,
FJP

#3 lore raw
Quoted 4 lines · Frans Pop

The use case is a wrapper script for kernel builds in which some variation are needed (due to changes in the build system) when building older kernels, e.g. during bisections or when building older stable updates. So I test for the presence of commits that introduced relevant changes.

Neat idea. How about this (untested)?

git rev-list ^HEAD --no-walk commit1 commit2 ...

#9 lore raw
In reply to Frans Pop

Hi,

Quoted 6 lines · Frans Pop

I'm currently using the following command for this:

   git log --pretty=format:%H | grep -q '^<commit id>"

Problem is that with large repos that can be quite slow.

Is there a faster way to do the test?

You are more likely looking for "git branch --contains". But this gives you _more_ information.

Ciao,
Dscho

#10 lore raw
In reply to Frans Pop
Quoted 6 lines · Frans Pop

I'm currently using the following command for this:

   git log --pretty=format:%H | grep -q '^<commit id>"

Problem is that with large repos that can be quite slow.

Is there a faster way to do the test?

Not answering the question exactly, but "git branch --contains <commitid>" does almost this and a bit more.

Signature · 2 lines
Matthieu Moy
http://www-verimag.imag.fr/~moy/
author
#11 lore raw
Quoted 10 lines · Matthieu Moy
Quoted 6 lines · Frans Pop

I'm currently using the following command for this:

   git log --pretty=format:%H | grep -q "^<commit id>"

Problem is that with large repos that can be quite slow.

Is there a faster way to do the test?

Not answering the question exactly, but "git branch --contains <commitid>" does almost this and a bit more.

Thanks. That would work as:
git branch --contains <commitid> | grep -q "^\*"

It's a lot slower than Junio's solution though (and even slower than my original solution).

Cheers,
FJP

author
#4 lore raw
In reply to Jonathan Nieder
Quoted 10 lines · Jonathan Nieder
Quoted 5 lines · Frans Pop

The use case is a wrapper script for kernel builds in which some variation are needed (due to changes in the build system) when building older kernels, e.g. during bisections or when building older stable updates. So I test for the presence of commits that introduced relevant changes.

Neat idea. How about this (untested)?

git rev-list ^HEAD --no-walk commit1 commit2 ...

Does not seem to work. But I'd be interested if you find a variant that does as it's very fast.

Thanks,
FJP

#5 lore raw
Quoted 4 lines
Quoted 1 line

git rev-list ^HEAD --no-walk commit1 commit2 ...

Does not seem to work. But I'd be interested if you find a variant that does as it's very fast.

I've been using the following. It produces output only if the commit is NOT in HEAD.

git rev-list -1 ^HEAD commitID

-Dale

author
#6 lore raw
Quoted 4 lines · Dale Farnsworth

I've been using the following. It produces output only if the commit is NOT in HEAD.

git rev-list -1 ^HEAD commitID

Nice. That does work.

Returns 0 in both cases and you'd need to allow for nonexisting commit IDs, so you'd get:
test -z "$(git rev-list -1 ^HEAD <commit id> 2>/dev/null)"

Time needed looks to be slightly faster than Junio's test.

Thanks,
FJP

#7 lore raw
In reply to Frans Pop
Quoted 7 lines · Frans Pop
Quoted 6 lines · Junio C Hamano
Quoted 5 lines · Frans Pop

I'm currently using the following command for this:

   git log --pretty=format:%H | grep -q "^<commit id>"

Problem is that with large repos that can be quite slow.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Is there a faster way to do the test?

test "$(git merge-base HEAD $commit)" = "$(git rev-parse $commit)"

Great! If the commit ID is not present that only takes 1 sec versus 11 secs for my test. (If the commit _is_ present and fairly recent my test can be faster, but 11 secs delay when it's not present hurts more.)

Isn't something very wrong if grepping the log output is faster than simple merge-base call? Can you post exact numbers?

Petr "Pasky" Baudis

author
#8 lore raw
Quoted 7 lines · Petr Baudis
Quoted 4 lines

Great! If the commit ID is not present that only takes 1 sec versus 11 secs for my test. (If the commit _is_ present and fairly recent my test can be faster, but 11 secs delay when it's not present hurts more.)

Isn't something very wrong if grepping the log output is faster than simple merge-base call? Can you post exact numbers?

It's only faster if the commit is present very early in the log. The reason is probably that because of the '-q' option it does not actually read the full log, but breaks out as soon as it's found a match.

In my test I had an elapsed time of ~0.5 sec (versus 1 sec for Junio's test). And here's a grep for the very first commit:

$ time git log --pretty=format:%H | grep -q 48404cb50b0b547
real    0m0.010s
user    0m0.000s
sys     0m0.012s