From 345ea1f3b90c9d91341bd7bddf58561c0c719fb4 Mon Sep 17 00:00:00 2001 From: Factory Ben Date: Sat, 22 Nov 2025 09:43:15 +0000 Subject: [PATCH] docs(hooks): fix co-authorship hook JSON escaping bug The previous implementation had critical bugs: 1. Literal newlines in JSON output broke JSON structure 2. sed command failed when embedding multiline strings 3. Shell variable expansion corrupted the JSON Fixed by using jq with --arg to properly handle string escaping and JSON construction. --- .../cli/configuration/hooks/git-workflows.mdx | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/docs/cli/configuration/hooks/git-workflows.mdx b/docs/cli/configuration/hooks/git-workflows.mdx index ed746d5..739c3c8 100644 --- a/docs/cli/configuration/hooks/git-workflows.mdx +++ b/docs/cli/configuration/hooks/git-workflows.mdx @@ -508,27 +508,27 @@ if echo "$commit_msg" | grep -qE "Co-authored-by:"; then exit 0 fi -# Add factory droid co-author +# Add factory droid co-author with proper newline handling coauthor="Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>" - -# Modify command to include co-author -modified_msg="$commit_msg - -$coauthor" - -# Return modified command via JSON output -cat << EOF -{ - "hookSpecificOutput": { - "hookEventName": "PreToolUse", - "permissionDecision": "allow", - "permissionDecisionReason": "Adding co-author to commit", - "updatedInput": { - "command": "$(echo "$command" | sed -E "s/(git commit.*-m[= ]*)[\"']([^\"']+)[\"']/\1\"$modified_msg\"/")" +modified_msg="${commit_msg}\n\n${coauthor}" + +# Build new command with properly escaped message +new_command=$(echo "$command" | sed -E "s|(git commit.*-m[= ]*)[\"']([^\"']+)[\"']|\1|") +new_command="${new_command} -m \"${modified_msg}\"" + +# Use jq to properly construct JSON with escaped strings +jq -n \ + --arg cmd "$new_command" \ + '{ + "hookSpecificOutput": { + "hookEventName": "PreToolUse", + "permissionDecision": "allow", + "permissionDecisionReason": "Adding co-author to commit", + "updatedInput": { + "command": $cmd + } } - } -} -EOF + }' exit 0 ```