Skip to content

Instantly share code, notes, and snippets.

@daveschumaker
Forked from krzyzanowskim/.gitconfig
Last active September 1, 2024 16:32
Show Gist options
  • Save daveschumaker/4ec9ee5bd43cda1976415d57d9d0a239 to your computer and use it in GitHub Desktop.
Save daveschumaker/4ec9ee5bd43cda1976415d57d9d0a239 to your computer and use it in GitHub Desktop.
commit-ai
[alias]
# need llm CLI: https://llm.datasette.io/en/stable/
# originally based on: https://gist.github.com/karpathy/1dd0294ef9567971c1e4348a90d69285?permalink_comment_id=5167582#gistcomment-5167582
# further modified from: https://gist.github.com/krzyzanowskim/a7faf277cd0b88cbff23bd9b52a4bd2a
commit-ai = "!f() { \
prefix=\"$1\"; \
shift; \
if [[ \"$prefix\" == -* ]]; then \
extra_args=\"$prefix $@\"; \
prefix=\"chore\"; \
else \
extra_args=\"\"; \
for arg in \"$@\"; do \
if [[ \"$arg\" == -* ]]; then \
extra_args=\"$extra_args $arg\"; \
fi; \
done; \
fi; \
while true; do \
message=\"${prefix}: $(git diff --cached | llm -m 'llama3.1' 'Below is a diff of all staged changes, coming from the command:\\n```\\ngit diff --cached\\n```\\nPlease generate a concise, one-sentence, maximum 100 characters commit message for these changes. Do not mention project name. Your ONLY response will be to return the commit message. Do not wrap the message in quotation marks.')\"; \
echo \"Generated commit message: $message\"; \
read -p \"Use this commit message? (Y/n/r) [Y]: \" choice; \
choice=${choice:-Y}; \
case \"$choice\" in \
[Yy]*) git commit -m \"$message\" $extra_args; break;; \
[Nn]*) echo 'Commit aborted.'; break;; \
[Rr]*) echo 'Regenerating...';; \
*) echo 'Please enter Y (yes), n (no), or r (regenerate).';; \
esac; \
done; \
}; f"
@daveschumaker
Copy link
Author

Modified from original source (https://gist.github.com/krzyzanowskim/a7faf277cd0b88cbff23bd9b52a4bd2a), in order to add ability to (optionally) pass in a semantic commit (e.g., git commit-ai fix), use a preferred local LLM (llama3.1 via ollama), and the ability to preview or regenerate a commit message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment