# Testing if a certain commit is in the current branch

12 messages from 2010-01-27 to 2010-01-27. Participants: Frans Pop, Junio C Hamano, Matthieu Moy, Johannes Schindelin, Jonathan Nieder, Petr Baudis, Dale Farnsworth.
Thread: https://gitlist.dev/t/22406

## Frans Pop, 2010-01-27 07:19

Subject: Testing if a certain commit is in the current branch
Message-ID: <201001270819.39819.elendil@planet.nl>
URL: https://gitlist.dev/e/201001270819.39819.elendil%40planet.nl

```
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

```

## Junio C Hamano, 2010-01-27 07:24

Subject: Re: Testing if a certain commit is in the current branch
Message-ID: <7viqaorowo.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7viqaorowo.fsf%40alter.siamese.dyndns.org
In-Reply-To: <201001270819.39819.elendil@planet.nl>

```
Frans Pop <elendil@planet.nl> writes:

> 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.

```

## Matthieu Moy, 2010-01-27 08:26

Subject: Re: Testing if a certain commit is in the current branch
Message-ID: <vpqiqaonec1.fsf@bauges.imag.fr>
URL: https://gitlist.dev/e/vpqiqaonec1.fsf%40bauges.imag.fr
In-Reply-To: <201001270819.39819.elendil@planet.nl>

```
Frans Pop <elendil@planet.nl> writes:

> 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.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

```

## Frans Pop, 2010-01-27 08:36

Subject: Re: Testing if a certain commit is in the current branch
Message-ID: <201001270936.14935.elendil@planet.nl>
URL: https://gitlist.dev/e/201001270936.14935.elendil%40planet.nl
In-Reply-To: <7viqaorowo.fsf@alter.siamese.dyndns.org>

```
On Wednesday 27 January 2010, Junio C Hamano wrote:
> Frans Pop <elendil@planet.nl> writes:
> > 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.)

> 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

```

## Frans Pop, 2010-01-27 08:47

Subject: Re: Testing if a certain commit is in the current branch
Message-ID: <201001270947.41480.elendil@planet.nl>
URL: https://gitlist.dev/e/201001270947.41480.elendil%40planet.nl
In-Reply-To: <vpqiqaonec1.fsf@bauges.imag.fr>

```
On Wednesday 27 January 2010, Matthieu Moy wrote:
> Frans Pop <elendil@planet.nl> writes:
> > 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

```

## Johannes Schindelin, 2010-01-27 08:56

Subject: Re: Testing if a certain commit is in the current branch
Message-ID: <alpine.DEB.1.00.1001270955350.4985@pacific.mpi-cbg.de>
URL: https://gitlist.dev/e/alpine.DEB.1.00.1001270955350.4985%40pacific.mpi-cbg.de
In-Reply-To: <201001270819.39819.elendil@planet.nl>

```
Hi,

On Wed, 27 Jan 2010, Frans Pop wrote:

> 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

```

## Jonathan Nieder, 2010-01-27 10:19

Subject: Re: Testing if a certain commit is in the current branch
Message-ID: <20100127101943.GA4470@progeny.tock>
URL: https://gitlist.dev/e/20100127101943.GA4470%40progeny.tock
In-Reply-To: <201001270936.14935.elendil@planet.nl>

```
Frans Pop wrote:

> 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 ...

```

## Frans Pop, 2010-01-27 13:26

Subject: Re: Testing if a certain commit is in the current branch
Message-ID: <201001271426.06844.elendil@planet.nl>
URL: https://gitlist.dev/e/201001271426.06844.elendil%40planet.nl
In-Reply-To: <20100127101943.GA4470@progeny.tock>

```
On Wednesday 27 January 2010, Jonathan Nieder wrote:
> Frans Pop wrote:
> > 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

```

## Petr Baudis, 2010-01-27 15:08

Subject: Re: Testing if a certain commit is in the current branch
Message-ID: <20100127150834.GG9553@machine.or.cz>
URL: https://gitlist.dev/e/20100127150834.GG9553%40machine.or.cz
In-Reply-To: <201001270936.14935.elendil@planet.nl>

```
On Wed, Jan 27, 2010 at 09:36:14AM +0100, Frans Pop wrote:
> On Wednesday 27 January 2010, Junio C Hamano wrote:
> > Frans Pop <elendil@planet.nl> writes:
> > > 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.)

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

```

## Dale Farnsworth, 2010-01-27 15:15

Subject: Re: Testing if a certain commit is in the current branch
Message-ID: <E1Na9ca-0004x2-Gf@xyzzy.farnsworth.org>
URL: https://gitlist.dev/e/E1Na9ca-0004x2-Gf%40xyzzy.farnsworth.org
In-Reply-To: <201001271426.06844.elendil@planet.nl>

```
> >  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

```

## Frans Pop, 2010-01-27 15:27

Subject: Re: Testing if a certain commit is in the current branch
Message-ID: <201001271627.45715.elendil@planet.nl>
URL: https://gitlist.dev/e/201001271627.45715.elendil%40planet.nl
In-Reply-To: <20100127150834.GG9553@machine.or.cz>

```
On Wednesday 27 January 2010, Petr Baudis wrote:
> > 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

```

## Frans Pop, 2010-01-27 15:39

Subject: Re: Testing if a certain commit is in the current branch
Message-ID: <201001271639.44167.elendil@planet.nl>
URL: https://gitlist.dev/e/201001271639.44167.elendil%40planet.nl
In-Reply-To: <E1Na9ca-0004x2-Gf@xyzzy.farnsworth.org>

```
On Wednesday 27 January 2010, Dale Farnsworth wrote:
> 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

```
