1

我在我的项目中使用 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-name\nsearching for changes\nchangeset:   1:e74dcb2eb5e1\ntag:         tip\nuser:        that-is-me\ndate:        Fri Nov 06 12:26:53 2015 +0100\nsummary:     added input.txt\n\n'

提取短节点信息e74dcb2eb5e1将很容易。然而,我真正需要的是 40 位十六进制修订 ID。有没有办法在不先拉回购的情况下检索这些信息?

4

1 回答 1

3

您需要指定一个模板,该模板提供完整节点哈希作为其输出的一部分。此外,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)。

或者,您可以使用 Mercurial命令服务器hglib(可通过 获得)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])
于 2015-11-06T16:34:50.113 回答