Split checkup into smaller functions, add output samples.

This commit is contained in:
voussoir 2020-01-11 02:23:07 -08:00
parent 78ed021d9f
commit 72b9f34bfe

View file

@ -17,19 +17,47 @@ DIRECTORIES = [
r'D:\Git\YCDL', r'D:\Git\YCDL',
] ]
def checkup(directory): # Here is an example of typical git status output:
#
# On branch master
# Changes not staged for commit:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: file1
# deleted: file2
#
# Untracked files:
# (use "git add <file>..." 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)
# Here is an example of typical git log -1 --decorate output:
#
# commit f8fddd0de4283a251a8beb35493bd1bd3a4c925a (HEAD -> master)
# Author: My Name <mymail@example.net>
# Date: Sat Jan 11 01:59:07 2020 -0800
#
# I made some changes.
def checkup_committed(directory):
os.chdir(directory) os.chdir(directory)
command = [GIT, 'status'] command = [GIT, 'status']
output = subprocess.check_output(command, stderr=subprocess.STDOUT) output = subprocess.check_output(command, stderr=subprocess.STDOUT)
if b'nothing to commit' in output: if b'nothing to commit' in output:
committed = True committed = True
details = '' details = ''
else: else:
modified = output.count(b'modified:') committed = False
deleted = output.count(b'deleted:')
if b'Untracked files' in output: if b'Untracked files' in output:
added = output.split(b'in what will be committed)')[1] 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'no changes added to commit')[0]
added = added.split(b'nothing added to commit but')[0] added = added.split(b'nothing added to commit but')[0]
added = [x.strip() for x in added.splitlines()] added = [x.strip() for x in added.splitlines()]
@ -38,30 +66,34 @@ def checkup(directory):
else: else:
added = 0 added = 0
committed = False modified = output.count(b'modified:')
details = f'(+{added}, -{deleted}, ~{modified})' deleted = output.count(b'deleted:')
details = f'+{added}, -{deleted}, ~{modified}'
return (committed, details)
def checkup_pushed(directory):
os.chdir(directory)
command = [GIT, 'log', '-1', '--decorate'] command = [GIT, 'log', '-1', '--decorate']
output = subprocess.check_output(command, stderr=subprocess.STDOUT) output = subprocess.check_output(command, stderr=subprocess.STDOUT)
headline = output.splitlines()[0] headline = output.splitlines()[0]
locations = headline.split(b'(')[-1].split(b')')[0] refs = headline.split(b'(')[-1].split(b')')[0]
if b',' in locations: return any (b'/' in ref for ref in refs.split(b','))
pushed = True
else: def checkup(directory):
pushed = False (committed, details) = checkup_committed(directory)
pushed = checkup_pushed(directory)
return {'committed': committed, 'pushed': pushed, 'details': details} return {'committed': committed, 'pushed': pushed, 'details': details}
def checkup_all(): def main(argv):
print('[C][P]') print('[C][P]')
for directory in DIRECTORIES: for directory in DIRECTORIES:
result = checkup(directory) result = checkup(directory)
committed = 'x' if result['committed'] else ' ' committed = 'x' if result['committed'] else ' '
pushed = 'x' if result['pushed'] else ' ' pushed = 'x' if result['pushed'] else ' '
details = (' ' + result['details']) if result['details'] else '' details = result['details']
details = f' ({details})' if details else ''
print(f'[{committed}][{pushed}] {directory}{details}') print(f'[{committed}][{pushed}] {directory}{details}')
def main(argv):
checkup_all()
if __name__ == '__main__': if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:])) raise SystemExit(main(sys.argv[1:]))