0

I'm trying to script git hub pull, push, and status commands using the python subprocess. check_output command as below. Can someone please let me know what these errors are and how can I smoothly execute this script. This happens only when there are no more files to commit.

import os
from subprocess import check_output

gitzap =['git pull','git add *','git commit -m "all"','git push','git status']

for command in gitzap:
    #print (command)
    # executioner = os.system(command).pop()
    executioner = check_output(command, stdin=None, stderr=None, shell=True,
                               universal_newlines=True, timeout=None, encoding='utf-8')
    print ('\n' + str(command) + '\n***************success***************\n')
    print (str(executioner))

Output is as follows:

~/documents/github/goodcode/githelpcommands $gitzap

git pull
***************success***************

Already up to date.


git add *
***************success***************


Traceback (most recent call last):
  File "/home/xxxxxxxxxx/documents/github/goodcode/githelpcommands/gitzap.py", line 10, in <module>
    executioner = check_output(command, stdin=None, stderr=None, shell=True, universal_newlines=True, timeout=None, encoding='utf-8')
  File "/usr/lib/python3.6/subprocess.py", line 356, in check_output
    **kwargs).stdout
  File "/usr/lib/python3.6/subprocess.py", line 438, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command 'git commit -m "all"' returned non-zero exit status 1.
martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

0

Your output and the description of when it happens tell you exactly what the error is.

git commit will return a non-zero exit status code when there are no files to commit, therefore you get:

subprocess.CalledProcessError: Command 'git commit -m "all"' returned non-zero exit status 1.

You have to check if there are files to commit first. For example, by quickly googling around - you could use the solution from this thread.

I would go for handling the error instead though. Perhaps create a handler that prints different error messages for different commands?

Kacperito
  • 1,277
  • 1
  • 10
  • 27