如何用Mercurial实现git describe --contains的等效功能?
在Mercurial中实现类似
git describe --contains的功能 Mercurial没有直接对应git describe --contains的内置命令,但可以通过组合revset语法和日志命令来实现相同效果。
核心实现思路
git describe --contains <commit>的核心是找到位于指定提交之后、且包含该提交的tag(即该提交是tag对应变更集的祖先),默认返回符合条件的tag。我们可以利用Mercurial的revset筛选这类tag,再通过模板输出结果。
具体命令
1. 找到包含指定提交的最新tag
如果需要返回和目标提交距离最近的后续最新tag,执行:
hg log -r "reverse(tag() and descendants(<REV>))" --limit 1 --template "{tag}\n"
参数解释:
tag():筛选所有tag对应的变更集descendants(<REV>):筛选指定提交<REV>的所有后代变更集(包括自身)reverse(...):将结果按提交时间从新到旧排序--limit 1:只取第一个结果(最新的符合条件的tag)--template "{tag}\n":仅输出tag名称
2. 找到包含指定提交的最早tag
如果需要返回目标提交之后第一个出现的tag(贴合git describe --contains的官方描述),可以去掉reverse:
hg log -r "tag() and descendants(<REV>)" --limit 1 --template "{tag}\n"
3. 排除本地tag(可选)
如果需要排除仅本地存在的tag,在revset中加入not local():
hg log -r "reverse(tag() and not local() and descendants(<REV>))" --limit 1 --template "{tag}\n"
为什么{latesttag}模板不行
hg log -r <REV> --template '{latesttag}'返回的是从指定提交往前追溯的最近tag(即该提交之前的tag),和git describe --contains的需求方向完全相反,因此无法满足需求。
内容的提问来源于stack exchange,提问作者Yann Droneaud
相关产品推荐
相关产品推荐

