You can use: GitPython
Or 'native way':
from subprocess import Popen, PIPE
cmd = 'git diff --name-only'
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
files_changed_list = stdout.decode("utf-8").splitlines()
Answer from David Meu on Stack OverflowYou can use: GitPython
Or 'native way':
from subprocess import Popen, PIPE
cmd = 'git diff --name-only'
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
files_changed_list = stdout.decode("utf-8").splitlines()
You could also use Python's os module.
import os
os.system("git diff --name-only | tee status")
would put the names of the changed files into a file called status.
Erm I think the other answer is better though.
An easier solution would be to use the Python subprocess module to call git. In your case, this would pull the latest code and build:
import subprocess
subprocess.call(["git", "pull"])
subprocess.call(["make"])
subprocess.call(["make", "test"])
Docs:
- subprocess - Python 2.x
- subprocess - Python 3.x
So with Python 3.5 and later, the .call() method has been deprecated.
https://docs.python.org/3.6/library/subprocess.html#older-high-level-api
The current recommended method is to use the .run() method on subprocess.
import subprocess
subprocess.run(["git", "pull"])
subprocess.run(["make"])
subprocess.run(["make", "test"])
Adding this as when I went to read the docs, the links above contradicted the accepted answer and I had to do some research. Adding my 2 cents to hopefully save someone else a bit of time.
» pip install git-adapter
Have you considered using GitPython? It's designed to handle all this nonsense for you.
import git # pip install gitpython
g = git.cmd.Git(git_dir)
g.pull()
To install the module use pip install GitPython
Project can be found here, GitPython
subprocess.Popen expects a list of the program name and arguments. You're passing it a single string, which is (with the default shell=False) equivalent to:
['git pull']
That means that subprocess tries to find a program named literally git pull, and fails to do so: In Python 3.3, your code raises the exception FileNotFoundError: [Errno 2] No such file or directory: 'git pull'. Instead, pass in a list, like this:
import subprocess
process = subprocess.Popen(["git", "pull"], stdout=subprocess.PIPE)
output = process.communicate()[0]
By the way, in Python 2.7+, you can simplify this code with the check_output convenience function:
import subprocess
output = subprocess.check_output(["git", "pull"])
Also, to use git functionality, it's by no way necessary (albeit simple and portable) to call the git binary. Consider using git-python or Dulwich.
GitPython uses the module logging. By adding logging.basicConfig(level=logging.DEBUG) before your code, it prints logs like
DEBUG:git.cmd:Popen(['git', 'fetch'], cwd=E:\path\foo, universal_newlines=False, shell=None)
If you want it to print formatted logs as you expect, you could modify GitPython's source code. I'm using Python 2.7 and my GitPython is installed at C:\Python27\lib\site-packages\git. You can run git.__file__ in REPL to find your installation directory. In the file C:\Python27\lib\site-packages\git\cmd.py, you can find a method def execute. It calls subprocess.Popen to run the command. You can modify the line log.debug before Popen or just insert print(' '.join(cmd)) at a proper line. If you are using Python 3.3 or newer, you could also refer to this answer on how to print the command with subprocess.
You could also:
pull = self.repo.git.pull('--rebase')
log.info(pull)