threads / discuss / 17454

Finding the name of the parent branch?

Subject: Finding the name of the parent branch?

## tl;dr

11 messages between Jan 30, 2009 and Jan 30, 2009.

replies: 10people: 4as markdown or json

Pascal Obry· Jan 30, 2009, 10:56 UTC · lore
I want to find the name of the parent branch in a script. What is the best way?
                         o---o---o---C
                         /
                    o---o---o---B
                   /
           ---o---o---o---o---o---A
For B I want to get A and for C I want to get B.

All this can be found in gitk, but from a script I did not found a good solution.

Any idea?

Thanks. Pascal.

-- 
--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|              http://www.obry.net
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595
Santi Béjar· Jan 30, 2009, 11:18 UTC · re: Pascal Obry · lore

Re: Finding the name of the parent branch?

2009/1/30 Pascal Obry <pascal@obry.net>:
Show 9 quoted lines
> I want to find the name of the parent branch in a script. What is the best way?
>
>                         o---o---o---C
>                         /
>                    o---o---o---B
>                   /
>           ---o---o---o---o---o---A
>
> For B I want to get A and for C I want to get B.

I think your definition is not well defined. A, B and C are just branches of you project, technically they are equivalent. Maybe you are thinking that the common commits of, say A and B, really belongs to A, but this is not the case they belong to both branches. In git a branch is really just a pointer to a commit and by extension the history, it is not a series of commits.

Just a counterexample, just rearranging you graph:
                        o---B
                        /
                   o---o---o---o---o---C
                  /
          ---o---o---o---o---o---A
>From you description: For B I would get C and for C I would get A.

HTH, Santi

Pascal Obry· Jan 30, 2009, 12:56 UTC · re: Santi Béjar · lore

Re: Finding the name of the parent branch?

Santi,
Thanks for you reply.
> I think your definition is not well defined. A, B and C are just
> branches of you project, technically they are equivalent. Maybe you
Right. Yet I want to know from which branch a branch as been started.
You need this to get the proper merge-base for example:
   $ git merge-base C A
      1
   $ git merge-base B C
      2
   $ git merge-base B A
      1

I always know on which topic branch I'm but, as shown above, depending on the parent branch passed to merge-base you do not get the same branch-point. This is fine.

So, when I'm in a topic branch I want to find the name of the parent branch. The one given when creating the branch:

   $ git branch B C

A "stupid" solution whould be to iterate over all branches. Looking for the merge-base and at the end output the branch having the youngest merge-base. I'm looking for something more efficient...

Show 14 quoted lines
> are thinking that the common commits of, say A and B, really belongs
> to A, but this is not the case they belong to both branches. In git a
> branch is really just a pointer to a commit and by extension the
> history, it is not a series of commits.
>
> Just a counterexample, just rearranging you graph:
>
>                        o---B
>                        /
>                   o---2---o---o---o---C
>                  /
>          ---o---1---o---o---o---A
>
> From you description: For B I would get C and for C I would get A.
Don't see this as a counter-example as it is exactly my example.
Did I missed something?
Pascal.
-- 
--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|              http://www.obry.net
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595
Santi Béjar· Jan 30, 2009, 13:16 UTC · re: Pascal Obry · lore

Re: Finding the name of the parent branch?

2009/1/30 Pascal Obry <pascal@obry.net>:
Show 8 quoted lines
> Santi,
>
> Thanks for you reply.
>
>> I think your definition is not well defined. A, B and C are just
>> branches of you project, technically they are equivalent. Maybe you
>
> Right. Yet I want to know from which branch a branch as been started.
You can set it when you create the branch:
git branch --track newbranch startpointbranch
(maybe --track is the default these days for remote branches)
And then the config keys:

branch.newbranch.remote branch.newbranch.merge

will tell you from which branch a branch was started. And it is used in "git pull" to merge from the tracking branch.

Show 10 quoted lines
> You need this to get the proper merge-base for example:
>
>   $ git merge-base C A
>      1
>
>   $ git merge-base B C
>      2
>
>   $ git merge-base B A
>      1
1 and 2 are defined in the graph below...
Show 9 quoted lines
> I always know on which topic branch I'm but, as shown above, depending on the
> parent branch passed to merge-base you do not get the same branch-point. This
> is fine.
>
> So, when I'm in a topic branch I want to find the name of the parent
> branch. The one given
> when creating the branch:
>
>   $ git branch B C
See above.
Show 7 quoted lines
>
> A "stupid" solution whould be to iterate over all branches. Looking
> for the merge-base and
> at the end output the branch having the youngest merge-base. I'm
> looking for something
> more efficient...
>

Maybe if you explain why you want it (a use case) instead of just this specific problem...

Show 14 quoted lines
>> are thinking that the common commits of, say A and B, really belongs
>> to A, but this is not the case they belong to both branches. In git a
>> branch is really just a pointer to a commit and by extension the
>> history, it is not a series of commits.
>>
>> Just a counterexample, just rearranging you graph:
>>
>>                        o---B
>>                        /
>>                   o---2---o---o---o---C
>>                  /
>>          ---o---1---o---o---o---A
>>
>> From you description: For B I would get C and for C I would get A.
Please, if you quote text do not edit it (the 1 and the 2 in this case).
> Don't see this as a counter-example as it is exactly my example.
>
> Did I missed something?
Yes. Compare your sentence and mine:

For B I want to get A and for C I want to get B. For B I would get C and for C I would get A.

So for B you get A while I get C, and the equivalent for C.
Santi
Pascal Obry· Jan 30, 2009, 13:35 UTC · re: Santi Béjar · lore

Re: Finding the name of the parent branch?

Santi,
> Maybe if you explain why you want it (a use case) instead of just this
> specific problem...

To know the proper merge base to display all commits done on a specific topic branch.

Show 11 quoted lines
>>> Just a counterexample, just rearranging you graph:
>>>
>>>                        o---B
>>>                        /
>>>                   o---2---o---o---o---C
>>>                  /
>>>          ---o---1---o---o---o---A
>>>
>>> From you description: For B I would get C and for C I would get A.
>
> Please, if you quote text do not edit it (the 1 and the 2 in this case).
Well I've just added 1 and 2, nothing changed in the semantic!
Show 6 quoted lines
> Yes. Compare your sentence and mine:
>
> For B I want to get A and for C I want to get B.
> For B I would get C and for C I would get A.
>
> So for B you get A while I get C, and the equivalent for C.
Ok, that's expected since you have renamed B to C and C to B.
My tree was:
                        o---o---o---C
                        /
                   o---o---o---B
                  /
          ---o---o---o---o---o---A
Your's was:
                       o---B
                       /
                  o---o---o---o---o---C
                 /
         ---o---o---o---o---o---A
So when I said:
    For B I want to get A and for C I want to get B.
It is equivalent to your (just rename B and C).:
   For B I would get C and for C I would get A.

Frankly I do not see your point... That's maybe the cause of the problem I'm having....

Pascal.
-- 
--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|              http://www.obry.net
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595
Santi Béjar· Jan 30, 2009, 13:57 UTC · re: Pascal Obry · lore

Re: Finding the name of the parent branch?

2009/1/30 Pascal Obry <pascal@obry.net>:
Show 8 quoted lines
> Santi,
>
>> Maybe if you explain why you want it (a use case) instead of just this
>> specific problem...
>
> To know the proper merge base to display all commits done on a specific
> topic branch.
>
gitk topicbranch ^trackingbranch
But I agree that a way to refer to the tracking branch would be great, as:
branch^{origin}
so you can say, for example:
gitk topicbranch ^topicbranch^{origin}
Show 22 quoted lines
>>>> Just a counterexample, just rearranging you graph:
>>>>
>>>>                        o---B
>>>>                        /
>>>>                   o---2---o---o---o---C
>>>>                  /
>>>>          ---o---1---o---o---o---A
>>>>
>>>> From you description: For B I would get C and for C I would get A.
>>
>> Please, if you quote text do not edit it (the 1 and the 2 in this case).
>
> Well I've just added 1 and 2, nothing changed in the semantic!
>
>> Yes. Compare your sentence and mine:
>>
>> For B I want to get A and for C I want to get B.
>> For B I would get C and for C I would get A.
>>
>> So for B you get A while I get C, and the equivalent for C.
>
> Ok, that's expected since you have renamed B to C and C to B.
I did not rename B to C and C to B, I just draw them differently.
Let's put name to the commits:
Your tree:
                        i---j---k---C
                        /
                   f---g---h---B
                  /
          ---a---b---c---d---e---A
My tree:
                       h---B
                       /
                  f---g---i---j---k---C
                 /
         ---a---b---c---d---e---A

So the commits in all the branches are equal, the only change is that I painted B above C.

Show 10 quoted lines
> So when I said:
>
>    For B I want to get A and for C I want to get B.
>
> It is equivalent to your (just rename B and C).:
>
>   For B I would get C and for C I would get A.
>
> Frankly I do not see your point... That's maybe the cause of the
> problem I'm having....
At least part of. You have to understand the branch model:
git model:
  * a branch is just a pointer to a commit
  * you cannot say "this commit was done in that branch"
  * what you can say is "this commit is contained in that branch"
in contrast to other models:
  * where a commit really belongs to a branch (it is specified at
commit time somehow)
  * you can say "this commit was done in that branch"

HTH, Santi

Pascal Obry· Jan 30, 2009, 14:06 UTC · re: Santi Béjar · lore

Re: Finding the name of the parent branch?

Santi,
Thanks for your patience!
Show 9 quoted lines
> gitk topicbranch ^trackingbranch
>
> But I agree that a way to refer to the tracking branch would be great, as:
>
> branch^{origin}
>
> so you can say, for example:
>
> gitk topicbranch ^topicbranch^{origin}
That's exactly what I'm looking for!
Show 6 quoted lines
> At least part of. You have to understand the branch model:
>
> git model:
>  * a branch is just a pointer to a commit
>  * you cannot say "this commit was done in that branch"
>  * what you can say is "this commit is contained in that branch"
The second point wasn't clear to me.
I had 2 out of 3 OK :)
Thanks.
-- 
--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|              http://www.obry.net
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595
Santi Béjar· Jan 30, 2009, 14:46 UTC · re: Pascal Obry · lore

Re: Finding the name of the parent branch?

2009/1/30 Pascal Obry <pascal@obry.net>:
Show 8 quoted lines
>> At least part of. You have to understand the branch model:
>>
>> git model:
>>  * a branch is just a pointer to a commit
>>  * you cannot say "this commit was done in that branch"
>>  * what you can say is "this commit is contained in that branch"
>
> The second point wasn't clear to me.
A practical example:

$ git clone path/to/project.git $ cd project $ git checkout -b bugfix-1234 origin/master $ # hack, hack, hack $ git commit -a -m "Fix for the Bug #1234" $ git push origin HEAD:master

So, you've done a commit in a local branch named bugfix-1234 and once you push it to the master branch in origin there is nothing to tell you so. A commit is defined with the current state, the old commit(s) and some metadata (author and committer) but nothing about which branch it was made, and as a branch is a pointer to a commit there is nothing more.

Santi
Michael J Gruber· Jan 30, 2009, 13:35 UTC · re: Pascal Obry · lore

Re: Finding the name of the parent branch?

Pascal Obry venit, vidit, dixit 30.01.2009 13:56:
Show 54 quoted lines
> Santi,
> 
> Thanks for you reply.
> 
>> I think your definition is not well defined. A, B and C are just
>> branches of you project, technically they are equivalent. Maybe you
> 
> Right. Yet I want to know from which branch a branch as been started.
> 
> You need this to get the proper merge-base for example:
> 
>    $ git merge-base C A
>       1
> 
>    $ git merge-base B C
>       2
> 
>    $ git merge-base B A
>       1
> 
> I always know on which topic branch I'm but, as shown above, depending on the
> parent branch passed to merge-base you do not get the same branch-point. This
> is fine.
> 
> So, when I'm in a topic branch I want to find the name of the parent
> branch. The one given
> when creating the branch:
> 
>    $ git branch B C
> 
> A "stupid" solution whould be to iterate over all branches. Looking
> for the merge-base and
> at the end output the branch having the youngest merge-base. I'm
> looking for something
> more efficient...
> 
>> are thinking that the common commits of, say A and B, really belongs
>> to A, but this is not the case they belong to both branches. In git a
>> branch is really just a pointer to a commit and by extension the
>> history, it is not a series of commits.
>>
>> Just a counterexample, just rearranging you graph:
>>
>>                        o---B
>>                        /
>>                   o---2---o---o---o---C
>>                  /
>>          ---o---1---o---o---o---A
>>
>> From you description: For B I would get C and for C I would get A.
> 
> Don't see this as a counter-example as it is exactly my example.
> 
> Did I missed something?

I think you still haven't *defined* what you mean by "parent branch". Your example alone doesn't define it, and whenever you have merge commits things are not clear:

     o---o---o---A
    /     \
---o       o---o---C
    \     /
     o---o---o---B
Now, which one is the "parent branch" of C? A+B? Similarly:
     o---A
    /
---o---C
    \
     o---B

Here it's clear which commit you want, but which branch does it belong to? A or B?

I really think this is impossible to define unambiguously in git, due to the nature of git branches, being movable tags, much different from say hg's hardwired branches (embedded in the commit object).

What you see in gitk is which branch contains a certain commit ("git branch --contains").

What you want may be the branch which differs from C by the least number of commits.

Cheers, Michael

Thomas Koch· Jan 30, 2009, 14:26 UTC · re: Pascal Obry · lore

Re: Finding the name of the parent branch?

Am Friday 30 January 2009 11:56:12 schrieb Pascal Obry:
Show 18 quoted lines
> I want to find the name of the parent branch in a script. What is the best
> way?
>
>                          o---o---o---C
>                          /
>                     o---o---o---B
>                    /
>            ---o---o---o---o---o---A
>
> For B I want to get A and for C I want to get B.
>
> All this can be found in gitk, but from a script I did not found a
> good solution.
>
> Any idea?
>
> Thanks.
> Pascal.
Isn't your problem solved by topgit?
-- 
Thomas Koch, YMC AG, http://www.ymc.ch
Phone: +41 (0)71 / 508 24 86
Pascal Obry· Jan 30, 2009, 15:58 UTC · re: Thomas Koch · lore

Re: Finding the name of the parent branch?

Thomas Koch a écrit :
> Isn't your problem solved by topgit?
Don't know! I have never used topgit. Why do you think so?
-- 
--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|              http://www.obry.net
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595

← back to recent threads