threads / discuss / 37784

Plumbing version of 'git branch --contains' ?

Subject: Plumbing version of 'git branch --contains' ?

## tl;dr

3 messages between Oct 22, 2014 and Oct 23, 2014.

replies: 2people: 3as markdown or json

Crabtree, Andrew· Oct 22, 2014, 20:19 UTC · lore
I need to get a list of refs that can reach a certain SHA in in a script.
git branch --contains SHA 
would be great (runs in ~2 seconds), but not my preferred option for scripting.
I tried
 
for br in $(git for-each-ref --format='%(refname:short)' refs/heads/)
do
    git merge-base --is-ancestor $1 ${br}
    if [ $? -eq 0 ]
    then
            echo "${br}"
    fi
done
Which gives me perfect output, but takes 82 seconds to run in my environment.  
Is there an alternative I'm missing to give me the run time performance of 'git branch --contains' but with stable output suitable for parsing?

Thanks in advance, -Andrew

Jeff King· Oct 23, 2014, 17:19 UTC · re: Crabtree, Andrew · lore

Re: Plumbing version of 'git branch --contains' ?

On Wed, Oct 22, 2014 at 08:19:07PM +0000, Crabtree, Andrew wrote:
Show 18 quoted lines
> I need to get a list of refs that can reach a certain SHA in in a script.
> 
> git branch --contains SHA 
> 
> would be great (runs in ~2 seconds), but not my preferred option for scripting.
> 
> I tried
>  
> for br in $(git for-each-ref --format='%(refname:short)' refs/heads/)
> do
>     git merge-base --is-ancestor $1 ${br}
>     if [ $? -eq 0 ]
>     then
>             echo "${br}"
>     fi
> done
> 
> Which gives me perfect output, but takes 82 seconds to run in my environment.

Right. There's some setup work that happens in `git branch --contains` that we end up repeating.

> Is there an alternative I'm missing to give me the run time
> performance of 'git branch --contains' but with stable output suitable
> for parsing?

Sadly, no, there isn't currently. The right tool would be `git for-each-ref --contains`, but it doesn't exist yet. I was working towards it, but got stopped on factoring out a `--contains` traversal suitable for both `git tag` and `git branch` (they currently are different and make performance tradeoffs based on the expected depth of the merge bases, which is usually different between tags and branches)[1]. That's work I'd love to resume, but I haven't gotten around to it yet.

-Peff
[1] http://thread.gmane.org/gmane.comp.version-control.git/252472
Junio C Hamano· Oct 23, 2014, 18:07 UTC · re: Jeff King · lore

Re: Plumbing version of 'git branch --contains' ?

Jeff King <peff@peff.net> writes:
Show 40 quoted lines
> On Wed, Oct 22, 2014 at 08:19:07PM +0000, Crabtree, Andrew wrote:
>
>> I need to get a list of refs that can reach a certain SHA in in a script.
>> 
>> git branch --contains SHA 
>> 
>> would be great (runs in ~2 seconds), but not my preferred option for scripting.
>> 
>> I tried
>>  
>> for br in $(git for-each-ref --format='%(refname:short)' refs/heads/)
>> do
>>     git merge-base --is-ancestor $1 ${br}
>>     if [ $? -eq 0 ]
>>     then
>>             echo "${br}"
>>     fi
>> done
>> 
>> Which gives me perfect output, but takes 82 seconds to run in my environment.
>
> Right. There's some setup work that happens in `git branch --contains`
> that we end up repeating.
>
>> Is there an alternative I'm missing to give me the run time
>> performance of 'git branch --contains' but with stable output suitable
>> for parsing?
>
> Sadly, no, there isn't currently. The right tool would be `git
> for-each-ref --contains`, but it doesn't exist yet. I was working
> towards it, but got stopped on factoring out a `--contains` traversal
> suitable for both `git tag` and `git branch` (they currently are
> different and make performance tradeoffs based on the expected depth of
> the merge bases, which is usually different between tags and
> branches)[1].  That's work I'd love to resume, but I haven't gotten
> around to it yet.
>
> -Peff
>
> [1] http://thread.gmane.org/gmane.comp.version-control.git/252472

Thanks for status update. I was wondering if I should start looking into it myself.

← back to recent threads