Skip to content

Commit 200f5f2

Browse files
committed
Refine aicommit with iterative subject Shortening
Two-Stage Commit Message Generation: First, generate a detailed commit description. Then, refine the subject line for conciseness and clarity. If the initial subject exceeds 50 characters, the script regenerates it with explicit length constraints. Up to 10 iterations ensure compliance with Git conventions. If still too long, a warning prompts users for manual adjustments. This commit also improved LLM instructions for better adherence to commit message best practices. Clear separation of Subject Line and Body with enforced constraints. Set SHOW_AI_COMMIT_PROMPT to see full prompt. Set TEMPERATURE to change model temperature. Change-Id: I6b9c1337210b86a400e84c3984c17dcdc72fb26e
1 parent b8a6fa9 commit 200f5f2

File tree

2 files changed

+102
-6
lines changed

2 files changed

+102
-6
lines changed

scripts/aspell-pws

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,5 @@ noreturn
352352
pragma
353353
ollama
354354
qwen
355+
aicommit
356+
LLM

scripts/prepare-commit-msg.hook

Lines changed: 100 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ EOF
3838
# commit style by learning from previous commits.
3939
# Inspired by https://github.com/acrosa/aicommits.
4040
MODEL="qwen2.5-coder"
41+
TEMPERATURE=${TEMPERATURE:-0.3}
4142
SUGGESTED_COMMITMSG=
4243
AICOMMIT=$(git config --get core.aicommit || echo 'auto')
4344
if [[ "$AICOMMIT" == "always" ]] || [[ "$AICOMMIT" == "auto" && -t 1 ]] && \
@@ -60,18 +61,21 @@ Analyze the following commit messages and produce **accurate, concise, and meani
6061
# Output:
6162
Provide a concise description of the style without quoting commit content.
6263
"
63-
echo "Running ollama... "
64+
echo "Running ollama to set temperature to $TEMPERATURE (You can set TEMPERATURE environment variable to change it)"
65+
echo "/set parameter temperature $TEMPERATURE"
66+
echo "Running ollama for style analysis... (You can set SHOW_AI_COMMIT_PROMPT to see full prompt)"
6467
style_description=$(echo "$style_prompt" | ollama run "$MODEL")
6568

6669
# Build the commit message prompt.
6770
prompt="
6871
# Context:
6972
Style: $style_description
7073
71-
# Instructions:
74+
# Instructions: Follow these carefully to generate a high-quality commit message. MUST be concise, style-consistent, and under length limits.
75+
76+
- Generate a commit message that consists of a Subject Line and a Body, separated by a blank line.
7277
- Analyze the diff below and generate a commit message based solely on its content.
73-
- Mimic the style described above (tone, length, structure) without copying previous messages.
74-
- Use clear action verbs and be concise.
78+
- **Crucially, mimic the style** described above (tone, length, structure) without copying previous messages. **Pay close attention to maintaining consistency with the established style.**- Use clear action verbs and be concise.
7579
- Output ONLY the commit message (subject, a blank line, then body).
7680
- Separate the subject from the body with a blank line.
7781
- Remove triple backticks; replace backticks with single quotes.
@@ -82,12 +86,15 @@ Style: $style_description
8286
- No concluding remarks.
8387
- Do NOT use conventional commit prefixes (like 'feat:', 'fix:', 'docs:')
8488
- Avoid the redundant message like 'Updated commit messages'
89+
- Directly output your commit message, without additional explanations.
90+
- Avoid using ### or other markdown formatting.
8591
8692
# Diff:
8793
<diff>$staged_diff</diff>
8894
8995
Commit message:"
90-
if [ "$2" = "--show-prompt" ]; then
96+
# Show prompts if SHOW_AI_COMMIT_PROMPT environment variable is set
97+
if [ -n "$SHOW_AI_COMMIT_PROMPT" ]; then
9198
echo "Full style prompt:"
9299
echo "$style_prompt"
93100
echo "Extracted style:"
@@ -97,15 +104,102 @@ Commit message:"
97104
fi
98105

99106
# Generate commit message using ollama.
107+
echo "Running ollama for commit message... "
100108
SUGGESTED_COMMITMSG=$(echo "$prompt" | ollama run "$MODEL")
101109

102110
# Post-process the commit message.
103111
# - Trim whitespace.
104112
# - Remove triple backticks.
105113
# - Replace backticks with single quotes.
106-
# - Wrap lines at 72 characters.
107114
SUGGESTED_COMMITMSG=$(echo "$SUGGESTED_COMMITMSG" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')
108115
SUGGESTED_COMMITMSG=$(echo "$SUGGESTED_COMMITMSG" | sed -E '/^(Author:|Date:|Commit message:)/d')
116+
SUGGESTED_COMMITMSG=$(echo "$SUGGESTED_COMMITMSG" | sed -E '/^```(markdown|diff|text|plaintext)?$/d; s/\*\*([^*]+)\*\*/\1/g; s/`([^`]+)`/'\''\1'\''/g')
117+
118+
# Extract the subject line (first line) and body
119+
subject_line=$(echo "$SUGGESTED_COMMITMSG" | head -n 1)
120+
body=$(echo "$SUGGESTED_COMMITMSG" | tail -n +3) # Skip the first line and the blank line
121+
122+
# Check if the subject line is too long
123+
if [ ${#subject_line} -gt 50 ]; then
124+
echo "Subject line too long (${#subject_line} chars), will attempt to regenerate up to 10 times..."
125+
126+
# Try up to 10 times to generate a subject line under 50 characters
127+
max_attempts=10
128+
attempt=1
129+
original_subject="$subject_line"
130+
131+
while [ $attempt -le $max_attempts ] && [ ${#subject_line} -gt 50 ]; do
132+
echo "Attempt $attempt of $max_attempts to generate a shorter subject line..."
133+
134+
# Generate a new subject line based on the body and previous attempts
135+
subject_prompt="
136+
# Context
137+
Original subject (${#original_subject} chars):
138+
$original_subject
139+
140+
Previous attempt (${#subject_line} chars):
141+
$subject_line
142+
143+
Body:
144+
$body
145+
146+
# Instructions:
147+
- The previous commit message's subject line was too long, exceeding 50 characters. Your task is to shorten it to 50 characters or less.
148+
- Based on this commit message, create ONLY a subject line for the commit message
149+
- The subject line MUST be under 50 characters (this is attempt $attempt of $max_attempts)
150+
- Use imperative mood (e.g., 'Add' not 'Added')
151+
- Capitalize the first word
152+
- Do not end with a period
153+
- Be specific but concise
154+
- Use clear action verbs
155+
- Avoid vague terms like 'Update' or 'Fix' without context
156+
- Output ONLY the subject line, nothing else
157+
- Do NOT use conventional commit prefixes (like 'feat:', 'fix:', 'docs:')
158+
- Use plain text without markdown or HTML
159+
160+
# Output:"
161+
162+
echo "Running ollama for new subject line (attempt $attempt)... "
163+
new_subject_line=$(echo "$subject_prompt" | ollama run "$MODEL")
164+
165+
# Clean up the new subject line
166+
new_subject_line=$(echo "$new_subject_line" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')
167+
new_subject_line=$(echo "$new_subject_line" | sed -E '/^(Author:|Date:|Subject line:)/d')
168+
new_subject_line=$(echo "$new_subject_line" | sed -E '/^```(markdown|diff|text|plaintext)?$/d; s/\*\*([^*]+)\*\*/\1/g; s/`([^`]+)`/'\''\1'\''/g')
169+
170+
# Update the subject line
171+
subject_line="$new_subject_line"
172+
173+
# Check if the new subject line is under 50 characters
174+
if [ ${#subject_line} -le 50 ]; then
175+
echo "Success! Generated a subject line under 50 characters (${#subject_line} chars)."
176+
break
177+
else
178+
echo "Attempt $attempt failed: Subject line still too long (${#subject_line} chars)."
179+
fi
180+
181+
attempt=$((attempt + 1))
182+
done
183+
184+
# If we've tried 3 times and still have a long subject line, inform the user
185+
if [ ${#subject_line} -gt 50 ]; then
186+
echo "Warning: After $max_attempts attempts, the subject line is still too long (${#subject_line} chars)."
187+
echo "You may want to edit it manually to comply with the 50-character limit."
188+
fi
189+
fi
190+
191+
# Combine the (possibly new) subject line with the original body
192+
SUGGESTED_COMMITMSG="$subject_line
193+
194+
$body"
195+
196+
# Show final subject line if SHOW_AI_COMMIT_PROMPT environment variable is set
197+
if [ -n "$SHOW_AI_COMMIT_PROMPT" ]; then
198+
echo "Final subject line (${#subject_line} chars):"
199+
echo "$subject_line"
200+
fi
201+
202+
# Wrap lines at 72 characters
109203
SUGGESTED_COMMITMSG="$(
110204
echo "$SUGGESTED_COMMITMSG" \
111205
| sed -E '/^```(markdown|diff|text|plaintext)?$/d; s/\*\*([^*]+)\*\*/\1/g; s/`([^`]+)`/'\''\1'\''/g' \

0 commit comments

Comments
 (0)