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, defaul开发者_运维技巧t, 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?
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.
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.
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.
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.
精彩评论