File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments