| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- #!/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":
- is_form_root = not check_output(
- ["git", "log", "-1", "--format=%P", form_commit]
- ).strip()
- rev_list_args = ["git", "rev-list", "HEAD"] if is_form_root else ["git", "rev-list", form_commit + "^..HEAD"]
- commits = (
- commit
- for commit in check_output(rev_list_args)
- .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,
- }
- is_root = not run(
- ["git", "log", "-1", "--format=%P", commit], {}
- ).strip()
- run(
- [
- "git",
- "rebase",
- "--interactive",
- "--autostash",
- "--keep-empty",
- "--no-autosquash",
- "--rebase-merges",
- ]
- + (["--root"] if is_root else [f"{commit}~"]),
- env,
- )
- run(
- [
- "git",
- "commit",
- "--allow-empty",
- "--only",
- "--no-edit",
- "--amend",
- "--no-verify",
- 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()
|