2020-08-20 18:10:55 +00:00
|
|
|
'''
|
2020-10-13 05:15:12 +00:00
|
|
|
gitcheckup
|
|
|
|
==========
|
|
|
|
|
2020-08-20 18:10:55 +00:00
|
|
|
This program helps you check the commit and push status of your favorite git
|
|
|
|
repositories. The output looks like this:
|
|
|
|
|
|
|
|
[ ][P] D:\\Git\\cmd (~1)
|
|
|
|
[C][P] D:\\Git\\Etiquette
|
|
|
|
[ ][P] D:\\Git\\voussoirkit (+1)
|
|
|
|
[C][ ] D:\\Git\\YCDL (↑3)
|
|
|
|
|
|
|
|
To specify the list of git directories, you may either:
|
|
|
|
- Create a gitcheckup.txt file in the same directory as this file, where every
|
|
|
|
line contains an absolute path to the directory, or
|
2020-10-13 05:15:12 +00:00
|
|
|
- Pass directories as a series of positional arguments to this program.
|
|
|
|
|
|
|
|
> gitcheckup.py <flags>
|
|
|
|
> gitcheckup.py dir1 dir2 <flags>
|
2020-08-20 18:10:55 +00:00
|
|
|
|
|
|
|
flags:
|
|
|
|
--fetch:
|
|
|
|
Run `git fetch --all` in each directory.
|
|
|
|
|
|
|
|
--pull:
|
|
|
|
Run `git pull --all` in each directory.
|
|
|
|
|
2020-10-13 05:15:12 +00:00
|
|
|
--push:
|
|
|
|
Run `git push` in each directory.
|
|
|
|
|
2020-09-02 16:32:16 +00:00
|
|
|
--add path:
|
|
|
|
Add path to the gitcheckup.txt file.
|
|
|
|
|
|
|
|
--remove path:
|
|
|
|
Remove path from the gitcheckup.txt file.
|
|
|
|
|
2020-08-20 18:10:55 +00:00
|
|
|
Examples:
|
|
|
|
> gitcheckup
|
|
|
|
> gitcheckup --fetch
|
|
|
|
> gitcheckup D:\\Git\\cmd D:\\Git\\YCDL --pull
|
|
|
|
'''
|
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-11 09:44:20 +00:00
|
|
|
|
2020-08-20 18:10:55 +00:00
|
|
|
from voussoirkit import betterhelp
|
2020-02-19 20:49:30 +00:00
|
|
|
from voussoirkit import dotdict
|
2020-09-02 16:32:16 +00:00
|
|
|
from voussoirkit import pathclass
|
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-02-19 22:06:11 +00:00
|
|
|
class GitCheckupException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class NoConfigFile(GitCheckupException):
|
|
|
|
def __str__(self):
|
|
|
|
return f'Please put your git repo locations in "{self.args[0]}".'
|
|
|
|
|
|
|
|
class NoUpstreamBranch(GitCheckupException):
|
2020-02-19 20:49:30 +00:00
|
|
|
def __str__(self):
|
2020-02-19 22:06:11 +00:00
|
|
|
return f'No upstream branch for {self.args[0]}.'
|
2020-02-19 20:01:13 +00:00
|
|
|
|
2020-02-19 21:31:57 +00:00
|
|
|
# HELPERS
|
|
|
|
################################################################################
|
2020-02-05 02:58:11 +00:00
|
|
|
def check_output(command):
|
|
|
|
output = subprocess.check_output(command, stderr=subprocess.STDOUT)
|
2020-02-19 22:20:54 +00:00
|
|
|
output = output.decode().strip()
|
2020-02-05 02:58:11 +00:00
|
|
|
return output
|
|
|
|
|
2020-09-02 16:32:16 +00:00
|
|
|
def add_directory(directory):
|
|
|
|
'''
|
|
|
|
Add a directory to the gitcheckup.txt file, creating that file if it does
|
|
|
|
not exist.
|
|
|
|
'''
|
|
|
|
directory = pathclass.Path(directory)
|
|
|
|
|
|
|
|
try:
|
|
|
|
directories = set(read_directories_file())
|
|
|
|
except NoConfigFile:
|
|
|
|
directories = set()
|
|
|
|
|
|
|
|
directories.add(directory)
|
|
|
|
write_directories_file(directories)
|
|
|
|
|
|
|
|
def remove_directory(directory):
|
|
|
|
'''
|
|
|
|
Remove a directory from the gitcheckup.txt file.
|
|
|
|
Raise NoConfigFile if it does not exist.
|
|
|
|
'''
|
|
|
|
directory = pathclass.Path(directory)
|
|
|
|
directories = set(read_directories_file())
|
|
|
|
try:
|
|
|
|
directories.remove(directory)
|
|
|
|
except KeyError:
|
|
|
|
return
|
|
|
|
write_directories_file(directories)
|
|
|
|
|
2020-02-19 21:31:57 +00:00
|
|
|
def read_directories_file():
|
2020-09-02 16:32:16 +00:00
|
|
|
'''
|
|
|
|
Return a list of pathclass.Path from the lines of gitcheckup.txt.
|
|
|
|
Raise NoConfigFile if it does not exist.
|
|
|
|
'''
|
|
|
|
directories_file = pathclass.Path(__file__).parent.with_child('gitcheckup.txt')
|
2020-02-19 21:31:57 +00:00
|
|
|
|
2020-02-19 22:06:11 +00:00
|
|
|
try:
|
2020-09-21 01:27:28 +00:00
|
|
|
handle = directories_file.open('r', encoding='utf-8')
|
2020-02-19 22:06:11 +00:00
|
|
|
except FileNotFoundError as exc:
|
|
|
|
raise NoConfigFile(exc.filename) from exc
|
|
|
|
|
|
|
|
with handle:
|
|
|
|
directories = handle.readlines()
|
2020-02-19 21:31:57 +00:00
|
|
|
|
|
|
|
directories = [line.strip() for line in directories]
|
|
|
|
directories = [line for line in directories if line]
|
2020-09-02 16:32:16 +00:00
|
|
|
directories = [pathclass.Path(line) for line in directories]
|
2020-02-19 21:31:57 +00:00
|
|
|
|
|
|
|
return directories
|
|
|
|
|
2020-09-02 16:32:16 +00:00
|
|
|
def write_directories_file(directories):
|
|
|
|
'''
|
|
|
|
Write a list of directories to the gitcheckup.txt file.
|
|
|
|
'''
|
|
|
|
directories = [pathclass.Path(d) for d in directories]
|
|
|
|
directories = sorted(directories)
|
|
|
|
directories = [d.correct_case() for d in directories]
|
|
|
|
directories = [d.absolute_path for d in directories]
|
|
|
|
|
|
|
|
directories_file = pathclass.Path(__file__).parent.with_child('gitcheckup.txt')
|
|
|
|
|
2020-09-21 01:27:28 +00:00
|
|
|
handle = directories_file.open('w', encoding='utf-8')
|
2020-09-02 16:32:16 +00:00
|
|
|
|
|
|
|
with handle:
|
|
|
|
handle.write('\n'.join(directories))
|
|
|
|
|
2020-02-19 21:31:57 +00:00
|
|
|
# GIT FUNCTIONS
|
|
|
|
################################################################################
|
|
|
|
def git_commits_between(a, b):
|
|
|
|
command = [GIT, 'log', '--oneline', f'{a}..{b}']
|
|
|
|
output = check_output(command)
|
2020-02-19 22:20:54 +00:00
|
|
|
lines = output.splitlines()
|
2020-02-19 21:31:57 +00:00
|
|
|
return lines
|
|
|
|
|
2020-02-19 22:20:54 +00:00
|
|
|
def git_current_branch():
|
|
|
|
command = [GIT, 'rev-parse', '--abbrev-ref', 'HEAD']
|
|
|
|
return check_output(command)
|
|
|
|
|
2020-02-19 21:31:57 +00:00
|
|
|
def git_fetch():
|
|
|
|
command = [GIT, 'fetch', '--all']
|
2020-02-19 22:20:54 +00:00
|
|
|
return check_output(command)
|
|
|
|
|
|
|
|
def git_merge_base():
|
|
|
|
command = [GIT, 'merge-base', '@', '@{u}']
|
|
|
|
return check_output(command)
|
|
|
|
|
2020-08-12 19:21:00 +00:00
|
|
|
def git_pull():
|
|
|
|
command = [GIT, 'pull', '--all']
|
|
|
|
return check_output(command)
|
|
|
|
|
2020-09-24 21:30:47 +00:00
|
|
|
def git_push():
|
|
|
|
command = [GIT, 'push']
|
|
|
|
return check_output(command)
|
|
|
|
|
2020-02-19 22:20:54 +00:00
|
|
|
def git_rev_parse(rev):
|
2020-02-19 23:08:02 +00:00
|
|
|
command = [GIT, 'rev-parse', rev]
|
2020-02-19 22:20:54 +00:00
|
|
|
return check_output(command)
|
|
|
|
|
|
|
|
def git_status():
|
|
|
|
command = [GIT, 'status', '--short', '--untracked-files=all']
|
|
|
|
return check_output(command)
|
2020-02-19 21:31:57 +00:00
|
|
|
|
|
|
|
# CHECKUP
|
|
|
|
################################################################################
|
2020-01-28 08:57:24 +00:00
|
|
|
def checkup_committed():
|
2020-02-19 20:49:30 +00:00
|
|
|
details = dotdict.DotDict(default=None)
|
|
|
|
|
|
|
|
details.added = 0
|
|
|
|
details.modified = 0
|
|
|
|
details.deleted = 0
|
2020-02-19 22:20:54 +00:00
|
|
|
|
|
|
|
for line in git_status().splitlines():
|
|
|
|
status = line.split()[0].strip()
|
2020-01-11 23:04:54 +00:00
|
|
|
|
|
|
|
# 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):
|
2020-02-19 20:49:30 +00:00
|
|
|
details.added += 1
|
2020-01-11 23:04:54 +00:00
|
|
|
if {'M', 'R', '!'}.intersection(status):
|
2020-02-19 20:49:30 +00:00
|
|
|
details.modified += 1
|
2020-01-11 23:04:54 +00:00
|
|
|
if {'D'}.intersection(status):
|
2020-02-19 20:49:30 +00:00
|
|
|
details.deleted += 1
|
2020-01-11 23:04:54 +00:00
|
|
|
|
2020-02-19 20:49:30 +00:00
|
|
|
details.committed = (details.added, details.modified, details.deleted) == (0, 0, 0)
|
2020-01-11 10:23:07 +00:00
|
|
|
|
2020-02-19 20:49:30 +00:00
|
|
|
return details
|
2020-01-11 09:44:20 +00:00
|
|
|
|
2020-01-28 08:57:24 +00:00
|
|
|
def checkup_pushed():
|
2020-02-19 20:49:30 +00:00
|
|
|
details = dotdict.DotDict(default=None)
|
|
|
|
|
2020-02-19 22:20:54 +00:00
|
|
|
my_head = git_rev_parse('@')
|
2020-02-19 20:01:13 +00:00
|
|
|
|
|
|
|
try:
|
2020-02-19 22:20:54 +00:00
|
|
|
remote_head = git_rev_parse('@{u}')
|
2020-02-19 20:01:13 +00:00
|
|
|
except subprocess.CalledProcessError as exc:
|
2020-02-19 22:20:54 +00:00
|
|
|
current_branch = git_current_branch()
|
2020-02-19 20:49:30 +00:00
|
|
|
details.error = NoUpstreamBranch(current_branch)
|
|
|
|
return details
|
2020-01-21 05:53:49 +00:00
|
|
|
|
2020-01-28 08:57:24 +00:00
|
|
|
if my_head == remote_head:
|
2020-02-19 20:49:30 +00:00
|
|
|
details.to_push = 0
|
|
|
|
details.to_pull = 0
|
2020-01-28 08:57:24 +00:00
|
|
|
else:
|
2021-01-15 22:19:09 +00:00
|
|
|
try:
|
|
|
|
merge_base = git_merge_base()
|
|
|
|
except subprocess.CalledProcessError as exc:
|
|
|
|
# This happens when the repository has been completely rewritten
|
|
|
|
# with a new root.
|
|
|
|
details.error = 'Root commit has changed'
|
|
|
|
return details
|
2020-02-05 03:01:31 +00:00
|
|
|
|
|
|
|
if my_head == merge_base:
|
2020-02-19 20:49:30 +00:00
|
|
|
details.to_push = 0
|
|
|
|
details.to_pull = len(git_commits_between(merge_base, remote_head))
|
2020-02-05 03:01:31 +00:00
|
|
|
elif remote_head == merge_base:
|
2020-02-19 20:49:30 +00:00
|
|
|
details.to_push = len(git_commits_between(merge_base, my_head))
|
|
|
|
details.to_pull = 0
|
2020-02-05 03:01:31 +00:00
|
|
|
else:
|
2020-02-19 20:49:30 +00:00
|
|
|
details.to_push = len(git_commits_between(merge_base, my_head))
|
|
|
|
details.to_pull = len(git_commits_between(merge_base, remote_head))
|
2020-01-28 08:57:24 +00:00
|
|
|
|
2021-01-15 22:19:09 +00:00
|
|
|
all_pushed = (details.to_push, details.to_pull) == (0, 0)
|
|
|
|
details.pushed = all_pushed
|
2020-02-19 20:49:30 +00:00
|
|
|
return details
|
2020-01-11 10:23:07 +00:00
|
|
|
|
2020-09-24 21:30:47 +00:00
|
|
|
def gitcheckup(directory, do_fetch=False, do_pull=False, do_push=False):
|
2020-09-02 16:32:16 +00:00
|
|
|
os.chdir(directory.absolute_path)
|
2020-02-19 21:31:57 +00:00
|
|
|
|
2020-01-28 08:57:24 +00:00
|
|
|
if do_fetch:
|
|
|
|
git_fetch()
|
2020-01-11 09:44:20 +00:00
|
|
|
|
2020-08-12 19:21:00 +00:00
|
|
|
if do_pull:
|
|
|
|
git_pull()
|
|
|
|
|
2020-09-24 21:30:47 +00:00
|
|
|
if do_push:
|
|
|
|
git_push()
|
|
|
|
|
2020-10-08 17:14:34 +00:00
|
|
|
commit_details = checkup_committed()
|
|
|
|
push_details = checkup_pushed()
|
2020-01-28 09:14:35 +00:00
|
|
|
|
2020-02-19 21:31:57 +00:00
|
|
|
committed = 'C' if commit_details.committed else ' '
|
|
|
|
pushed = 'P' if push_details.pushed else ' '
|
2020-02-19 20:49:30 +00:00
|
|
|
|
|
|
|
details = []
|
2020-01-30 00:39:31 +00:00
|
|
|
|
2020-02-19 21:31:57 +00:00
|
|
|
commit_summary = []
|
|
|
|
if commit_details.added: commit_summary.append(f'+{commit_details.added}')
|
|
|
|
if commit_details.deleted: commit_summary.append(f'-{commit_details.deleted}')
|
|
|
|
if commit_details.modified: commit_summary.append(f'~{commit_details.modified}')
|
|
|
|
commit_summary = ', '.join(commit_summary)
|
|
|
|
if commit_summary: details.append(f'({commit_summary})')
|
|
|
|
|
|
|
|
push_summary = []
|
|
|
|
if push_details.to_push: push_summary.append(f'↑{push_details.to_push}')
|
|
|
|
if push_details.to_pull: push_summary.append(f'↓{push_details.to_pull}')
|
|
|
|
if push_details.error: push_summary.append(f'!{push_details.error}')
|
|
|
|
push_summary = ', '.join(push_summary)
|
|
|
|
if push_summary: details.append(f'({push_summary})')
|
2020-01-28 09:16:33 +00:00
|
|
|
|
2020-02-19 20:49:30 +00:00
|
|
|
details = ' '.join(details)
|
|
|
|
details = (' ' + details).rstrip()
|
2020-09-02 16:32:16 +00:00
|
|
|
print(f'[{committed}][{pushed}] {directory.absolute_path}{details}')
|
2020-01-28 09:16:33 +00:00
|
|
|
|
2020-02-19 22:06:11 +00:00
|
|
|
# COMMAND LINE
|
|
|
|
################################################################################
|
|
|
|
def gitcheckup_argparse(args):
|
2020-09-02 16:32:16 +00:00
|
|
|
if args.add_directory is not None:
|
|
|
|
add_directory(args.add_directory)
|
|
|
|
|
|
|
|
if args.remove_directory is not None:
|
|
|
|
remove_directory(args.remove_directory)
|
|
|
|
|
2020-02-19 22:06:11 +00:00
|
|
|
if args.directories:
|
2020-09-02 16:32:16 +00:00
|
|
|
directories = [pathclass.Path(d) for d in args.directories]
|
2020-02-19 22:06:11 +00:00
|
|
|
else:
|
2020-02-19 20:49:30 +00:00
|
|
|
directories = read_directories_file()
|
2020-01-28 09:16:33 +00:00
|
|
|
|
2020-10-08 17:14:34 +00:00
|
|
|
try:
|
|
|
|
for directory in directories:
|
|
|
|
gitcheckup(directory, do_fetch=args.do_fetch, do_pull=args.do_pull, do_push=args.do_push)
|
|
|
|
except subprocess.CalledProcessError as exc:
|
|
|
|
sys.stdout.write(f'{exc.cmd} exited with status {exc.returncode}\n')
|
|
|
|
sys.stdout.write(exc.output.decode())
|
|
|
|
return 1
|
2020-01-28 08:57:24 +00:00
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
|
|
|
2020-02-19 22:06:11 +00:00
|
|
|
parser.add_argument('directories', nargs='*')
|
2020-01-28 08:57:24 +00:00
|
|
|
parser.add_argument('--fetch', dest='do_fetch', action='store_true')
|
2020-08-12 19:21:00 +00:00
|
|
|
parser.add_argument('--pull', dest='do_pull', action='store_true')
|
2020-09-24 21:30:47 +00:00
|
|
|
parser.add_argument('--push', dest='do_push', action='store_true')
|
2020-09-02 16:32:16 +00:00
|
|
|
parser.add_argument('--add', dest='add_directory')
|
|
|
|
parser.add_argument('--remove', dest='remove_directory')
|
2020-01-28 08:57:24 +00:00
|
|
|
parser.set_defaults(func=gitcheckup_argparse)
|
|
|
|
|
2020-02-19 22:06:11 +00:00
|
|
|
try:
|
2020-08-20 18:10:55 +00:00
|
|
|
return betterhelp.single_main(argv, parser, docstring=__doc__)
|
2020-02-19 22:06:11 +00:00
|
|
|
except GitCheckupException as exc:
|
|
|
|
print(exc)
|
|
|
|
return 1
|
2020-01-28 08:57:24 +00:00
|
|
|
|
2020-01-11 09:44:20 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
raise SystemExit(main(sys.argv[1:]))
|