我在我的项目中使用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。有没有办法检索这些信息而不先拉回购?
答案 0 :(得分: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
)。
或者,您可以使用hglib
中实施的Mercurial command server(可通过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])