Skip to content

Commit

Permalink
check-gitrange.bash: make it actually generic, shellcheck-independent
Browse files Browse the repository at this point in the history
Ready for pylint or anything else.

Signed-off-by: Marc Herbert <marc.herbert@intel.com>
  • Loading branch information
marc-hb authored and xiulipan committed Oct 27, 2020
1 parent 65db441 commit 336094b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ jobs:
- name: "shellcheck"
# Triple-dotted 'origin/master...HEAD'
script: ./tools/CI/check-gitrange.bash "${TRAVIS_COMMIT_RANGE}"
text/x-shellscript shellcheck -x
44 changes: 36 additions & 8 deletions tools/CI/check-gitrange.bash
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,37 @@

set -e

# Sample usage:
#
# $ shellcheck-gitrange.bash origin/master... [ -f gcc ]
usage()
{
cat <<EOFUSAGE
Sample usage:
$0 origin/master... text/x-shellscript shellcheck -x -f gcc
$0 origin/master... text/x-python pylint --disable=C
EOFUSAGE

exit 1
}

main()
{
# git outputs relative paths
local git_top; git_top=$(git rev-parse --show-toplevel)
cd "$git_top"

# The rest of args is passed as is to shellcheck
local diffrange="$1"; shift
local diffrange="$1"; shift || usage

printf '%s checking diff range: %s\n\n' "$0" "$diffrange"

local checked_ftype="$1"; shift || usage
file --list | grep -qF "$checked_ftype" ||
die 'The file command does not know what %s is\n' "$checked_ftype"

local checker="$1"; shift || usage
type "$checker" || die 'Checker %s not found\n' "$checker"

# Triple dot "git log A...B" includes commits not relevant to triple
# dot "git diff A...B"
local logrange=${diffrange/.../..}
Expand All @@ -36,14 +52,26 @@ main()
# directory".
stat "$fname" > /dev/null
ftype=$(file --brief --mime-type "$fname")
if [ x'text/x-shellscript' = x"$ftype" ]; then
printf '\n\n ----- shellcheck %s ----\n\n' "$fname"
shellcheck -x "$@" "$fname" || : $((failed_files++))
if [ x"$checked_ftype" = x"$ftype" ]; then
printf '\n\n ----- %s' "$checker"
printf ' %s' "$@"
printf ' %s ----\n\n' "$fname"
"$checker" "$@" "$fname" || : $((failed_files++))
fi

done < <(git diff --name-only --diff-filter=d "$diffrange" -- )

return $failed_files
}


die()
{
>&2 printf '%s ERROR: ' "$0"
# We want die() to be usable exactly like printf
# shellcheck disable=SC2059
>&2 printf "$@"
exit 1
}

main "$@"

0 comments on commit 336094b

Please sign in to comment.