4

I am trying to run git clone command using subprocess.check_output() so that I can verify if it clones successfully or not, but its throwing error.

Command 'git clone <url>' returned non-zero exit status 128

I am doing:

resp = subprocess.check_output('git clone <url>', shell=True)

How can I get the output of git clone so that I can verify it worked fine or not and can catch any errors if any.

Thanks

S Andrew
  • 5,592
  • 27
  • 115
  • 237
  • How are you running the git clone command? Like `proc = subprocess.Popen(['git', 'clone', url], stdout=subprocess.PIPE)`? Then you can use `proc.stdout.read()`. – FHTMitchell Aug 22 '18 at 08:29
  • @FHTMitchell I am using subprocess.check_output not with Popen, but I'll try it and will update. – S Andrew Aug 22 '18 at 08:30
  • @FHTMitchell Can you answer the question by giving an example – S Andrew Aug 22 '18 at 08:55

1 Answers1

5

So the best method to read a subprocess' output is to use subprocess.PIPE. For example

import subprocess
from collections import namedtuple

def git_clone(url):
    process = subprocess.Popen(['git', 'clone', url], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    return namedtuple('Std', 'out, err')(process.stdout.read(), process.stderr.read())

# test on fake url
out, err = git_clone('http://fake.url')
print('out = {}\nerr = {}'.format(out, err)

outputs:

out = b''
err = b"Cloning into 'fake.url'...\nfatal: unable to access 'http://fake.url/': Couldn't resolve host 'fake.url'\n"

Hence you can test success by changing the function to be

from warnings import warn

def git_clone(url):

    process = subprocess.Popen(['git', 'clone', url], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    if not process.stdout.read():
        warn(process.stderr.read())
        return False

    return True
FHTMitchell
  • 11,793
  • 2
  • 35
  • 47