Skip to content

A local-first personal finance app

[AI] Fix git-guard false positives on heredoc and multi -m commit messages (#8192)

Matiss Janis Aboltinse6811e8ddf7d
diff --git a/scripts/agent-hooks/git-guard.sh b/scripts/agent-hooks/git-guard.sh
index d089a65d44..cfd5006c4a 100755
--- a/scripts/agent-hooks/git-guard.sh
+++ b/scripts/agent-hooks/git-guard.sh
@@ -86,12 +86,40 @@ esac
# Commit messages must start with [AI].
case "$cmd" in
*"git commit"*)
- # Extract the message from -m / --message (quoted first, then unquoted).
- # An empty result means no inline message (editor/--amend) — left alone.
- msg=$(printf '%s' "$cmd" | sed -n "s/.*-m[[:space:]]*['\"]\\([^'\"]*\\).*/\\1/p")
- [ -n "$msg" ] || msg=$(printf '%s' "$cmd" | sed -n "s/.*--message[=[:space:]][[:space:]]*['\"]\\([^'\"]*\\).*/\\1/p")
- [ -n "$msg" ] || msg=$(printf '%s' "$cmd" | sed -n "s/.*-m[[:space:]][[:space:]]*\\([^[:space:]'\"][^[:space:]]*\\).*/\\1/p")
- [ -n "$msg" ] || msg=$(printf '%s' "$cmd" | sed -n "s/.*--message[=[:space:]][[:space:]]*\\([^[:space:]'\"][^[:space:]]*\\).*/\\1/p")
+ # The [AI] rule applies to the commit subject, which git takes from the
+ # FIRST -m/--message flag (later -m flags become body paragraphs, e.g. a
+ # Co-Authored-By trailer). ${var#pattern} strips the shortest matching
+ # prefix, i.e. finds the first occurrence; when both flag spellings are
+ # present, the longer remainder is the one whose flag came first.
+ rest=''
+ case "$cmd" in
+ *" --message"*)
+ rest=${cmd#*" --message"}
+ rest=${rest#=} ;;
+ esac
+ case "$cmd" in
+ *" -m"*)
+ m_rest=${cmd#*" -m"}
+ [ ${#m_rest} -gt ${#rest} ] && rest=$m_rest ;;
+ esac
+ # First line of the remainder tells us the message form. An empty result
+ # means no inline message (editor/--amend/-F) — left alone.
+ first=$(printf '%s\n' "$rest" | sed -n '1s/^[[:space:]]*//p')
+ msg=''
+ case "$first" in
+ *'$('*'<<'*)
+ # Heredoc via command substitution: -m "$(cat <<'EOF' ... EOF)".
+ # The subject is the first non-blank line of the heredoc body (git's
+ # default cleanup strips leading blank lines).
+ msg=$(printf '%s\n' "$rest" | sed -n '2,$p' |
+ sed -n '/[^[:space:]]/{s/^[[:space:]]*//;p;q;}') ;;
+ esac
+ # Inline string: strip one leading quote, truncate at the next quote
+ # (good enough for a prefix check). Also the fallback when the heredoc
+ # pattern matched but no body line followed — e.g. a single-line <<<
+ # here-string — so a statically unknowable message fails closed instead
+ # of slipping past as empty.
+ [ -n "$msg" ] || msg=$(printf '%s\n' "$first" | sed "s/^['\"]//; s/['\"].*//")
case "$msg" in
"" | "[AI]"*) ;;
*) block "Blocked: commit messages must start with '[AI]'. Got: $msg" ;;
diff --git a/upcoming-release-notes/8192.md b/upcoming-release-notes/8192.md
new file mode 100644
index 0000000000..fed4f69103
--- /dev/null
+++ b/upcoming-release-notes/8192.md
@@ -0,0 +1,6 @@
+---
+category: Maintenance
+authors: [MatissJanis]
+---
+
+Fix agent git-guard hook false positives on heredoc-style and multi-paragraph commit messages