Skip to content

Commit

Permalink
Check scripts with shellcheck before accepting new commands
Browse files Browse the repository at this point in the history
Only check scripts that have been added in the current pull request.

Do not check them on macOS as homebrew is slow to install shellcheck and
the macOS cut command does not support an empty -d argument.
  • Loading branch information
pabs3 committed Feb 4, 2020
1 parent b708a8a commit da4b0c2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ addons:
apt:
packages:
- bsdmainutils
- shellcheck
script:
- ./check_integrity.sh $(find bin | cut -b 5- | xargs)
- ./check_changed_files.sh
# Test for the brew release
- mkdir ../release && git archive --format=tar.gz HEAD > ../release/git-extras-release.tar.gz
- cd ../release
Expand Down
13 changes: 13 additions & 0 deletions check_changed_files.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

err() {
echo >&2 "$1"
exit 1
}

if command -v shellcheck > /dev/null ; then
# Check all changed shell scripts
if ! git diff -z --name-only "$TRAVIS_BRANCH...HEAD" | xargs -0 ./shellcheck.sh ; then
err "Please fix the shellcheck issues"
fi
fi
17 changes: 17 additions & 0 deletions shellcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
files=()
for f ; do
if [ -r "$f" ] ; then
file --mime-type -r0 "$f" |
cut -d "" -f 2 |
grep -q "^: text/x-shellscript$" &&
files+=("$f")
fi
done
if [ ${#files[@]} -gt 0 ] ; then
shellcheck "${files[@]}"
ret=$?
if [ $ret -eq 1 ] || [ $ret -eq 2 ] ; then
exit 1
fi
fi

0 comments on commit da4b0c2

Please sign in to comment.