threads / discuss / 14589

How to find the first commit belonging to any branch

Subject: How to find the first commit belonging to any branch

## tl;dr

3 messages between Jul 22, 2008 and Jul 22, 2008.

replies: 2people: 2as markdown or json

Kristian Amlie· Jul 22, 2008, 09:08 UTC · lore
Hi all!

I have a question about git: I have one commit sha1, and I would like to know the nearest commit that appears in *any* other branch. The sha1 that I have does not belong to any branch.

The obvious thing to do would be to make a for loop and iterate over existing branches while calling git merge-base, but I'm wondering if there's a more clever method.

Regards Kristian Amlie

Junio C Hamano· Jul 22, 2008, 09:27 UTC · re: Kristian Amlie · lore

Re: How to find the first commit belonging to any branch

Kristian Amlie <kristian.amlie@trolltech.com> writes:
Show 7 quoted lines
> I have a question about git: I have one commit sha1, and I would like
> to know the nearest commit that appears in *any* other branch. The
> sha1 that I have does not belong to any branch.
>
> The obvious thing to do would be to make a for loop and iterate over
> existing branches while calling git merge-base, but I'm wondering if
> there's a more clever method.
If the $commit does not belong to any branch, then:
    $ git rev-list --bounardy $commit^0 --not --branches | sed -ne 's/^-//p'
would give you boundary commits of the above traversal, which says:
    Traverse from $commit following the parents, but stop at anything that
    is reachable from any breanch.

which means that the ones that are output are the candidates that are on some branch.

So pipe that to name-rev like this, perhaps (untested)?
    $ git rev-list --bounardy $commit^0 --not --branches |
      sed -ne 's/^-//p' |
      git name-rev --stdin
Kristian Amlie· Jul 22, 2008, 11:18 UTC · re: Junio C Hamano · lore

Re: How to find the first commit belonging to any branch

Junio C Hamano wrote:
Show 29 quoted lines
> Kristian Amlie <kristian.amlie@trolltech.com> writes:
> 
>> I have a question about git: I have one commit sha1, and I would like
>> to know the nearest commit that appears in *any* other branch. The
>> sha1 that I have does not belong to any branch.
>>
>> The obvious thing to do would be to make a for loop and iterate over
>> existing branches while calling git merge-base, but I'm wondering if
>> there's a more clever method.
> 
> If the $commit does not belong to any branch, then:
> 
>     $ git rev-list --bounardy $commit^0 --not --branches | sed -ne 's/^-//p'
> 
> would give you boundary commits of the above traversal, which says:
> 
>     Traverse from $commit following the parents, but stop at anything that
>     is reachable from any breanch.
> 
> which means that the ones that are output are the candidates that are on
> some branch.
> 
> So pipe that to name-rev like this, perhaps (untested)?
> 
>     $ git rev-list --bounardy $commit^0 --not --branches |
>       sed -ne 's/^-//p' |
>       git name-rev --stdin
> 
> 
Thanks! That did the trick!
Kristian

← back to recent threads