2020-01-28 08:57:24 +00:00
|
|
|
import argparse
|
2020-01-11 09:44:20 +00:00
|
|
|
import os
|
2020-01-11 11:14:01 +00:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2020-01-28 09:16:33 +00:00
|
|
|
import types
|
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-28 08:57:24 +00:00
|
|
|
def checkup_committed():
|
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
|
|
|
|
|
2020-01-28 09:16:33 +00:00
|
|
|
committed = (added, modified, deleted) == (0, 0, 0)
|
|
|
|
details = types.SimpleNamespace(added=added, deleted=deleted, modified=modified)
|
2020-01-11 10:23:07 +00:00
|
|
|
|
|
|
|
return (committed, details)
|
2020-01-11 09:44:20 +00:00
|
|
|
|
2020-01-28 08:57:24 +00:00
|
|
|
def checkup_pushed():
|
2020-01-28 09:12:24 +00:00
|
|
|
command = [GIT, 'rev-parse', '@', '@{u}']
|
|
|
|
output = subprocess.check_output(command, stderr=subprocess.STDOUT)
|
|
|
|
(my_head, remote_head) = output.strip().decode().splitlines()
|
2020-01-21 05:53:49 +00:00
|
|
|
|
2020-01-28 08:57:24 +00:00
|
|
|
command = [GIT, 'merge-base', '@', '@{u}']
|
|
|
|
merge_base = subprocess.check_output(command, stderr=subprocess.STDOUT)
|
|
|
|
merge_base = merge_base.strip().decode()
|
|
|
|
|
|
|
|
if my_head == remote_head:
|
|
|
|
to_push = 0
|
|
|
|
to_pull = 0
|
|
|
|
else:
|
|
|
|
to_push = len(git_commits_between(merge_base, my_head))
|
|
|
|
to_pull = len(git_commits_between(merge_base, remote_head))
|
|
|
|
|
2020-01-28 09:16:33 +00:00
|
|
|
pushed = (to_push, to_pull) == (0, 0)
|
|
|
|
details = types.SimpleNamespace(
|
|
|
|
to_push=to_push,
|
|
|
|
to_pull=to_pull,
|
|
|
|
)
|
2020-01-21 05:53:49 +00:00
|
|
|
|
|
|
|
return (pushed, details)
|
2020-01-11 10:23:07 +00:00
|
|
|
|
2020-01-28 08:57:24 +00:00
|
|
|
def git_commits_between(a, b):
|
|
|
|
command = [GIT, 'log', '--oneline', f'{a}..{b}']
|
|
|
|
output = subprocess.check_output(command, stderr=subprocess.STDOUT)
|
|
|
|
lines = output.strip().decode().splitlines()
|
|
|
|
return lines
|
|
|
|
|
|
|
|
def git_fetch():
|
|
|
|
command = [GIT, 'fetch', '--all']
|
|
|
|
output = subprocess.check_output(command, stderr=subprocess.STDOUT)
|
|
|
|
|
|
|
|
def checkup(directory, do_fetch=False):
|
|
|
|
os.chdir(directory)
|
|
|
|
if do_fetch:
|
|
|
|
git_fetch()
|
|
|
|
(committed, commit_details) = checkup_committed()
|
|
|
|
(pushed, push_details) = checkup_pushed()
|
2020-01-28 09:16:33 +00:00
|
|
|
return types.SimpleNamespace(
|
|
|
|
committed=committed,
|
|
|
|
commit_details=commit_details,
|
|
|
|
pushed=pushed,
|
|
|
|
push_details=push_details,
|
|
|
|
)
|
2020-01-11 09:44:20 +00:00
|
|
|
|
2020-01-28 09:14:35 +00:00
|
|
|
def read_directories_file():
|
2020-01-13 04:06:14 +00:00
|
|
|
directories_file = os.path.join(os.path.dirname(__file__), 'gitcheckup.txt')
|
|
|
|
|
2020-01-28 09:42:37 +00:00
|
|
|
handle = open(directories_file, 'r')
|
2020-01-13 04:06:14 +00:00
|
|
|
directories = handle.readlines()
|
|
|
|
handle.close()
|
|
|
|
|
|
|
|
directories = [line.strip() for line in directories]
|
|
|
|
directories = [line for line in directories if line]
|
|
|
|
|
2020-01-28 09:14:35 +00:00
|
|
|
return directories
|
|
|
|
|
|
|
|
def gitcheckup(do_fetch=False):
|
2020-01-28 09:42:37 +00:00
|
|
|
try:
|
|
|
|
directories = read_directories_file()
|
|
|
|
except FileNotFoundError as exc:
|
|
|
|
print(f'Please put your git repo locations in {exc.filename}.')
|
|
|
|
return 1
|
2020-01-28 09:14:35 +00:00
|
|
|
|
2020-01-13 04:06:14 +00:00
|
|
|
for directory in directories:
|
2020-01-30 00:39:31 +00:00
|
|
|
try:
|
|
|
|
result = checkup(directory, do_fetch=do_fetch)
|
|
|
|
except subprocess.CalledProcessError as exc:
|
|
|
|
raise Exception(exc.output)
|
|
|
|
|
2020-01-28 09:16:33 +00:00
|
|
|
committed = 'C' if result.committed else ' '
|
|
|
|
pushed = 'P' if result.pushed else ' '
|
2020-01-21 05:53:49 +00:00
|
|
|
|
|
|
|
details = []
|
2020-01-28 09:16:33 +00:00
|
|
|
|
|
|
|
commit_details = []
|
|
|
|
if result.commit_details.added: commit_details.append(f'+{result.commit_details.added}')
|
|
|
|
if result.commit_details.deleted: commit_details.append(f'-{result.commit_details.deleted}')
|
|
|
|
if result.commit_details.modified: commit_details.append(f'~{result.commit_details.modified}')
|
|
|
|
commit_details = ', '.join(commit_details)
|
2020-01-28 20:41:34 +00:00
|
|
|
if commit_details: details.append(f'({commit_details})')
|
2020-01-28 09:16:33 +00:00
|
|
|
|
|
|
|
push_details = []
|
|
|
|
if result.push_details.to_push: push_details.append(f'↑{result.push_details.to_push}')
|
|
|
|
if result.push_details.to_pull: push_details.append(f'↓{result.push_details.to_pull}')
|
|
|
|
push_details = ', '.join(push_details)
|
2020-01-28 20:41:34 +00:00
|
|
|
if push_details: details.append(f'({push_details})')
|
2020-01-28 09:16:33 +00:00
|
|
|
|
2020-01-21 05:53:49 +00:00
|
|
|
details = ' '.join(details)
|
|
|
|
details = (' ' + details).rstrip()
|
2020-01-11 09:44:20 +00:00
|
|
|
print(f'[{committed}][{pushed}] {directory}{details}')
|
|
|
|
|
2020-01-28 08:57:24 +00:00
|
|
|
def gitcheckup_argparse(args):
|
|
|
|
return gitcheckup(do_fetch=args.do_fetch)
|
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
|
|
|
|
|
|
parser.add_argument('--fetch', dest='do_fetch', action='store_true')
|
|
|
|
parser.set_defaults(func=gitcheckup_argparse)
|
|
|
|
|
|
|
|
args = parser.parse_args(argv)
|
|
|
|
return args.func(args)
|
|
|
|
|
2020-01-11 09:44:20 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
raise SystemExit(main(sys.argv[1:]))
|