From 5fb9626533641a3965f54542d261b69af21d1541 Mon Sep 17 00:00:00 2001 From: Matthew Rothenberg Date: Sun, 5 Dec 2021 12:36:06 -0500 Subject: [PATCH 1/4] build(ci): add shellcheck lint --- .github/workflows/lint.yml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 .github/workflows/lint.yml diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..317a217 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,19 @@ +name: reviewdog +on: + push: + branches-ignore: + - 'gh-pages' + +jobs: + shellcheck: + name: shellcheck + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: shellcheck + uses: reviewdog/action-shellcheck@v1 + with: + github_token: ${{ secrets.github_token }} + reporter: github-check + path: "./commands/inits/data" + pattern: "*.sh" From d82e6ef299c18891d797069ab7bd9f24ec98f8b9 Mon Sep 17 00:00:00 2001 From: Matthew Rothenberg Date: Sun, 5 Dec 2021 12:48:47 -0500 Subject: [PATCH 2/4] build(ci): add shellcheck shell hints --- commands/inits/data/aliases.sh | 2 ++ commands/inits/data/git_wrapper.sh | 3 +++ commands/inits/data/status_shortcuts.sh | 2 ++ 3 files changed, 7 insertions(+) diff --git a/commands/inits/data/aliases.sh b/commands/inits/data/aliases.sh index c3201bc..dfdf3fe 100644 --- a/commands/inits/data/aliases.sh +++ b/commands/inits/data/aliases.sh @@ -1,3 +1,5 @@ +# source file: aliases.sh +# shellcheck shell=sh alias gs='scmpuff_status' alias ga='git add' alias gd='git diff' diff --git a/commands/inits/data/git_wrapper.sh b/commands/inits/data/git_wrapper.sh index 2c4c17a..1d6ffb9 100644 --- a/commands/inits/data/git_wrapper.sh +++ b/commands/inits/data/git_wrapper.sh @@ -1,3 +1,6 @@ +# source file: git_wrapper.sh +# shellcheck shell=sh + # Remove any existing git alias or function unalias git > /dev/null 2>&1 unset -f git > /dev/null 2>&1 diff --git a/commands/inits/data/status_shortcuts.sh b/commands/inits/data/status_shortcuts.sh index d22f6e9..b6a2c38 100644 --- a/commands/inits/data/status_shortcuts.sh +++ b/commands/inits/data/status_shortcuts.sh @@ -1,3 +1,5 @@ +# source file: status_shortcuts.sh +# shellcheck shell=sh scmpuff_status() { local scmpuff_env_char="e" From fc3fcdc5437df24819f6a61ab1ae9b2a6cf9fc54 Mon Sep 17 00:00:00 2001 From: Matthew Rothenberg Date: Sun, 5 Dec 2021 20:09:18 -0500 Subject: [PATCH 3/4] fix(init): address shellcheck recommendations --- commands/inits/data/git_wrapper.sh | 2 +- commands/inits/data/status_shortcuts.sh | 20 +++++++++++--------- features/command_init.feature | 2 +- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/commands/inits/data/git_wrapper.sh b/commands/inits/data/git_wrapper.sh index 1d6ffb9..403d35a 100644 --- a/commands/inits/data/git_wrapper.sh +++ b/commands/inits/data/git_wrapper.sh @@ -9,7 +9,7 @@ unset -f git > /dev/null 2>&1 SCMPUFF_GIT_CMD=${SCMPUFF_GIT_CMD:-"$(\which git)"} export SCMPUFF_GIT_CMD -function git() { +git() { case $1 in commit|blame|log|rebase|merge) scmpuff exec -- "$SCMPUFF_GIT_CMD" "$@";; diff --git a/commands/inits/data/status_shortcuts.sh b/commands/inits/data/status_shortcuts.sh index b6a2c38..ec01d9c 100644 --- a/commands/inits/data/status_shortcuts.sh +++ b/commands/inits/data/status_shortcuts.sh @@ -9,7 +9,7 @@ scmpuff_status() { # Run scmpuff status, store output # (`local` needs to be on its own line otherwise exit code is swallowed!) local cmd_output - cmd_output="$(/usr/bin/env scmpuff status --filelist $@)" + cmd_output=$(/usr/bin/env scmpuff status --filelist "$@") # if there was an error, exit prematurely, and pass along the exit code # (STDOUT was swallowed but not STDERR, so user should still see error msg) @@ -19,17 +19,19 @@ scmpuff_status() { fi # Fetch list of files (from first line of script output) + local files files="$(echo "$cmd_output" | head -n 1)" # Export numbered env variables for each file scmpuff_clear_vars - IFS=$'\t' + IFS=$(printf '\t') local e=1 + local file for file in $files; do export $scmpuff_env_char$e="$file" - let e++ + e=$((e+1)) done - IFS=$' \t\n' + IFS=$(printf ' \t\n') # Print status (from line two onward) echo "$cmd_output" | tail -n +2 @@ -42,14 +44,14 @@ scmpuff_status() { # Clear numbered env variables scmpuff_clear_vars() { local scmpuff_env_char="e" - local i - - for (( i=1; i<=999; i++ )); do - local env_var_i=${scmpuff_env_char}${i} - if [[ -n ${env_var_i} ]]; then + local i=0 + while [ $i -le 999 ]; do + env_var_i=${scmpuff_env_char}${i} + if [ -n "$env_var_i" ]; then unset ${env_var_i} else break fi + i=$((i+1)) done } diff --git a/features/command_init.feature b/features/command_init.feature index bb299f2..8a759db 100644 --- a/features/command_init.feature +++ b/features/command_init.feature @@ -26,7 +26,7 @@ Feature: init command Scenario Outline: --wrap controls git cmd wrapping in output (default: yes) When I successfully run `scmpuff init ` - Then the output contain "function git()" + Then the output contain "git() {" Examples: | flags | should? | | -s | should | From 46529d655ed87bd79697551f0fe066a96cf8c7d9 Mon Sep 17 00:00:00 2001 From: Matthew Rothenberg Date: Sun, 30 Jan 2022 11:34:49 -0500 Subject: [PATCH 4/4] fix(init): remove local from POSIX scripts fixes shellcheck SC3043: https://github.com/koalaman/shellcheck/wiki/SC3043 strictly speaking, local is not POSIX, but is supported in many shells, including bash, ksh, dash, and BusyBox ash. We could disable this rule, but perhaps safer to just do the effort to try to be fully POSIX compliant for now, even though it makes the script more difficult to read and reason about. --- commands/inits/data/status_shortcuts.sh | 42 +++++++++++-------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/commands/inits/data/status_shortcuts.sh b/commands/inits/data/status_shortcuts.sh index ec01d9c..3fdd991 100644 --- a/commands/inits/data/status_shortcuts.sh +++ b/commands/inits/data/status_shortcuts.sh @@ -1,57 +1,53 @@ # source file: status_shortcuts.sh # shellcheck shell=sh + scmpuff_status() { - local scmpuff_env_char="e" + __scmpuff_env_char="e" # Ensure shwordsplit is on for zsh if [ -n "$ZSH_VERSION" ]; then setopt shwordsplit; fi; # Run scmpuff status, store output - # (`local` needs to be on its own line otherwise exit code is swallowed!) - local cmd_output - cmd_output=$(/usr/bin/env scmpuff status --filelist "$@") + __scmpuff_status_cmd_output=$(/usr/bin/env scmpuff status --filelist "$@") + __scmpuff_status_exit=$? # if there was an error, exit prematurely, and pass along the exit code # (STDOUT was swallowed but not STDERR, so user should still see error msg) - local es=$? - if [ $es -ne 0 ]; then - return $es + if [ $__scmpuff_status_exit -ne 0 ]; then + return $__scmpuff_status_exit fi # Fetch list of files (from first line of script output) - local files - files="$(echo "$cmd_output" | head -n 1)" + __scmpuff_files="$(echo "$__scmpuff_status_cmd_output" | head -n 1)" # Export numbered env variables for each file scmpuff_clear_vars IFS=$(printf '\t') - local e=1 - local file - for file in $files; do - export $scmpuff_env_char$e="$file" - e=$((e+1)) + __scmpuff_loop_e=1 + for __scmpuff_file in $__scmpuff_files; do + export $__scmpuff_env_char$__scmpuff_loop_e="$__scmpuff_file" + __scmpuff_loop_e=$((__scmpuff_loop_e+1)) #e++ done IFS=$(printf ' \t\n') # Print status (from line two onward) - echo "$cmd_output" | tail -n +2 + echo "$__scmpuff_status_cmd_output" | tail -n +2 # Reset zsh environment to default if [ -n "$ZSH_VERSION" ]; then unsetopt shwordsplit; fi; } - # Clear numbered env variables scmpuff_clear_vars() { - local scmpuff_env_char="e" - local i=0 - while [ $i -le 999 ]; do - env_var_i=${scmpuff_env_char}${i} - if [ -n "$env_var_i" ]; then - unset ${env_var_i} + __scmpuff_env_char="e" + __scmpuff_loop_i=0 + while [ $__scmpuff_loop_i -le 999 ]; do + __scmpuff_env_var_i=${__scmpuff_env_char}${__scmpuff_loop_i} + if [ -n "$__scmpuff_env_var_i" ]; then + unset "$__scmpuff_env_var_i" else break fi - i=$((i+1)) + __scmpuff_loop_i=$((__scmpuff_loop_i+1)) #i++ done }