Skip to content

Commit 0fa9d79

Browse files
authored
Improve code formatting checks (AliceO2Group#676)
* Add whitespace formatting check * Mark incorrect copyright notices in GitHub UI
1 parent c370877 commit 0fa9d79

1 file changed

Lines changed: 65 additions & 11 deletions

File tree

.github/workflows/code-formatting.yml

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
name: PR formatting
1+
name: Formatting
22

33
on: [pull_request_target]
44

55
jobs:
66
clang-format:
77
name: clang-format
88
# We need at least 20.04 to install clang-format-11.
9-
runs-on: ubuntu-20.04
9+
runs-on: ubuntu-latest
1010

1111
steps:
12-
- uses: actions/checkout@v2
12+
- name: Checkout code
13+
uses: actions/checkout@v2
1314
with:
1415
ref: ${{ github.event.pull_request.head.sha }}
1516
persist-credentials: false
@@ -80,7 +81,7 @@ jobs:
8081
echo ::set-output name=clean::false
8182
fi
8283
83-
- name: pull-request
84+
- name: Create pull request with formatting changes
8485
uses: alisw/pull-request@master
8586
with:
8687
source_branch: 'alibuild:alibot-cleanup-${{ github.event.pull_request.number }}'
@@ -107,7 +108,8 @@ jobs:
107108
runs-on: ubuntu-latest
108109

109110
steps:
110-
- uses: actions/checkout@v2
111+
- name: Checkout code
112+
uses: actions/checkout@v2
111113
with:
112114
ref: ${{ github.event.pull_request.head.sha }}
113115
persist-credentials: false
@@ -134,19 +136,71 @@ jobs:
134136
# Run copyright notice check
135137
COPYRIGHT=$(printf '%s' "$COPYRIGHT") # strip trailing newline
136138
copyright_lines=$(echo "$COPYRIGHT" | wc -l)
137-
base_commit=$(git merge-base HEAD ${{ github.event.pull_request.base.sha }})
138139
have_err=
139140
while read -r file; do
140141
if [ "$(head -n "$copyright_lines" "$file")" != "$COPYRIGHT" ]; then
141-
echo "::error::$file: missing or malformed copyright notice" >&2
142+
echo "::error file=$file,line=1,endLine=$copyright_lines::Missing or malformed copyright notice"
142143
have_err=1
143144
fi
144-
done < <(git diff --diff-filter d --name-only "$base_commit" -- '*.cxx' '*.h')
145+
done < <(git diff --diff-filter d --name-only --merge-base \
146+
${{ github.event.pull_request.base.sha }} -- '*.cxx' '*.h')
145147
146148
# Tell user what to do in case of copyright notice error
147149
if [ -n "$have_err" ]; then
148-
echo '::error::The files listed above are missing the correct copyright notice.' >&2
149-
echo '::error::Make sure all your source files begin with the following exact lines:' >&2
150-
echo "$COPYRIGHT" >&2
150+
echo '::error::The files listed above are missing the correct copyright notice.'
151+
echo '::error::Make sure all your source files begin with the following exact lines:'
152+
echo "$COPYRIGHT"
151153
exit 1
152154
fi
155+
156+
whitespace:
157+
name: whitespace
158+
runs-on: ubuntu-latest
159+
160+
steps:
161+
- name: Checkout code
162+
uses: actions/checkout@v2
163+
with:
164+
fetch-depth: 0 # needed to get the full history
165+
166+
- name: Fetch upstream
167+
run: |
168+
# Fetch the main upstream branch to find the common ancestor
169+
git remote add upstream https://github.com/AliceO2Group/O2Physics.git
170+
git fetch upstream ${{ github.event.pull_request.base.ref }}
171+
172+
- name: Find bad spacing
173+
env:
174+
base_branch: ${{ github.event.pull_request.base.ref }}
175+
run: |
176+
# Find changed files, ignoring binary files.
177+
readarray -d '' files < \
178+
<(git diff -z --diff-filter d --name-only --merge-base "upstream/$base_branch" |
179+
xargs -0r sh -c 'for f in "$@"; do file -bi "$f" | grep -q charset=binary || printf "%s\\0" "$f"; done' -)
180+
echo 'Changed text files are:'
181+
printf '%s\n' "${files[@]}"
182+
# Find tabs and trailing whitespaces in modified text files and show where they are.
183+
exec awk '
184+
BEGIN { exit_code = 0 }
185+
function error(message) {
186+
printf "::error file=%s,line=%i,col=%i,endColumn=%i::%s\n",
187+
FILENAME, FNR, RSTART, RSTART + RLENGTH, message
188+
exit_code = 1
189+
}
190+
match($0, / +$/) { error("Trailing spaces found") }
191+
match($0, /\t+/) { error("Tab character found -- use spaces to indent") }
192+
END {
193+
if (exit_code != 0) {
194+
print "::notice::Fix the errors in your editor (or with a command)"
195+
print "::notice::Command tips:"
196+
print "::notice::- Get list of files you changed: git diff --diff-filter d --name-only --merge-base upstream/" ENVIRON["base_branch"]
197+
print "::notice::- Replace each tab with two spaces: sed -i \"s/\\t/ /g\" <files>"
198+
print "::notice::- Remove trailing whitespaces: sed -i \"s/[[:space:]]*$//\" <files>"
199+
print "::notice::To avoid these errors, configure your editor to:"
200+
print "::notice::- Emit spaces when the Tab key is pressed."
201+
print "::notice::- Display whitespace characters."
202+
print "::notice::- Replace tabs with spaces and remove trailing whitespaces when a file is saved."
203+
}
204+
exit exit_code
205+
}
206+
' "${files[@]}"

0 commit comments

Comments
 (0)