1

Is it possible to collect output of a git command into a Python variable?

Lets a simple command like git status which gives the list of files changed on the repo. Lets say the output of git status is following

A.cpp
B.hpp
C.cpp

Is it possible to get the above output of git status into a Python list?

I read the some git commands do not pipe the output and rather just print? Is it true even today? If a git command was piping the output, how can one collect it a Python list or variable?

Can GitPython help here?

TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
  • 1
    I used GitPython and it worked fine. I opted not to use the offered API, but rather only invoked pure git commands via passing strings to it, as it was much faster to learn and more reliable. It does nice nice wrapping over that part. If you do end up using it a lot, I think it can benefit to actually use the offered API. – Gulzar Apr 19 '21 at 10:57
  • I forgot exactly the method that allows passing the strings, but it never failed me. – Gulzar Apr 19 '21 at 10:58
  • 1
    Interesting solution with GitPython: [Get changed files using gitpython](https://stackoverflow.com/a/42792158/8106583) – wuerfelfreak Apr 19 '21 at 12:32

2 Answers2

3

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()
David Meu
  • 1,527
  • 9
  • 14
  • Be aware that the output from `git diff --name-only` has whatever encoding the names actually have, which may not always be valid UTF-8. If it *isn't* UTF-8, the exception you'll get here is perhaps the best you can do anyway, though. :-) – torek Apr 19 '21 at 16:00
1

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.