2020-01-11 09:44:20 +00:00
|
|
|
import os
|
2020-01-11 11:14:01 +00:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2020-01-11 09:44:20 +00:00
|
|
|
|
|
|
|
from voussoirkit import winwhich
|
|
|
|
|
|
|
|
GIT = winwhich.which('git')
|
|
|
|
|
2020-01-11 23:04:54 +00:00
|
|
|
# https://git-scm.com/docs/git-status#_short_format
|
|
|
|
# Here is an example of typical `git status --short` output:
|
2020-01-11 10:23:07 +00:00
|
|
|
#
|
2020-01-11 23:04:54 +00:00
|
|
|
# M file1
|
|
|
|
# D file2
|
|
|
|
# A file4
|
|
|
|
# ?? file3
|
2020-01-11 10:23:07 +00:00
|
|
|
|
2020-01-21 05:53:49 +00:00
|
|
|
# Here is an example of typical `git log --oneline --branches --not --remotes` output:
|
|
|
|
# Only the commits that haven't been pushed to a remote are shown!
|
|
|
|
# Thank you cxreg https://stackoverflow.com/a/3338774
|
2020-01-11 10:23:07 +00:00
|
|
|
#
|
2020-01-21 05:53:49 +00:00
|
|
|
# a755f32 Ready to publish
|
|
|
|
# 5602e1e Another commit message
|
|
|
|
# a32a372 My commit message
|
2020-01-11 10:23:07 +00:00
|
|
|
|
|
|
|
def checkup_committed(directory):
|
2020-01-11 09:44:20 +00:00
|
|
|
os.chdir(directory)
|
2020-01-13 04:01:05 +00:00
|
|
|
command = [GIT, 'status', '--short', '--untracked-files=all']
|
2020-01-11 09:44:20 +00:00
|
|
|
output = subprocess.check_output(command, stderr=subprocess.STDOUT)
|
2020-01-11 10:23:07 +00:00
|
|
|
|
2020-01-11 23:04:54 +00:00
|
|
|
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):
|
2020-01-11 09:44:20 +00:00
|
|
|
committed = True
|
|
|
|
details = ''
|
|
|
|
else:
|
2020-01-11 10:23:07 +00:00
|
|
|
committed = False
|
2020-01-21 05:53:49 +00:00
|
|
|
details = f'(+{added}, -{deleted}, ~{modified})'
|
2020-01-11 10:23:07 +00:00
|
|
|
|
|
|
|
return (committed, details)
|
2020-01-11 09:44:20 +00:00
|
|
|
|
2020-01-11 10:23:07 +00:00
|
|
|
def checkup_pushed(directory):
|
|
|
|
os.chdir(directory)
|
2020-01-21 05:53:49 +00:00
|
|
|
command = [GIT, 'log', '--oneline', '--branches', '--not', '--remotes']
|
2020-01-11 09:44:20 +00:00
|
|
|
output = subprocess.check_output(command, stderr=subprocess.STDOUT)
|
2020-01-21 05:53:49 +00:00
|
|
|
|
|
|
|
commits = sum(1 for line in output.splitlines() if line.strip())
|
|
|
|
|
|
|
|
if commits == 0:
|
|
|
|
pushed = True
|
|
|
|
details = ''
|
|
|
|
else:
|
|
|
|
pushed = False
|
|
|
|
details = f'(↑{commits})'
|
|
|
|
|
|
|
|
return (pushed, details)
|
2020-01-11 10:23:07 +00:00
|
|
|
|
|
|
|
def checkup(directory):
|
2020-01-21 05:53:49 +00:00
|
|
|
(committed, commit_details) = checkup_committed(directory)
|
|
|
|
(pushed, push_details) = checkup_pushed(directory)
|
|
|
|
return {
|
|
|
|
'committed': committed,
|
|
|
|
'commit_details': commit_details,
|
|
|
|
'pushed': pushed,
|
|
|
|
'push_details': push_details,
|
|
|
|
}
|
2020-01-11 09:44:20 +00:00
|
|
|
|
2020-01-11 10:23:07 +00:00
|
|
|
def main(argv):
|
2020-01-13 04:06:14 +00:00
|
|
|
directories_file = os.path.join(os.path.dirname(__file__), 'gitcheckup.txt')
|
|
|
|
try:
|
|
|
|
handle = open(directories_file, 'r')
|
|
|
|
except FileNotFoundError:
|
|
|
|
print(f'Please put your git repo locations in {directories_file}.')
|
|
|
|
return 1
|
|
|
|
|
|
|
|
directories = handle.readlines()
|
|
|
|
handle.close()
|
|
|
|
|
|
|
|
directories = [line.strip() for line in directories]
|
|
|
|
directories = [line for line in directories if line]
|
|
|
|
|
|
|
|
for directory in directories:
|
2020-01-11 09:44:20 +00:00
|
|
|
result = checkup(directory)
|
2020-01-11 10:58:08 +00:00
|
|
|
committed = 'C' if result['committed'] else ' '
|
|
|
|
pushed = 'P' if result['pushed'] else ' '
|
2020-01-21 05:53:49 +00:00
|
|
|
|
|
|
|
details = []
|
|
|
|
if result['commit_details']:
|
|
|
|
details.append(result['commit_details'])
|
|
|
|
if result['push_details']:
|
|
|
|
details.append(result['push_details'])
|
|
|
|
details = ' '.join(details)
|
|
|
|
details = (' ' + details).rstrip()
|
2020-01-11 09:44:20 +00:00
|
|
|
print(f'[{committed}][{pushed}] {directory}{details}')
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
raise SystemExit(main(sys.argv[1:]))
|