Repository navigation
feat(lint): fail on text files committed with CRLF line endings #584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
67f718d
feat(lint): fail on text files committed with CRLF line endings
DevSecNinja c62eb8a
fix(lint): scan line endings before tool setup and name the full fix
DevSecNinja 4c3f344
chore(lint): take main's architecture.md and #591's tests before merg…
DevSecNinja fb0ff13
Merge branch 'main' into steward/lint-line-endings-v2
DevSecNinja 0d7ebae
docs(lint): restore the line-endings section next to the zizmor config
DevSecNinja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| #!/usr/bin/env bash | ||
| # | ||
| # Fails when a tracked text file is committed with CRLF (or mixed) line | ||
| # endings, using git's own attribute resolution (`git ls-files --eol`): | ||
| # a path whose .gitattributes rule unsets `text` (`-text`, or `binary`) is | ||
| # stored byte for byte and skipped. An `eol=crlf` path passes once git | ||
| # stores it with LF (it only checks it out with CRLF); one still stored | ||
| # with CRLF is reported, like any other, until `git add --renormalize`. | ||
| # A step of .github/workflows/lint.yml, run the same way in both lint modes. | ||
| # Env: LINT_LINE_ENDINGS_BLOCKING (true: errors, fail; false: warnings only). | ||
|
|
||
| set -euo pipefail | ||
| # Bytes, not characters: in a UTF-8 locale `read -d ''` can swallow the NUL | ||
| # after a path that ends in an incomplete multibyte sequence. | ||
| export LC_ALL=C | ||
|
|
||
| blocking="${LINT_LINE_ENDINGS_BLOCKING:-true}" | ||
| case "${blocking}" in | ||
| true) level=error ;; | ||
| false) level=warning ;; | ||
| *) | ||
| echo "::error::lint-line-endings-blocking must be true or false (got '${blocking}')." | ||
| exit 2 | ||
| ;; | ||
| esac | ||
|
|
||
| # Escapes the message of a workflow command, and (escape_property) a | ||
| # property value such as file=.... | ||
| escape_data() { | ||
| local value="$1" | ||
| value="${value//%/%25}" | ||
| value="${value//$'\r'/%0D}" | ||
| value="${value//$'\n'/%0A}" | ||
| printf '%s' "${value}" | ||
| } | ||
| escape_property() { | ||
| local value | ||
| value="$(escape_data "$1")" | ||
| value="${value//:/%3A}" | ||
| value="${value//,/%2C}" | ||
| printf '%s' "${value}" | ||
| } | ||
|
|
||
| max_annotations=50 | ||
| checked=0 | ||
| found=() | ||
| # A file, not a process substitution: `set -e` then stops on a failing | ||
| # `git ls-files` (no repository, dubious ownership) instead of passing. | ||
| listing="$(mktemp)" | ||
| trap 'rm -f "${listing}"' EXIT | ||
| git ls-files --eol -z >"${listing}" | ||
| # Each record is "i/<index eol> w/<worktree eol> attr/<attributes>\t<path>"; | ||
| # split at the first tab only, so a path keeps its own tabs. | ||
| while IFS= read -r -d '' record; do | ||
|
DevSecNinja marked this conversation as resolved.
Resolved
|
||
| checked=$((checked + 1)) | ||
| info="${record%%$'\t'*}" | ||
| path="${record#*$'\t'}" | ||
| case "${info%% *}" in | ||
| i/crlf | i/mixed) ;; | ||
| *) continue ;; | ||
| esac | ||
| attributes="${info#*attr/}" | ||
| case " ${attributes} " in | ||
| *" -text "*) continue ;; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| *) ;; | ||
| esac | ||
| found+=("${path}") | ||
| done <"${listing}" | ||
|
|
||
| if [ "${#found[@]}" -eq 0 ]; then | ||
| echo "✓ No text file is committed with CRLF line endings (${checked} files checked)." | ||
| exit 0 | ||
| fi | ||
|
|
||
| for path in "${found[@]:0:${max_annotations}}"; do | ||
| echo "::${level} file=$(escape_property "${path}"),title=CRLF line endings::$(escape_data "${path}") is committed with CRLF line endings." | ||
| done | ||
| [ "${#found[@]}" -le "${max_annotations}" ] || | ||
| echo "… and $((${#found[@]} - max_annotations)) more file(s)." | ||
| echo "::${level} title=CRLF line endings::${#found[@]} text file(s) are committed with CRLF line endings. Store them with LF: put \"* text=auto eol=lf\" in .gitattributes, run \"git add .gitattributes\" and \"git add --renormalize .\", and commit both. A file that needs CRLF gets its own .gitattributes rule: \"eol=crlf\" (CRLF in the checkout, LF in the repository) or \"-text\" (stored byte for byte)." | ||
| [ "${blocking}" = false ] || exit 1 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.