#!/usr/bin/env python3 from logging import NOTSET, basicConfig, debug from subprocess import check_output from sys import argv from re import sub, MULTILINE def run(args: list[str], env: dict): return check_output(args, env=env) if __name__ == "__main__": basicConfig(level=NOTSET) debug(f"Called with parameters {argv}") if argv[1] == "commit": _, _, form_commit, form_following, form_timestamp = tuple(argv) if form_following == "following": commits = ( commit for commit in check_output(["git", "rev-list", form_commit + "^..HEAD"]) .decode("utf-8") .split("\n") if len(commit) > 1 ) else: commits = [form_commit] for commit in commits: previous_data = [ run(["git", "log", "-1", f'--pretty="%{attribute}"', commit], {}) for attribute in ["an", "ae", "cn", "ce"] ] author_name, author_email, committer_name, committer_email = tuple( previous_data ) env = { "GIT_SEQUENCE_EDITOR": f"/usr/bin/env python3 {__file__}", "GIT_COMMITTER_DATE": form_timestamp, "GIT_AUTHOR_NAME": author_name, "GIT_AUTHOR_EMAIL": author_email, "GIT_COMMITTER_NAME": committer_name, "GIT_COMMITTER_EMAIL": committer_email, } run( [ "git", "rebase", "--interactive", "--autostash", "--keep-empty", "--no-autosquash", "--rebase-merges", f"{commit}~", ], env, ) run( [ "git", "commit", "--allow-empty", "--only", "--no-edit", "--amend", f'--date="{form_timestamp}"', ], env, ) run( [ "git", "rebase", "--continue", ], env, ) else: path = argv[-1] file = open(path, "r") content = sub("^pick ", "edit ", file.read(), 1, flags=MULTILINE) file.close() file = open(path, "w") file.write(content) file.close()