Skip to content

Commit e036d2e

Browse files
committed
line ending pre-commit hook
1 parent 9d1c718 commit e036d2e

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

.husky/pre-commit

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/sh
2+
# Convert CRLF to LF in all staged files before committing.
3+
# This guards against AI tooling and Windows environments introducing CRLF.
4+
5+
STAGED=$(git diff --cached --name-only)
6+
7+
if [ -z "$STAGED" ]; then
8+
exit 0
9+
fi
10+
11+
FIXED=0
12+
13+
for file in $STAGED; do
14+
# Skip files that no longer exist on disk (e.g. deleted files)
15+
if [ ! -f "$file" ]; then
16+
continue
17+
fi
18+
19+
# Check for carriage returns
20+
if grep -qU $'\r' "$file"; then
21+
# perl -pi works cross-platform (macOS BSD and Linux GNU)
22+
perl -pi -e 's/\r\n/\n/g; s/\r/\n/g' "$file"
23+
FIXED=1
24+
fi
25+
done
26+
27+
if [ "$FIXED" = "1" ]; then
28+
echo "pre-commit: CRLF line endings converted to LF in staged files."
29+
git add --update
30+
fi

0 commit comments

Comments
 (0)