Skip to content

Commit 3322138

Browse files
imlk0oxr463
authored andcommitted
ci: add shellcheck to CI workflow
1 parent 4a90504 commit 3322138

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

.githooks/pre-commit

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
#!/bin/bash
22

33
echo "Starting check shell code issues ..."
4-
find . -name "*.sh" | xargs --no-run-if-empty shellcheck || exit 1
4+
find . -name "*.sh" -print0 | xargs -0 --no-run-if-empty shellcheck || exit 1
55

66
echo "Starting check Rust code format ..."
77
has_issues=0
88
edition=2018
99

1010
for file in $(git diff --name-only --staged | grep '\.rs$'); do
11-
if [ -f ${file} ] && ! rustfmt --edition ${edition} --check --color auto ${file}; then
11+
if [ -f "${file}" ] && ! rustfmt --edition ${edition} --check --color auto "${file}"; then
1212
echo ""
1313
has_issues=1
14-
rustfmt --edition ${edition} ${file}
14+
rustfmt --edition ${edition} "${file}"
1515
fi
1616
done
1717

1818
if [ ${has_issues} -eq 0 ]; then
1919
exit 0
2020
fi
2121

22-
echo 'Your code contains formatting issues and has been corrected. Please run `git add` to add them and commit them.'
22+
echo "Your code contains formatting issues and has been corrected. Please run \`git add\` to add them and commit them."
2323
exit 1

.github/workflows/rust.yml

+24-3
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,30 @@ jobs:
2020
- name: Check
2121
shell: bash
2222
run: |
23-
# Run code formatting check
23+
set +o pipefail
24+
25+
# Determine the two commits to be compared
26+
if [ "$GITHUB_BASE_REF" ]; then # is pull_request event
27+
before_commit=${{ github.event.pull_request.base.sha }}
28+
else # is push event
29+
if [ "${{ github.event.before }}" = "0000000000000000000000000000000000000000" ]; then
30+
before_commit="HEAD~1"
31+
else
32+
before_commit=${{ github.event.before }}
33+
fi
34+
fi
35+
after_commit=$GITHUB_SHA
36+
echo "before_commit: ${before_commit} after_commit: ${after_commit}"
37+
git fetch origin "${before_commit}" --depth=1
38+
39+
# Run Shell code issues check
40+
git diff --name-only "${before_commit}".."${after_commit}" | grep '\.rs$' | xargs --no-run-if-empty ls -1df 2>/dev/null | xargs --no-run-if-empty shellcheck || { echo "shell check failed and exit"; exit 1; }
41+
echo 'Shell code issues check passed.'
42+
43+
# Run Rust code formatting check
2444
has_issues=0
25-
for file in $(git diff --name-only ${{ github.event.before }}..${{ github.event.after }} | grep '\.rs$'); do
26-
if [ -f ${file} ] && ! rustfmt --edition 2018 --check --color auto ${file}; then
45+
for file in $(git diff --name-only "${before_commit}".."${after_commit}" | grep '\.rs$'); do
46+
if [ -f "${file}" ] && ! rustfmt --edition 2018 --check --color auto "${file}"; then
2747
echo ""
2848
has_issues=1
2949
fi
@@ -34,6 +54,7 @@ jobs:
3454
echo 'Code formatting issues detected.'
3555
exit 1
3656
fi
57+
3758
# Run clippy check
3859
cargo clippy
3960
- name: Build

0 commit comments

Comments
 (0)