From ba5bbb2a16606ccb6f9c3103ace35a91195f8db5 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sat, 11 Jan 2020 15:04:54 -0800 Subject: [PATCH] Use `git status --short` for easier parsing. Thank you kringel8. --- gitcheckup.py | 59 ++++++++++++++++++++++----------------------------- 1 file changed, 25 insertions(+), 34 deletions(-) diff --git a/gitcheckup.py b/gitcheckup.py index c7fab7f..8ff2538 100644 --- a/gitcheckup.py +++ b/gitcheckup.py @@ -17,26 +17,15 @@ DIRECTORIES = [ r'D:\Git\YCDL', ] -# Here is an example of typical git status output: +# https://git-scm.com/docs/git-status#_short_format +# Here is an example of typical `git status --short` output: # -# On branch master -# Changes not staged for commit: -# (use "git add/rm ..." to update what will be committed) -# (use "git checkout -- ..." to discard changes in working directory) -# -# modified: file1 -# deleted: file2 -# -# Untracked files: -# (use "git add ..." to include in what will be committed) -# -# file3 -# -# no changes added to commit (use "git add" and/or "git commit -a") -# --- ALTERNATE LAST LINE --- -# nothing added to commit but untracked files present (use "git add" to track) +# M file1 +# D file2 +# A file4 +# ?? file3 -# Here is an example of typical git log -1 --decorate output: +# Here is an example of typical `git log -1 --decorate` output: # # commit f8fddd0de4283a251a8beb35493bd1bd3a4c925a (HEAD -> master) # Author: My Name @@ -46,28 +35,30 @@ DIRECTORIES = [ def checkup_committed(directory): os.chdir(directory) - command = [GIT, 'status'] + command = [GIT, 'status', '--short'] output = subprocess.check_output(command, stderr=subprocess.STDOUT) - if b'nothing to commit' in output: + added = 0 + modified = 0 + deleted = 0 + for line in output.splitlines(): + status = line.split()[0].strip().decode('ascii') + + # These are ifs instead of elifs because you might have a file that is + # added in the index but deleted on disk, etc. Anyway these numbers + # don't need to be super accurate, just enough to remind you to commit. + if {'A', '?'}.intersection(status): + added += 1 + if {'M', 'R', '!'}.intersection(status): + modified += 1 + if {'D'}.intersection(status): + deleted += 1 + + if (added, modified, deleted) == (0, 0, 0): committed = True details = '' - else: committed = False - - if b'Untracked files' in output: - added = output.split(b'include in what will be committed)')[1] - added = added.split(b'no changes added to commit')[0] - added = added.split(b'nothing added to commit but')[0] - added = [x.strip() for x in added.splitlines()] - added = [x for x in added if x] - added = len(added) - else: - added = 0 - - modified = output.count(b'modified:') - deleted = output.count(b'deleted:') details = f'+{added}, -{deleted}, ~{modified}' return (committed, details)