#!/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()