From b6d999983ff39422dbbc8d872ff84d48e4b5d07c Mon Sep 17 00:00:00 2001 From: Daniel Palma Date: Sat, 25 Nov 2023 14:15:24 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=9E:=20Explain=20and=20generate=20git?= =?UTF-8?q?=20commands=20using=20plain=20English.=20Install=20and=20add=20?= =?UTF-8?q?pre-commit=20hook.=20Show=20magic=20with=20example.=20Update=20?= =?UTF-8?q?commit=20messages=20and=20add=20main=20function=20for=20pre-com?= =?UTF-8?q?mit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 31 +++++++++++++++++++++++++++++++ git_genie/main.py | 7 +++---- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ea0a730..acb4efb 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,37 @@ Generate & explain git commands using plain english. Generated command: git commit -m 'Update README with commit message example and instructions' ``` +### Generate commit messages as a pre-commit hook + +#### Install pre-commit + +```shell +pip install pre-commit +``` + +#### Add pre-commit hook + +```yaml +repos: + - repo: https://github.com/danthelion/git-genie + rev: "v0.3.0" + hooks: + - id: git-genie + args: [ '--mode=append' ] # or --mode=replace +``` + +#### Watch the magic happen + +```shell +~/Personal/git-genie-pre-commit main* ❯ git add . && git commit -m "test" + +git-genie................................................................Passed + +[main 0dc8c69] test 🧞: Specify specific revision and add '--mode=replace' argument to git-genie hook. + + 1 file changed, 2 insertions(+), 1 deletion(-) +``` + ### Generate & Explain complex git commands using plain english ![example](example.png) diff --git a/git_genie/main.py b/git_genie/main.py index 90003e2..bd458a4 100644 --- a/git_genie/main.py +++ b/git_genie/main.py @@ -1,6 +1,5 @@ import argparse import subprocess -import sys import typer from langchain.chains import LLMChain @@ -274,7 +273,7 @@ def main( ) -def update_commit_message(filename, mode): +def update_commit_message(filename: str, mode: str = "append"): with open(filename, "r+") as fd: contents = fd.readlines() commit_msg = contents[0].rstrip("\r\n") @@ -304,8 +303,8 @@ def pre_commit(argv=None): parser.add_argument("filenames", nargs="+") parser.add_argument("--mode", nargs="?", const=append, default=append, choices=[append, replace]) args = parser.parse_args(argv) - update_commit_message(args.filenames[0], mode="append") + update_commit_message(args.filenames[0], mode=args.mode) if __name__ == "__main__": - sys.exit(pre_commit()) + app()