-
-
Save codemicro/bde7fc6082197b5fd67c5e9630a4be1b to your computer and use it in GitHub Desktop.
Git Autocommit!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import subprocess | |
from typing import * | |
import os | |
GIT_EXE = "git" | |
def transformFilenameLine(line: str) -> str: | |
parts = list( | |
map( | |
lambda x: "`" + repr(x.strip().strip('"').split(os.path.sep)[-1]).strip("'") + "`", | |
line.split("->"), | |
) | |
) | |
if len(parts) > 1: | |
return " to ".join(parts) | |
return parts[0] | |
def parseStatusMessage(message: str) -> List[Tuple[str, str]]: | |
o: Tuple[str, str] = [] | |
for line in message.splitlines(): | |
if line[0] == " ": | |
# not staged | |
continue | |
status = line[0:2].strip().lower() | |
filename = transformFilenameLine(line[2:]) | |
readableStatus = None | |
# See https://git-scm.com/docs/git-status#_short_format | |
if status == "m" or status == "t": | |
readableStatus = "Update" | |
elif status == "a": | |
readableStatus = "Add" | |
elif status == "d": | |
readableStatus = "Delete" | |
elif status == "r": | |
readableStatus = "Rename" | |
if readableStatus is None: | |
continue | |
o.append([readableStatus, filename]) | |
return o | |
def formOutput(elements: List[Tuple[str, str]]) -> str: | |
if len(elements) == 1: | |
return " ".join(elements[0]) | |
else: | |
return "Alter {} files\n\n".format(len(elements)) + "\n".join( | |
map(lambda x: " ".join(x), elements) | |
) | |
if __name__ == "__main__": | |
git_status = subprocess.check_output([GIT_EXE, "status", "-s", "--porcelain"]) | |
output = formOutput(parseStatusMessage(git_status.decode())) | |
print(output, end="\n\n") | |
subprocess.run([GIT_EXE, "commit", "--file=-"], input=output.encode()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment