Skip to content

Commit 740b2e4

Browse files
committed
Convert commit history to structured messages
Transform git commit tracking from subject-only to full structured messages. This enhancement provides richer context for commit message practices and creates a format better suited for LLM learning and manipulation. Change-Id: I601aa5d46873066c13a324bb3e0a6a16bd6864d4
1 parent 742d822 commit 740b2e4

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

scripts/prepare-commit-msg.hook

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,35 @@ AICOMMIT=$(git config --get core.aicommit || echo 'auto')
5454
if [[ "$AICOMMIT" == "always" ]] || [[ "$AICOMMIT" == "auto" && -t 1 ]] && \
5555
git diff --cached --name-only | grep -qiE "\.(c|h|cpp|hpp)$"; then
5656
# Build commit history list from the last non-merge commit messages.
57-
commit_history=$(git log -n 70 --no-merges --pretty=format:'"%s",' | \
58-
sed -E 's/ \(\#[0-9]+\)//; $ s/,$//')
59-
commit_history="[$commit_history]"
57+
commit_history=$(
58+
{
59+
echo '---'
60+
git log -n 50 --no-merges --pretty=format:'%B%n---'
61+
} | sed -E 's/ \(\#[0-9]+\)//; /^Change-Id:/d' | awk '
62+
function print_block(block) {
63+
sub(/\n+$/, "", block)
64+
n = split(block, a, "\n")
65+
subject = a[1]
66+
body = ""
67+
for (i = 2; i <= n; i++) {
68+
if (a[i] ~ /[^[:space:]]/)
69+
body = body (body ? "\n" : "") a[i]
70+
}
71+
print "<subject>" subject "</subject>"
72+
print "<body>" body "</body>"
73+
print "---"
74+
}
75+
{
76+
if ($0=="---") {
77+
if (block != "") { print_block(block); block = "" }
78+
} else {
79+
block = block $0 "\n"
80+
}
81+
}
82+
END {
83+
if (block != "") print_block(block)
84+
}'
85+
)
6086

6187
# Capture the staged diff.
6288
staged_diff=$(git diff --cached)
@@ -65,8 +91,8 @@ if [[ "$AICOMMIT" == "always" ]] || [[ "$AICOMMIT" == "auto" && -t 1 ]] && \
6591
style_prompt="
6692
You are a specialized system for generating high-quality Git commit messages based on 'git diff --cached' output and optional developer descriptions.
6793
# Task:
68-
Analyze the following commit messages and produce **accurate, concise, and meaningful commit messages** that clearly describe the changes:
69-
- $commit_history
94+
Analyze the following commit messages and produce **accurate, concise, and meaningful commit messages** that clearly describe the changes: (Format: '---' separates messages, <subject>...</subject> and <body>...</body> tags)
95+
$commit_history
7096
7197
# Output:
7298
Provide a concise description of the style without quoting commit content.
@@ -92,7 +118,8 @@ Style: $style_description
92118
- Keep the first line (subject) under 50 characters
93119
- Ensure no line exceeds 72 characters.
94120
- Avoid vague messages like 'Updates' or 'Fixed bug'
95-
- Always write in **plain text** without markdown or HTML.
121+
- Respond in **plain text only**. No markdown, HTML, JSON, code blocks, or special formatting characters.
122+
- Use only standard punctuation and paragraph breaks.
96123
- No concluding remarks.
97124
- Do NOT use conventional commit prefixes (like 'feat:', 'fix:', 'docs:')
98125
- Avoid the redundant message like 'Updated commit messages'
@@ -122,7 +149,7 @@ Commit message:"
122149
# - Remove triple backticks.
123150
# - Replace backticks with single quotes.
124151
SUGGESTED_COMMITMSG=$(echo "$SUGGESTED_COMMITMSG" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')
125-
SUGGESTED_COMMITMSG=$(echo "$SUGGESTED_COMMITMSG" | sed -E '/^(Author:|Date:|Commit message:)/d')
152+
SUGGESTED_COMMITMSG=$(echo "$SUGGESTED_COMMITMSG" | sed -E '/^(Author:|Date:|Commit message:|commit )/d')
126153
SUGGESTED_COMMITMSG=$(echo "$SUGGESTED_COMMITMSG" | sed -E '/^```(markdown|diff|text|plaintext)?$/d; s/\*\*([^*]+)\*\*/\1/g; s/`([^`]+)`/'\''\1'\''/g')
127154

128155
# Extract the subject line (first line) and body

0 commit comments

Comments
 (0)