Abhishek Gupta
Abhishek Gupta

Reputation: 6615

git show not working with python check_output

I am writing a python script in which I have to read the output of the git show command from the script. I decided to use python's subprocess.check_output function.

But its giving me No such file or directory error.

Running from python:

>>> import subprocess
>>> subprocess.check_output(['pwd'])
'/Users/aapa/Projects/supertext\n'
>>> subprocess.check_output(['git show', 'c9a89aa:supertext/src/com/stxt/supercenter/rest/api/bootstrap/BootstrapDTO.java'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 566, in check_output
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 709, in __init__
    errread, errwrite)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1326, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
>>>

Running directly:

$ pwd
/Users/aapa/Projects/supertext
$ git show c9a89aa:supertext/src/com/stxt/supercenter/rest/api/bootstrap/BootstrapDTO.java
package com.stxt.supercenter.rest.api.bootstrap;

import com.google.common.collect.Maps;
import com.stxt.base.rolepermission.enums.Role;
import com.stxt.superbase.profile.agent.bean.Agent;
import com.stxt.supercenter.rest.api.profile.agnet.AgentDTO;

import java.util.Arrays;
import java.util.Map;
.
.
.

One thing to point out git show outputs in vi style i.e. not the complete file is getting printed directly but the part of file is printed to cover the complete scree with a : in the end to print next line or to quit.

Why I am getting error using check_output?

Upvotes: 0

Views: 1551

Answers (1)

shevron
shevron

Reputation: 3673

Try this:

subprocess.check_output(['git', 'show', 'c9a89aa:supertext/src/com/stxt/supercenter/rest/api/bootstrap/BootstrapDTO.java'])

Otherwise, your code tries to execute a command literally containing a space ("git show") instead of the command git with show as its first argument.

Upvotes: 2

Related Questions