Skip to content

Instantly share code, notes, and snippets.

@mofosyne
Last active November 15, 2022 10:41
Show Gist options
  • Save mofosyne/cdb18587ab6dd0457d63afe6219a320d to your computer and use it in GitHub Desktop.
Save mofosyne/cdb18587ab6dd0457d63afe6219a320d to your computer and use it in GitHub Desktop.
dependency free subprocess parsing of git output in python
# gitsubprocess.py
# Dependency free subprocess parsing of git output in python
# Public Domain. Intended to be copy pasted into various internal build scripts as needed.
# Feel free to suggest changes https://gist.github.com/mofosyne/cdb18587ab6dd0457d63afe6219a320d
#
# By Brian Khuu 2022
#
# ## Staging and working file status type quick reference ##
# Reference: https://git-scm.com/docs/git-status#_short_format
# ' ' = unmodified
# M = modified
# T = file type changed (regular file, symbolic link or submodule)
# A = added
# D = deleted
# R = renamed
# C = copied (if config option status.renames is set to "copies")
# U = updated but unmerged
# ? = not tracked
# ! = ignored
import subprocess
def git_repo_status():
# git porcelain reference: https://git-scm.com/docs/git-status#_porcelain_format_version_1
status_entries = subprocess.check_output(['git', 'status', '--porcelain=v1']).decode('utf-8').splitlines()
status_output = []
for line in status_entries:
status_output.append({'staging':line[0], 'working':line[1], 'filepath':line[3:]})
return status_output
def git_repo_status_get_staging():
return [x for x in git_repo_status() if x['staging'] != ' ' and x['staging'] != '?' and x['staging'] != '!']
def git_repo_status_get_working():
return [x for x in git_repo_status() if x['working'] != ' ' and x['working'] != '?' and x['working'] != '!']
print(git_repo_status())
print(git_repo_status_get_staging())
print(git_repo_status_get_working())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment