Repository navigation
Enforce clang-format 20 and commentflow in CI #102
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| function build_mod() | ||
| { | ||
| { | ||
| make all || 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| # Reflow comments to the column limit, or check that they already are. | ||
| # | ||
| # clang-format breaks a comment line that runs past ColumnLimit but never | ||
| # refills a short-wrapped one, so comment width is the one part of the house | ||
| # style no other gate settles. commentflow reads the same ColumnLimit from | ||
| # .clang-format, which keeps comment width and code width on one number. | ||
| # | ||
| # The selection below is shared by --write and the default check, so what gets | ||
| # rewritten is what gets checked. | ||
| # | ||
| # Exit codes: 0 clean, 1 files would change, 2 the check could not run. | ||
|
|
||
| set -u -o pipefail | ||
|
|
||
| cd "$(dirname "${BASH_SOURCE[0]}")/.." || exit 2 | ||
|
|
||
| case "${1:-}" in | ||
| "" | --check) write=0 ;; | ||
| --write) write=1 ;; | ||
| *) | ||
| echo "usage: $0 [--check | --write]" >&2 | ||
| exit 2 | ||
| ;; | ||
| esac | ||
|
|
||
| COMMENTFLOW="${COMMENTFLOW:-commentflow}" | ||
|
|
||
| if ! command -v "$COMMENTFLOW" > /dev/null 2>&1; then | ||
| echo "Error: $COMMENTFLOW not found" >&2 | ||
| echo "Install it from https://github.com/sysprog21/commentflow" >&2 | ||
| exit 2 | ||
| fi | ||
|
|
||
| # Untracked but non-ignored files are included, so a new file is covered before | ||
| # it is staged. A while-read loop because macOS ships bash 3.2 (no mapfile). | ||
| # Enumerated before the loop so a failing git is an error, not an empty list. | ||
| list=$(mktemp) | ||
| trap 'rm -f "$list"' EXIT | ||
| git ls-files -z --cached --others --exclude-standard \ | ||
| -- '*.c' '*.h' '*.sh' > "$list" || exit 2 | ||
| files=() | ||
| while IFS= read -r -d '' f; do | ||
| # A tracked file deleted from the worktree is not a reflow target. | ||
| [ -e "$f" ] && files+=("$f") | ||
| done < "$list" | ||
|
|
||
| if [ "${#files[@]}" -eq 0 ]; then | ||
| echo "Error: no files selected; the pathspec no longer matches the tree" >&2 | ||
| exit 2 | ||
| fi | ||
|
|
||
| printf ' CFLOW %d files\n' "${#files[@]}" | ||
|
|
||
| if [ "$write" -eq 1 ]; then | ||
| exec "$COMMENTFLOW" "${files[@]}" | ||
| fi | ||
|
|
||
| status=0 | ||
| "$COMMENTFLOW" --check "${files[@]}" || status=$? | ||
|
|
||
| if [ "$status" -eq 0 ]; then | ||
| echo "Comment reflow clean." | ||
| exit 0 | ||
| fi | ||
|
|
||
| # Exit 1 means "these files would change"; anything else is the tool failing to | ||
| # read them, which must not be reported as a style violation. | ||
| if [ "$status" -ne 1 ]; then | ||
| echo "Error: $COMMENTFLOW exited $status (I/O or parse error)" >&2 | ||
| exit "$status" | ||
| fi | ||
|
|
||
| echo "Run '$0 --write' to reflow, or '$COMMENTFLOW --diff <file>' to see one." >&2 | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,62 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| SOURCES=$(find $(git rev-parse --show-toplevel) | egrep "\.(cpp|cc|c|h)\$") | ||
|
|
||
| CLANG_FORMAT=$(which clang-format-12) | ||
| if [ $? -ne 0 ]; then | ||
| CLANG_FORMAT=$(which clang-format) | ||
| if [ $? -ne 0 ]; then | ||
| echo "[!] clang-format not installed. Unable to check source file format policy." >&2 | ||
| exit 1 | ||
| # Verify clang-format conformance for all tracked C/H files. The repository's | ||
| # .clang-format is calibrated against clang-format-20; other versions produce | ||
| # different output and are rejected to keep CI deterministic. | ||
| # | ||
| # Exit codes, matching .ci/check-commentflow.sh: 0 clean, 1 files differ from | ||
| # the style, 2 the check could not run. | ||
|
|
||
| set -u -o pipefail | ||
|
|
||
| cd "$(dirname "${BASH_SOURCE[0]}")/.." || exit 2 | ||
|
|
||
| if [ -z "${CLANG_FORMAT:-}" ]; then | ||
| if command -v clang-format-20 > /dev/null 2>&1; then | ||
| CLANG_FORMAT="clang-format-20" | ||
| else | ||
| CLANG_FORMAT="clang-format" | ||
| fi | ||
| fi | ||
|
|
||
| set -x | ||
| # The pin is checked on whatever binary was resolved, an explicit CLANG_FORMAT | ||
| # included, so it cannot be bypassed by naming one. | ||
| if ! "$CLANG_FORMAT" --version 2> /dev/null | grep -qE 'version 20\.'; then | ||
| echo "Error: clang-format 20 is required (other versions differ in style)" >&2 | ||
| exit 2 | ||
| fi | ||
|
|
||
| expected=$(mktemp) | ||
| trap 'rm -f "$expected"' EXIT | ||
|
|
||
| # Enumerate first, NUL-delimited: a failing git inside a process substitution | ||
| # would be indistinguishable from "no files" and pass the gate. | ||
| list=$(mktemp) | ||
| trap 'rm -f "$expected" "$list"' EXIT | ||
| git ls-files -z -- '*.c' '*.h' > "$list" || exit 2 | ||
| if [ ! -s "$list" ]; then | ||
| echo "Error: no C sources tracked; the pathspec no longer matches" >&2 | ||
| exit 2 | ||
| fi | ||
|
|
||
| # A tool failure (2) outranks a style difference (1). | ||
| rc=0 | ||
| while IFS= read -r -d '' file; do | ||
| # A tracked file deleted from the worktree is not a style defect. | ||
| [ -e "$file" ] || continue | ||
| if ! "$CLANG_FORMAT" "$file" > "$expected"; then | ||
| echo "Error: $CLANG_FORMAT failed on $file" >&2 | ||
| rc=2 | ||
| continue | ||
| fi | ||
| st=0 | ||
| diff -u -p --label="$file" --label="expected coding style" \ | ||
| "$file" "$expected" || st=$? | ||
| if [ "$st" -gt 1 ]; then | ||
| rc=2 | ||
| elif [ "$st" -eq 1 ] && [ "$rc" -eq 0 ]; then | ||
| rc=1 | ||
| fi | ||
| done < "$list" | ||
|
|
||
| for file in ${SOURCES}; | ||
| do | ||
| $CLANG_FORMAT ${file} > expected-format | ||
| diff -u -p --label="${file}" --label="expected coding style" ${file} expected-format | ||
| done | ||
| exit $($CLANG_FORMAT --output-replacements-xml ${SOURCES} | egrep -c "</replacement>") | ||
| exit "$rc" |
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,6 @@ | ||
| version: 2 | ||
| updates: | ||
| - package-ecosystem: github-actions | ||
| directory: / | ||
| schedule: | ||
| interval: weekly |
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
Oops, something went wrong.
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.