git: add lazy checkin scripts

This commit is contained in:
2020-07-05 13:36:18 -07:00
parent f01d02ceaa
commit 909c0c336f
2 changed files with 71 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
#!/usr/bin/env python3
import argparse
import subprocess
import pathlib
def call_proc(cmd):
return subprocess.run(cmd, check=True, capture_output=True)
def lazy_commit(all: bool = True, message: str = None):
git_cmd_base = ["git"]
try:
call_proc(git_cmd_base + ["rev-parse", "--abbrev-ref", "HEAD@{upstream}"])
except subprocess.CalledProcessError:
print("Current branch has no upstream")
# exit(1)
call_proc(git_cmd_base + ["add", "-A" if all else "-u"])
if message is None:
try:
message = call_proc(git_cmd_base + ["lazy-description"]).stdout
except subprocess.CalledProcessError:
print("Failed to describe changes, are there any staged changes?")
exit(1)
call_proc(["git", "commit", "-m", message])
# call_proc(["git", "push"])
def main():
parser = argparse.ArgumentParser(
description="Lazily add, commit, and push to upstream."
)
parser.add_argument(
"-a", "--all", help="Add all files, including untracked", action="store_true"
)
parser.add_argument(
"-m",
"--message",
help='Commit message. Defaults to a "lazily-generated" one',
action="store",
default=None,
type=str,
)
args = parser.parse_args()
lazy_commit(all=args.all, message=args.message)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,19 @@
#!/bin/bash
diffstat="$(git diff --staged --numstat | awk '{print ($1 + $2), $3}' | sort -k1nr)"
count=$(echo "$diffstat" | wc -l)
if [[ -z "$diffstat" ]]; then
echo "No staged changes to describe" >&2
exit 1
fi
file=$(echo "$diffstat" | head -n1 | cut -f2 -d' ')
if [[ $count -eq 1 ]]; then
echo "Update $file"
elif [[ $count -gt 1 ]]; then
echo "Update \`$file\` and $((count - 1)) other files"
fi
exit 0