diff --git a/README.md b/README.md index 2f4b33a..0cc4814 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,42 @@ # ai-commit -A Git Commit Hook with AI Support + +Are you tired to think about a commit message at the end of making changes on your code? +Have you often `Update README.md` or `test` as message in your git log? Let create AI for you a meanful message based on last `git diff`. Here is a Git Commit Hook with AI Support + +Look at the [demo](https://eumel8.github.io/ai-commit/recording.html) how to work on a Terraform manifest for OpenTelekomCloud + +# preparation + +* A running [Ollama](https://ollama.com/) instance running with API access, probably on your computer on `127.0.0.1:11434` +* Loaded LLM to use, e.g. [deepseek-coder-v2-lite-base-q4_k_m-gguf:latest](https://ollama.com/networkjohnny/deepseek-coder-v2-lite-base-q4_k_m-gguf), a DeepSeek-Coder-V2-Lite-Base with only 2GB + + ```bash + # ollama pull networkjohnny/deepseek-coder-v2-lite-base-q4_k_m-gguf:latest + # ollama serve + ``` + +* required tools on your computer: curl, jq, sed +* copy `commit-msg` to `.git/hooks/commit-msg` in your Git repo and make it executable (`chmod +x`) +* make changes and review AI's work. If you are unhappy, just edit the message with `git commit --amend` bevore push + + +# customization + +ai-commit works out of the bos with a local Ollama instance. Nevertheless you can customize such things in the `commit-msg` file: + +* OLLAMA_API: API endpoint of OLLAMA instance +* MODEL: Name of the usesd LLM (available on the OLLAMA host, can be any LLM such llamam, mistral, deepseek-r1) +* PROMPT: exact task explained to the AI +* COMMIT_MSG: Lots of magic to modify the output and make a conform Git commit message + +# super lazy mode + +``` +alias ac='git add . && git commit -m ""' +``` + +# Credits + +Frank Kloeker f.kloeker@telekom.de + +Life is for sharing. If you have an issue with the code or want to improve it, feel free to open an issue or an pull request. diff --git a/ai-commit-64.png b/ai-commit-64.png new file mode 100644 index 0000000..aefc624 Binary files /dev/null and b/ai-commit-64.png differ diff --git a/ai-commit.png b/ai-commit.png new file mode 100644 index 0000000..9bc2770 Binary files /dev/null and b/ai-commit.png differ diff --git a/commit-msg b/commit-msg new file mode 100644 index 0000000..c75f3ac --- /dev/null +++ b/commit-msg @@ -0,0 +1,51 @@ +#!/bin/bash +# write commit message by ai + +# File containing the commit message +COMMIT_MSG_FILE=$1 + +# Get the Git diff +DIFF=$(git diff --cached) + +# If there's no diff (e.g., an empty commit), exit +if [ -z "$DIFF" ]; then + exit 0 +fi + +# Define the Ollama API URL +OLLAMA_API="http://127.0.0.1:11434/api/chat" +MODEL="networkjohnny/deepseek-coder-v2-lite-base-q4_k_m-gguf:latest" + +# Define the prompt for generating commit messages +PROMPT=$(jq -n --arg diff "$DIFF" \ + '{ + "model": "'$MODEL'", + "messages": [ + {"role": "system", "content": "You are an expert software engineer."}, + {"role": "user", "content": ("Based on the following Git diff, generate a concise, meaningful commit message.\n\nKeep it within 50 characters and ensure it clearly describes the change.\n\nGit diff:\n" + $diff + "Hint: Answer only with the commit message and it should take maximal 10 seconds")} + ] + }' +) + +# Make the API request to Ollama +RESPONSE=$(curl -s -X POST "$OLLAMA_API" -H "Content-Type: application/json" -d "$PROMPT") + +# Extract **only** the commit message content from JSON +COMMIT_MSG=$(echo "$RESPONSE" | jq -r '.message.content // ""') + +# **More cleaning**: Remove all unwanted prefixes +COMMIT_MSG=$(echo "$COMMIT_MSG" | sed -E 's/^(`|")//g') + +# Remove newlines, trim extra spaces, and enforce single-line output +COMMIT_MSG=$(echo "$COMMIT_MSG" | tr -d '\n' | sed 's/ */ /g') + +# Truncate to 50 characters (safety limit) +COMMIT_MSG=$(echo "$COMMIT_MSG" | sed -E 's/^(.{1,50})([[:space:]]|$)/\1 /') + +# If the AI response is empty, do nothing +if [ -z "$COMMIT_MSG" ]; then + exit 0 +fi + +# Write the AI-generated commit message to the commit message file +echo "$COMMIT_MSG" > "$COMMIT_MSG_FILE" diff --git a/recording.html b/recording.html new file mode 100644 index 0000000..2b6c1f0 --- /dev/null +++ b/recording.html @@ -0,0 +1,4163 @@ + + + + + + + + + + + + +
+ + + + + +