Skip to content

Instantly share code, notes, and snippets.

@kljohann
Created October 26, 2018 18:58
Show Gist options
  • Save kljohann/8ea4948ff5bfaaf9794929a8321ed83c to your computer and use it in GitHub Desktop.
Save kljohann/8ea4948ff5bfaaf9794929a8321ed83c to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
from pathlib import Path
import shlex
import subprocess
import sys
OPERATIONS = (
("merge", "MERGE_HEAD"),
("rebase", "REBASE_HEAD"),
("am", "rebase-apply/applying"),
("cherry-pick", "CHERRY_PICK_HEAD"),
("revert", "REVERT_HEAD"),
)
def infer_action_from_executable_name():
executable_name = Path(sys.argv[0]).name
if not executable_name.startswith("git-"):
return None
action = executable_name[len("git-") :]
if action not in ["continue", "skip", "abort", "quit"]:
return None
return action
def infer_active_git_operation():
git_dir = Path(
subprocess.check_output(
["git", "rev-parse", "--git-dir"], stderr=subprocess.STDOUT
)
.decode("utf-8")
.strip()
)
for operation, evidence in OPERATIONS:
if (git_dir / evidence).exists():
return operation
return None
def main():
action = infer_action_from_executable_name()
if action is None:
print("Unknown action.", file=sys.stderr)
sys.exit(1)
operation = infer_active_git_operation()
if operation is None:
print("Nothing to {}.".format(action), file=sys.stderr)
sys.exit(0)
cmd = ["git", operation, "--{}".format(action)] + sys.argv[1:]
print(">", " ".join(map(shlex.quote, cmd)))
sys.exit(subprocess.call(cmd))
if __name__ == "__main__":
try:
main()
except subprocess.CalledProcessError as err:
sys.stderr.write(err.output.decode("utf-8"))
sys.exit(err.returncode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment