[python子进程来调用git命令

问题描述 投票:0回答:1

我正在使用git命令从日志历史记录中获取ID,然后尝试通过管道传递到另一个命令中。第一个工作正常,但其他所有工作均不正常。这是我的代码:

import subprocess as sb

commit_id = sb.Popen(['git', 'merge-base' ,'FETCH_HEAD', 'HEAD'], stdout=sb.PIPE)
test=commit_id.communicate()[0]
print(test)
sb.Popen(['git' , 'diff' ,'--name-status' ,test, 'HEAD'])

它打印b'0bf694cea03670b318eeef8369dc0a0e0c761b29\n',然后给出错误。

这是我得到的错误:

fatal: ambiguous argument '0bf694cea03670b318eeef8369dc0a0e0c761b29
': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

我不确定我要去哪里错

以下是我要实现的git命令。它们可以从Linux命令行正常工作:

git merge-base FETCH_HEAD HEAD /this returns the commit id
git diff --name-status commit_id HEAD /this returns changed files
git diff --src-prefix 'Original:' --dst-prefix 'New:' commit_id filename /this returns lines changed in files
python git subprocess popen
1个回答
2
投票

似乎新行不正确,请尝试:

sb.Popen(['git' , 'diff' ,'--name-status' ,test.strip(), 'HEAD'])

0
投票

您的测试变量后面有换行符,将其删除,它将可以正常工作

import subprocess as sb

commit_id = sb.Popen(['git', 'merge-base' ,'FETCH_HEAD', 'HEAD'], stdout=sb.PIPE)
test=commit_id.communicate()[0]
print(test)
sb.Popen(['git' , 'diff' ,'--name-status' ,test[:-1], 'HEAD'])
© www.soinside.com 2019 - 2024. All rights reserved.