简体   繁体   中英

python subprocess to call git commands

I am using a git command to get an ID from log history and then trying to pipe into into another command. The first one works fine but everything else is not. Here is my code:

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'])

It prints b'0bf694cea03670b318eeef8369dc0a0e0c761b29\\n' and then gives an error.

Here is the error I am getting:

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>...]'

I'm not sure where I'm going wrong

Here are the git commands I am trying to implement. They work fine from Linux command line:

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

好像换行不对,试试:

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

Your test variable has a trailing newline, strip it and it will work fine

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'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM