Skip to content

Commit

Permalink
fix: calling of head and grep in DEBUG mode
Browse files Browse the repository at this point in the history
In DEBUG mode, `head` and `grep` was incorrectly called with `''`.

This caused warnings like:
`grep: ^\s*((#|!)|(#\s*!)|(!\s*#))\s*(/usr(/local)?)?/bin/(env\s+)?(sh|ash|bash|dash|ksh|bats)\b: No such file or directory`

In the end, Differential ShellCheck, in some cases, couldn't identify
shell scripts correctly in DEBUG mode.
  • Loading branch information
jamacku committed Aug 17, 2023
1 parent aeb8b25 commit 7a3068e
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions src/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -131,29 +131,23 @@ has_shebang () {
[[ $# -le 0 ]] && return 1
local file="$1"

! is_debug && local quiet=--quiet

local grep_args=(
"${quiet:-}"
)

# shell shebangs detection
if head -n1 "${file}" | grep "${grep_args[@]}" -E '^\s*((#|!)|(#\s*!)|(!\s*#))\s*(/usr(/local)?)?/bin/(env\s+)?(sh|ash|bash|dash|ksh|bats)\b'; then
if head -n1 "${file}" | grep ${RUNNER_DEBUG:+"--quiet"} -E '^\s*((#|!)|(#\s*!)|(!\s*#))\s*(/usr(/local)?)?/bin/(env\s+)?(sh|ash|bash|dash|ksh|bats)\b'; then
return 0
fi

# ShellCheck shell directive detection
if grep "${grep_args[@]}" -E '^\s*#\s*shellcheck\s+shell=(sh|ash|bash|dash|ksh|bats)\s*' "${file}"; then
if grep ${RUNNER_DEBUG:+"--quiet"} -E '^\s*#\s*shellcheck\s+shell=(sh|ash|bash|dash|ksh|bats)\s*' "${file}"; then
return 0
fi

# Emacs mode detection
if grep "${grep_args[@]}" -E '^\s*#\s+-\*-\s+(sh|ash|bash|dash|ksh|bats)\s+-\*-\s*' "${file}"; then
if grep ${RUNNER_DEBUG:+"--quiet"} -E '^\s*#\s+-\*-\s+(sh|ash|bash|dash|ksh|bats)\s+-\*-\s*' "${file}"; then
return 0
fi

# Vi and Vim modeline filetype detection
if grep "${grep_args[@]}" -E '^\s*#\s+vim?:\s+(set\s+)?(ft|filetype)=(sh|ash|bash|dash|ksh|bats)\s*' "${file}"; then
if grep ${RUNNER_DEBUG:+"--quiet"} -E '^\s*#\s+vim?:\s+(set\s+)?(ft|filetype)=(sh|ash|bash|dash|ksh|bats)\s*' "${file}"; then
return 0
fi

Expand Down

0 comments on commit 7a3068e

Please sign in to comment.