You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
@@ -60,18 +71,21 @@ Analyze the following commit messages and produce **accurate, concise, and meani
60
71
# Output:
61
72
Provide a concise description of the style without quoting commit content.
62
73
"
63
-
echo"Running ollama... "
74
+
echo"Running ollama to set temperature to $TEMPERATURE (You can set aicommit.temperature git config to change it)"
75
+
echo"/set parameter temperature $TEMPERATURE"
76
+
echo"Running ollama for style analysis... (You can set aicommit.show-prompt git config to see full prompt)"
64
77
style_description=$(echo "$style_prompt"| ollama run "$MODEL")
65
78
66
79
# Build the commit message prompt.
67
80
prompt="
68
81
# Context:
69
82
Style: $style_description
70
83
71
-
# Instructions:
84
+
# Instructions: Follow these carefully to generate a high-quality commit message. MUST be concise, style-consistent, and under length limits.
85
+
86
+
- Generate a commit message that consists of a Subject Line and a Body, separated by a blank line.
72
87
- 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.
88
+
- **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.
75
89
- Output ONLY the commit message (subject, a blank line, then body).
76
90
- Separate the subject from the body with a blank line.
77
91
- Remove triple backticks; replace backticks with single quotes.
@@ -82,12 +96,15 @@ Style: $style_description
82
96
- No concluding remarks.
83
97
- Do NOT use conventional commit prefixes (like 'feat:', 'fix:', 'docs:')
84
98
- Avoid the redundant message like 'Updated commit messages'
99
+
- Directly output your commit message, without additional explanations.
100
+
- Avoid using ### or other markdown formatting.
85
101
86
102
# Diff:
87
103
<diff>$staged_diff</diff>
88
104
89
105
Commit message:"
90
-
if [ "$2"="--show-prompt" ];then
106
+
# Show prompts if aicommit.show-prompt is set to true
107
+
if [ "$SHOW_AI_COMMIT_PROMPT"="true" ];then
91
108
echo"Full style prompt:"
92
109
echo"$style_prompt"
93
110
echo"Extracted style:"
@@ -97,15 +114,102 @@ Commit message:"
97
114
fi
98
115
99
116
# Generate commit message using ollama.
117
+
echo"Running ollama for commit message... "
100
118
SUGGESTED_COMMITMSG=$(echo "$prompt"| ollama run "$MODEL")
101
119
102
120
# Post-process the commit message.
103
121
# - Trim whitespace.
104
122
# - Remove triple backticks.
105
123
# - Replace backticks with single quotes.
106
-
# - Wrap lines at 72 characters.
107
124
SUGGESTED_COMMITMSG=$(echo "$SUGGESTED_COMMITMSG"| sed 's/^[[:space:]]*//; s/[[:space:]]*$//')
108
125
SUGGESTED_COMMITMSG=$(echo "$SUGGESTED_COMMITMSG"| sed -E '/^(Author:|Date:|Commit message:)/d')
126
+
SUGGESTED_COMMITMSG=$(echo "$SUGGESTED_COMMITMSG"| sed -E '/^```(markdown|diff|text|plaintext)?$/d; s/\*\*([^*]+)\*\*/\1/g; s/`([^`]+)`/'\''\1'\''/g')
127
+
128
+
# Extract the subject line (first line) and body
129
+
subject_line=$(echo "$SUGGESTED_COMMITMSG"| head -n 1)
130
+
body=$(echo "$SUGGESTED_COMMITMSG"| tail -n +3)# Skip the first line and the blank line
131
+
132
+
# Check if the subject line is too long
133
+
if [ ${#subject_line}-gt 50 ];then
134
+
echo"Subject line too long (${#subject_line} chars), will attempt to regenerate up to 10 times..."
135
+
136
+
# Try up to 10 times to generate a subject line under 50 characters
137
+
max_attempts=10
138
+
attempt=1
139
+
original_subject="$subject_line"
140
+
141
+
while [ $attempt-le$max_attempts ] && [ ${#subject_line}-gt 50 ];do
142
+
echo"Attempt $attempt of $max_attempts to generate a shorter subject line..."
143
+
144
+
# Generate a new subject line based on the body and previous attempts
145
+
subject_prompt="
146
+
# Context
147
+
Original subject (${#original_subject} chars):
148
+
$original_subject
149
+
150
+
Previous attempt (${#subject_line} chars):
151
+
$subject_line
152
+
153
+
Body:
154
+
$body
155
+
156
+
# Instructions:
157
+
- The previous commit message's subject line was too long, exceeding 50 characters. Your task is to shorten it to 50 characters or less.
158
+
- Based on this commit message, create ONLY a subject line for the commit message
159
+
- The subject line MUST be under 50 characters (this is attempt $attempt of $max_attempts)
160
+
- Use imperative mood (e.g., 'Add' not 'Added')
161
+
- Capitalize the first word
162
+
- Do not end with a period
163
+
- Be specific but concise
164
+
- Use clear action verbs
165
+
- Avoid vague terms like 'Update' or 'Fix' without context
166
+
- Output ONLY the subject line, nothing else
167
+
- Do NOT use conventional commit prefixes (like 'feat:', 'fix:', 'docs:')
168
+
- Use plain text without markdown or HTML
169
+
170
+
# Output:"
171
+
172
+
echo"Running ollama for new subject line (attempt $attempt)... "
173
+
new_subject_line=$(echo "$subject_prompt"| ollama run "$MODEL")
174
+
175
+
# Clean up the new subject line
176
+
new_subject_line=$(echo "$new_subject_line"| sed 's/^[[:space:]]*//; s/[[:space:]]*$//')
177
+
new_subject_line=$(echo "$new_subject_line"| sed -E '/^(Author:|Date:|Subject line:)/d')
178
+
new_subject_line=$(echo "$new_subject_line"| sed -E '/^```(markdown|diff|text|plaintext)?$/d; s/\*\*([^*]+)\*\*/\1/g; s/`([^`]+)`/'\''\1'\''/g')
179
+
180
+
# Update the subject line
181
+
subject_line="$new_subject_line"
182
+
183
+
# Check if the new subject line is under 50 characters
184
+
if [ ${#subject_line}-le 50 ];then
185
+
echo"Success! Generated a subject line under 50 characters (${#subject_line} chars)."
186
+
break
187
+
else
188
+
echo"Attempt $attempt failed: Subject line still too long (${#subject_line} chars)."
189
+
fi
190
+
191
+
attempt=$((attempt +1))
192
+
done
193
+
194
+
# If we've tried 3 times and still have a long subject line, inform the user
195
+
if [ ${#subject_line}-gt 50 ];then
196
+
echo"Warning: After $max_attempts attempts, the subject line is still too long (${#subject_line} chars)."
197
+
echo"You may want to edit it manually to comply with the 50-character limit."
198
+
fi
199
+
fi
200
+
201
+
# Combine the (possibly new) subject line with the original body
202
+
SUGGESTED_COMMITMSG="$subject_line
203
+
204
+
$body"
205
+
206
+
# Show final subject line if aicommit.show-prompt is set to true
207
+
if [ "$SHOW_AI_COMMIT_PROMPT"="true" ];then
208
+
echo"Final subject line (${#subject_line} chars):"
209
+
echo"$subject_line"
210
+
fi
211
+
212
+
# Wrap lines at 72 characters
109
213
SUGGESTED_COMMITMSG="$(
110
214
echo"$SUGGESTED_COMMITMSG" \
111
215
| sed -E '/^```(markdown|diff|text|plaintext)?$/d; s/\*\*([^*]+)\*\*/\1/g; s/`([^`]+)`/'\''\1'\''/g' \
0 commit comments