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
asked Mar 19, 2020 at 21:07
programminglearner
5424 silver badges25 bronze badges
-
I think this a git question, not a python oneNicolas Martinez– Nicolas Martinez2020年03月19日 21:08:37 +00:00Commented Mar 19, 2020 at 21:08
-
1@NicolasMartinez not quite. the commands work fine as git commands. the issue is calling it from the script.programminglearner– programminglearner2020年03月19日 21:09:21 +00:00Commented Mar 19, 2020 at 21:09
-
Is there a reason you’re using Python and not shell scripting?s3dev– s3dev2020年03月19日 21:11:13 +00:00Commented Mar 19, 2020 at 21:11
-
1I see, have you tried removing last character of test? Maybe the newline is breaking it.Nicolas Martinez– Nicolas Martinez2020年03月19日 21:11:28 +00:00Commented Mar 19, 2020 at 21:11
-
As the answers state, the newline is breaking it.kpie– kpie2020年03月19日 21:12:58 +00:00Commented Mar 19, 2020 at 21:12
2 Answers 2
Seems like the new line is not right, try:
sb.Popen(['git' , 'diff' ,'--name-status' ,test.strip(), 'HEAD'])
answered Mar 19, 2020 at 21:10
Eran
2,4543 gold badges23 silver badges29 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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'])
answered Mar 19, 2020 at 21:12
Nicolas Martinez
7981 gold badge7 silver badges27 bronze badges
Comments
lang-py