mercurial API:如何从传入的命令中获得40位数的修订哈希



我在我的项目中使用python mercurial API。

from mercurial import ui, hg, commands
from mercurial.node import hex
user_id = ui.ui()
hg_repo = hg.repository(user_id, '/path/to/repo')
hg_repo.ui.pushbuffer()
some_is_coming = commands.incoming(hg_repo.ui, hg_repo, source='default',
                                       bundle=None, force=False)
if some_is_coming:
    output = hg_repo.ui.popbuffer()
In [95]: output
Out[95]: 'comparing with ssh:host-namensearching for changesnchangeset:   1:e74dcb2eb5e1ntag:         tipnuser:        that-is-mendate:        Fri Nov 06 12:26:53 2015 +0100nsummary:     added input.txtnn'

提取短节点信息e74dcb2eb5e1将是容易的。然而,我真正需要的是40位十六进制修订id。有什么方法可以在不首先提取回购的情况下检索这些信息吗?

您需要指定一个模板,该模板提供完整的节点哈希作为其输出的一部分。此外,commands.incoming返回一个数字错误代码,其中零表示成功。也就是说,你需要这样的东西:

from mercurial import ui, hg, commands
from mercurial.node import hex
user_id = ui.ui()
hg_repo = hg.repository(user_id, '/path/to/repo')
hg_repo.ui.pushbuffer()
command_result = commands.incoming(hg_repo.ui, hg_repo, source='default',
    bundle=None, force=False, template="json")
if command_result == 0:
    output = hg_repo.ui.popbuffer()
    print output

还有两件事:首先,您还将获得诊断输出("与…进行比较"),可以通过-q(或ui.setconfig("ui", "quiet", "yes"))进行抑制。但是,请注意,此选项也会影响默认模板,您可能需要提供自己的模板。其次,建议设置环境变量HGPLAIN,以便忽略.hgrc中的别名和默认值(请参见hg help scripting)。

或者,您可以使用hglib中实现的Mercurial命令服务器(可通过pip install python-hglib获得)。

import hglib
client = hglib.open(".")
# Standard implementation of incoming, which returns a list of tuples.
# force=False and bundle=None are the defaults, so we don't need to
# provide them here.
print client.incoming(path="default")
# Or the raw command output with a custom template.
changeset = "[ {rev|json}, {node|json}, {author|json}, {desc|json}, {branch|json}, {bookmarks|json}, {tags|json}, {date|json} ]n"
print client.rawcommand(["incoming", "-q", "-T" + changeset])

最新更新