0

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
5
  • I think this a git question, not a python one Commented 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. Commented Mar 19, 2020 at 21:09
  • Is there a reason you’re using Python and not shell scripting? Commented Mar 19, 2020 at 21:11
  • 1
    I see, have you tried removing last character of test? Maybe the newline is breaking it. Commented Mar 19, 2020 at 21:11
  • As the answers state, the newline is breaking it. Commented Mar 19, 2020 at 21:12

2 Answers 2

4

Seems like the new line is not right, try:

sb.Popen(['git' , 'diff' ,'--name-status' ,test.strip(), 'HEAD'])
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.