Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you find the parent of a named mercurial branch?

At some point in time you have a mercurial named branch, let's call it, default, and you create a named branch from default, let's call it, foo, and you make some commits on foo. That work kind of looks like this:

hg update default hg branch foo hg commit -m "created branch foo" # work, work hg commit # work work hg commit 

Meanwhile others are making commits on default. Now someone else is told to do some work on foo and they want to see if and what might need to be merged from foo's parent branch. If they knew that foo came from default they could just run:

hg merge --preview default 

but they don't know where foo came from. They could run:

hg glog 

or fire up tortoisehg and trace the line back to foo's parent, but is there a command they could run that would just tell them the parent branch of foo?

like image 980
krupan Avatar asked Mar 31 '11 19:03

krupan


People also ask

What is mercurial branch?

Branches occur if lines of development diverge. The term "branch" may thus refer to a "diverged line of development". For Mercurial, a "line of development" is a linear sequence of consecutive changesets. Combining a line of development into an existing one is called merging. Creating a branch.

What is mercurial default branch?

Mercurial's main branch is called "default" and is analogous to "trunk" in SVN, "HEAD" in CVS, and "master" in Git. If you try to use some other name for your "main" branch, users will get a more or less random branch when they clone and commit things in the wrong place, which is generally undesirable.

How do I change my branch on mercurial?

From the main menu, select Hg | Mercurial | Update to. In the Switch Working Directory dialog that opens, specify the target working directory: To switch to another line of development, choose Branch and select the desired branch from the list.


2 Answers

Thanks for the revset examples. I fiddled around and found this solution that I kind of like:

hg log -r "parents(min(branch(foo)))" 

which will print the revision that the first commit on the foo branch was based on. You can look for the branch (or lack thereof indicating the default branch) in that changeset and that is the parent branch of foo (at least the way parent branch is defined in the question).

But I have this nagging feeling...parents() could return more than one changeset. Could the first commit on a branch ever have two parents? I can't think of how that could happen.

like image 89
krupan Avatar answered Oct 13 '22 01:10

krupan


You could use revsets to find this information, provided you're using Mercurial v1.6.0 or later. For example:

hg update BRANCH hg log -r "max(ancestors(BRANCH) and not branch(BRANCH))" 

this will print out a single revision, the "branch point" of your feature branch.

like image 34
dls Avatar answered Oct 13 '22 00:10

dls