Skip to content

Commit b744daa

Browse files
nearestnaborsclaude
andcommitted
Fix pre-commit hook to not stage unrelated working directory changes
The update-links section was using `git diff --name-only` which captures ALL modified files in the working directory, not just files modified by the update-links script. This could accidentally stage files the user had modified but intentionally not staged for the current commit. Fix: Capture the list of already-modified files BEFORE running update-links, then only stage files that weren't already modified (i.e., files that became modified as a result of update-links). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent da0ae54 commit b744daa

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

.husky/pre-commit

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,29 @@ fi
6363
if git diff --cached --name-only | grep -q "next.config.ts"; then
6464
echo "🔗 Updating internal links for new redirects..."
6565

66+
# Capture files already modified in working directory BEFORE running update-links
67+
# This prevents accidentally staging unrelated changes the user has in progress
68+
ALREADY_MODIFIED=$(git diff --name-only -- 'app/**/*.mdx' 'app/**/*.tsx' 'app/**/*.md' 2>/dev/null || true)
69+
6670
# Run the TypeScript update script
6771
if pnpm update-links 2>/dev/null; then
68-
# Stage any files that were modified
69-
UPDATED_FILES=$(git diff --name-only -- 'app/**/*.mdx' 'app/**/*.tsx' 'app/**/*.md' 2>/dev/null || true)
70-
if [ -n "$UPDATED_FILES" ]; then
71-
echo "$UPDATED_FILES" | xargs git add
72-
echo "✅ Internal links updated and staged"
72+
# Find files modified AFTER running update-links
73+
NOW_MODIFIED=$(git diff --name-only -- 'app/**/*.mdx' 'app/**/*.tsx' 'app/**/*.md' 2>/dev/null || true)
74+
75+
# Only stage files that were NOT already modified (i.e., newly modified by update-links)
76+
STAGED_COUNT=0
77+
if [ -n "$NOW_MODIFIED" ]; then
78+
for file in $NOW_MODIFIED; do
79+
# Check if this file was already modified before update-links ran
80+
if [ -z "$ALREADY_MODIFIED" ] || ! echo "$ALREADY_MODIFIED" | grep -qxF "$file"; then
81+
git add "$file"
82+
STAGED_COUNT=$((STAGED_COUNT + 1))
83+
fi
84+
done
85+
fi
86+
87+
if [ "$STAGED_COUNT" -gt 0 ]; then
88+
echo "✅ Internal links updated and staged ($STAGED_COUNT file(s))"
7389
fi
7490
fi
7591
fi

0 commit comments

Comments
 (0)