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 +61,21 @@ Analyze the following commit messages and produce **accurate, concise, and meani
60
61
# Output:
61
62
Provide a concise description of the style without quoting commit content.
62
63
"
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)"
64
67
style_description=$(echo "$style_prompt"| ollama run "$MODEL")
65
68
66
69
# Build the commit message prompt.
67
70
prompt="
68
71
# Context:
69
72
Style: $style_description
70
73
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.
72
77
- 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.
75
79
- Output ONLY the commit message (subject, a blank line, then body).
76
80
- Separate the subject from the body with a blank line.
77
81
- Remove triple backticks; replace backticks with single quotes.
@@ -82,12 +86,15 @@ Style: $style_description
82
86
- No concluding remarks.
83
87
- Do NOT use conventional commit prefixes (like 'feat:', 'fix:', 'docs:')
84
88
- Avoid the redundant message like 'Updated commit messages'
89
+
- Directly output your commit message, without additional explanations.
90
+
- Avoid using ### or other markdown formatting.
85
91
86
92
# Diff:
87
93
<diff>$staged_diff</diff>
88
94
89
95
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
91
98
echo"Full style prompt:"
92
99
echo"$style_prompt"
93
100
echo"Extracted style:"
@@ -97,15 +104,102 @@ Commit message:"
97
104
fi
98
105
99
106
# Generate commit message using ollama.
107
+
echo"Running ollama for commit message... "
100
108
SUGGESTED_COMMITMSG=$(echo "$prompt"| ollama run "$MODEL")
101
109
102
110
# Post-process the commit message.
103
111
# - Trim whitespace.
104
112
# - Remove triple backticks.
105
113
# - Replace backticks with single quotes.
106
-
# - Wrap lines at 72 characters.
107
114
SUGGESTED_COMMITMSG=$(echo "$SUGGESTED_COMMITMSG"| sed 's/^[[:space:]]*//; s/[[:space:]]*$//')
108
115
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
109
203
SUGGESTED_COMMITMSG="$(
110
204
echo"$SUGGESTED_COMMITMSG" \
111
205
| sed -E '/^```(markdown|diff|text|plaintext)?$/d; s/\*\*([^*]+)\*\*/\1/g; s/`([^`]+)`/'\''\1'\''/g' \
0 commit comments