diff --git a/.mgr_config/bin/git-lazy-checkin b/.mgr_config/bin/git-lazy-checkin new file mode 100755 index 0000000..e81c64e --- /dev/null +++ b/.mgr_config/bin/git-lazy-checkin @@ -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() diff --git a/.mgr_config/bin/git-lazy-description b/.mgr_config/bin/git-lazy-description new file mode 100755 index 0000000..1399d43 --- /dev/null +++ b/.mgr_config/bin/git-lazy-description @@ -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