How do you find the parent of a named mercurial branch?
Solution 1
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.
Solution 2
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.
Solution 3
Assuming you have Mercurial v1.7 or later, this can be done with a revset.
hg log -r "ancestor(<changeset>, <changeset>)"
where changeset
may be a branch, tag, revision id or changeset hash.
This will print out the greatest common ancestor of the two changesets.
If there is no common ancestor, then the result is empty.
Solution 4
Based on @krupan's answer I came up with the following:
hg log --rev "parents(min(branch(.)))" --template "{branch}\n"
This will print only the name of the parent branch.
-
--rev "revset expression"
specifies a revset using a revset expression. -
branch()
selects all revisions of the specified branch. -
min()
selects the first revision of the specified revision range. -
parents()
selects the parents of the specified revision range. -
.
indicates the current revision. Alternatively you can specify any revision by name. -
--template "{branch}\n"
specifies a custom template which only prints the branch name.
tcrafton
Updated on July 18, 2022Comments
-
tcrafton almost 2 years
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?
-
dls over 13 yearsthis is more concise than my answer, but I think it assumes that the feature branch came from 'default'
-
tcrafton over 13 yearsHow does this tell me the parent of branch foo? In other words, what to put in for the question marks:
hg log -r "ancestor(???, foo)"
-
tcrafton over 13 yearsInteresting. That seems to show me the most recent commit that was merged to the branch in question. Which gets me thinking, you could have merged from different branches to a given branch (foo, in this example) at different points in history. If you wanted to know which branch foo was created from this could be wrong.
-
Tim Henigan over 13 years@krupan: based on your original post, it sounded like you knew which branch
foo
was created from (in your example, it wasdefault
. So you would useancestor(default, foo)
. If you don't know where it was branched from, then you should try the answer from @dls. -
dls over 13 years@krupan: yes, I believe this would be a liability of this method. You can check out the revset help 'hg help revset' to see if you can think of another way to get at what you want.
-
dls over 13 yearsyes, you can occasionally have two parents. One way to achieve this is to perform a merge and commit the merge results to a new named branch. TortoiseHg seems to prohibit this, but you can do it from the CLI. That being said, this revset is not a bad idea.
-
Krazy Glew over 11 yearsI put the following in my .hgrc [alias] section: parent-branch = ! export REV=$1; export REV=${REV:-.} ; echo hg parent-branch ${REV}; $HG log -r "parents(min(branch(${REV})))"
-
Ti Strga about 10 years@KrazyGlew +1 for echoing what's going on instead of simply printing the result :-)
-
Krazy Glew about 10 years@TiStrga: I like to have such verbosity, but I am slightly embarrassed by it, since it violates the UNIX principle of being as quiet as possible.
-
Blaise over 6 yearsIf you only want to return the branch name (e.g. for scripting):
hg id -br 'max(ancestors(BRANCH) and not branch(BRANCH))'