Skip to content

Instantly share code, notes, and snippets.

@haje01
Last active January 3, 2025 02:40
Show Gist options
  • Save haje01/ba3d03b57a7f673e15db7b339dba9197 to your computer and use it in GitHub Desktop.
Save haje01/ba3d03b57a7f673e15db7b339dba9197 to your computer and use it in GitHub Desktop.
로컬 LLM 을 이용한 한국어 Git 커밋 메시지
# -----------------------------------------------------------------------------
# AI-powered Git Commit Function
# 원본 : https://gist.github.com/karpathy/1dd0294ef9567971c1e4348a90d69285
# Ollama (https://ollama.com/) 설치 후, 다음처럼 Aya-Expanse 모델 설치 후 이용
# `ollama run aya-expanse:latest hi`
#
gcm() {
# Function to generate commit message
generate_commit_message() {
local git_diff=$1
echo "$git_diff" | ollama run aya-expanse "
Below is a diff of all staged changes, coming from the command:
\`\`\`
git diff
\`\`\`
Please generate a concise, one-line english commit message for these changes. If no diff exists, you can simply output 'No changes. Please cancel'." | ollama run aya-expanse "아래 내용을 가급적 한 줄의 한국어 텍스트로 요약해. 과거형 동사를 이용하고 장황한 설명을 하지마. 코드의 변수나 클래스 이름 등은 그대로 유지해. 'No changes. Please cancel' 식의 메시지는 그대로 출력"
}
# Function to read user input compatibly with both Bash and Zsh
read_input() {
if [ -n "$ZSH_VERSION" ]; then
echo -n "$1"
read -r REPLY
else
read -p "$1" -r REPLY
fi
}
git_diff=$(git diff)
if [ -z "$git_diff" ]; then
echo "No changes to commit."
return 1
fi
# Main script
echo "Generating AI-powered commit message..."
commit_message=$(generate_commit_message "$git_diff")
while true; do
echo -e "\nProposed commit message:"
echo "\"$commit_message\""
read_input "Do you want to (a)ccept, (e)dit, (r)egenerate, or (c)ancel? "
choice=$REPLY
case "$choice" in
a|A )
if git commit -m "$commit_message" -a; then
echo "Changes committed successfully!"
return 0
else
echo "Commit failed. Please check your changes and try again."
return 1
fi
;;
e|E )
read_input "Enter your commit message: "
commit_message=$REPLY
if [ -n "$commit_message" ] && git commit -m "$commit_message" -a; then
echo "Changes committed successfully with your message!"
return 0
else
echo "Commit failed. Please check your message and try again."
return 1
fi
;;
r|R )
echo "Regenerating commit message..."
commit_message=$(generate_commit_message "$git_diff")
;;
c|C )
echo "Commit cancelled."
return 1
;;
* )
echo "Invalid choice. Please try again."
;;
esac
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment