Skip to content

Commit a1c6bd9

Browse files
mvvmmtest
andauthored
Prevent CLRF line endings with pre-commit hook (#29169)
* test: verify CRLF hook * chore: remove CRLF hook test file * line ending pre-commit hook * add CI check for line endings as well * test: spaces in filename * chore: remove spaces filename test file * remove compile changes * just check the whole repo * dont need to scope it to specific files --------- Co-authored-by: test <test@test.com>
1 parent d707225 commit a1c6bd9

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ jobs:
107107
node-version: 22.x
108108
cache: npm
109109

110+
- name: Check for CRLF line endings
111+
run: |
112+
if git grep -Il $'\r'; then
113+
echo "::error::CRLF line endings detected. Configure your editor to use LF line endings (this repo has an .editorconfig file that most editors respect automatically)."
114+
exit 1
115+
fi
116+
110117
- run: |
111118
FILES=$(
112119
find src/content \

.husky/pre-commit

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
while IFS= read -r file; 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 << EOF
26+
$STAGED
27+
EOF
28+
29+
if [ "$FIXED" = "1" ]; then
30+
echo "pre-commit: CRLF line endings converted to LF in staged files."
31+
git add --update
32+
fi

package-lock.json

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"ai-setup:import-from-opencode": "npx rulesync import --targets opencode --features rules,commands,subagents",
2929
"ai-setup:claudecode": "npm run ai-setup:import-from-opencode && npx rulesync generate --targets claudecode --features rules,commands,subagents",
3030
"ai-setup:cursor": "npm run ai-setup:import-from-opencode && npx rulesync generate --targets cursor --features commands,subagents",
31-
"ai-setup:copilot": "npm run ai-setup:import-from-opencode && npx rulesync generate --targets copilot --features commands,subagents"
31+
"ai-setup:copilot": "npm run ai-setup:import-from-opencode && npx rulesync generate --targets copilot --features commands,subagents",
32+
"prepare": "husky"
3233
},
3334
"devDependencies": {
3435
"@actions/core": "1.11.1",
@@ -86,6 +87,7 @@
8687
"hast-util-select": "6.0.4",
8788
"hastscript": "9.0.1",
8889
"he": "1.2.0",
90+
"husky": "9.1.7",
8991
"jsonc-parser": "3.3.1",
9092
"ldrs": "1.1.7",
9193
"lz-string": "1.5.0",

0 commit comments

Comments
 (0)