From e691826b4b5fcb15dcee650e3948d00b9e58836c Mon Sep 17 00:00:00 2001 From: oikarinen <7252104+oikarinen@users.noreply.github.com> Date: Sat, 21 Dec 2024 09:16:39 +0200 Subject: [PATCH] Fix all ShellCheck errors and add to CI (#1179) --- .github/workflows/ci.yml | 2 ++ bin/git-create-branch | 2 +- bin/git-fork | 2 +- bin/git-guilt | 8 +++++--- bin/git-obliterate | 8 ++++---- bin/git-repl | 2 ++ bin/git-scp | 13 +++++++------ 7 files changed, 22 insertions(+), 15 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5689a176d..8eceaa3b4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,6 +36,8 @@ jobs: env: # NOTE: use env to pass the output in order to avoid possible injection attacks FILES: "${{ steps.files.outputs.added_modified }}" + - name: Shellcheck + run: shellcheck --severity=error bin/* ./*.sh - name: Lint and format Python with Ruff uses: astral-sh/ruff-action@v2 diff --git a/bin/git-create-branch b/bin/git-create-branch index c11c4ea7a..369a5f8fa 100755 --- a/bin/git-create-branch +++ b/bin/git-create-branch @@ -39,7 +39,7 @@ then REMOTE=origin fi -test -z $BRANCH && echo "branch argument required." 1>&2 && exit 1 +test -z "$BRANCH" && echo "branch argument required." 1>&2 && exit 1 if [[ -n $REMOTE ]] then diff --git a/bin/git-fork b/bin/git-fork index 3d3742fd4..2242c4a5d 100755 --- a/bin/git-fork +++ b/bin/git-fork @@ -61,7 +61,7 @@ else # clone forked repo into current dir git clone "${remote_prefix}${user}/${project}.git" "$project" # add reference to origin fork so can merge in upstream changes - cd "$project" + cd "$project" || exit git remote add upstream "${remote_prefix}${owner}/${project}.git" git fetch upstream fi diff --git a/bin/git-guilt b/bin/git-guilt index 3fe644ac9..30e2e0ad1 100755 --- a/bin/git-guilt +++ b/bin/git-guilt @@ -31,7 +31,7 @@ do esac done -cd "$(git-root)" # cd for git blame +cd "$(git-root)" || exit # cd for git blame MERGED_LOG=$(git_extra_mktemp) if [[ $EMAIL == '-e' ]] then @@ -44,9 +44,11 @@ for file in $(git diff --name-only "$@") do test -n "$DEBUG" && echo "git blame $file" # $1 - since $2 - until + # shellcheck disable=SC2086 git blame $NOT_WHITESPACE --line-porcelain "$1" -- "$file" 2> /dev/null | LC_ALL=C sed -n "$PATTERN" | sort | uniq -c | LC_ALL=C sed 's/^\(.\)/- \1/' >> "$MERGED_LOG" # if $2 not given, use current commit as "until" + # shellcheck disable=SC2086 git blame $NOT_WHITESPACE --line-porcelain "${2-@}" -- "$file" 2> /dev/null | LC_ALL=C sed -n "$PATTERN" | sort | uniq -c | LC_ALL=C sed 's/^\(.\)/+ \1/' >> "$MERGED_LOG" done @@ -71,7 +73,7 @@ END { printf("%d %s\n", contributors[people], people) } } -}' $MERGED_LOG | sort -nr | # only gawk supports built-in sort function +}' "$MERGED_LOG" | sort -nr | # only gawk supports built-in sort function while read -r line do people=${line#* } @@ -103,7 +105,7 @@ do do printf "-" done - printf "(%s)" $num + printf "(%s)" "$num" else for (( i = 0; i > num; i-- )) do diff --git a/bin/git-obliterate b/bin/git-obliterate index 992e0eb6d..83e4ca1c1 100755 --- a/bin/git-obliterate +++ b/bin/git-obliterate @@ -9,15 +9,15 @@ do file="$file"' '"$i" shift done -test -n "$*" && range="$*" +test -n "$*" && range=("$@") test -z "$file" && echo "file required." 1>&2 && exit 1 -if [ -z "$range" ] +if [ -z "${range[*]}" ] then git filter-branch -f --index-filter "git rm -r --cached ""$file"" --ignore-unmatch" \ --prune-empty --tag-name-filter cat -- --all else - # don't quote $range so that we can forward multiple rev-list arguments + # $range is an array so that we can forward multiple rev-list arguments git filter-branch -f --index-filter "git rm -r --cached ""$file"" --ignore-unmatch" \ - --prune-empty --tag-name-filter cat -- $range + --prune-empty --tag-name-filter cat -- "${range[@]}" fi diff --git a/bin/git-repl b/bin/git-repl index a1368793d..a706de37a 100755 --- a/bin/git-repl +++ b/bin/git-repl @@ -44,8 +44,10 @@ while true; do esac if [[ $cmd == !* ]]; then + # shellcheck disable=SC2086 eval ${cmd:1} elif [[ $cmd == git* ]]; then + # shellcheck disable=SC2086 eval $cmd else eval git "$cmd" diff --git a/bin/git-scp b/bin/git-scp index 52d4d1463..7a10a1fec 100755 --- a/bin/git-scp +++ b/bin/git-scp @@ -59,7 +59,7 @@ function php_lint() function _dos2unix() { - command -v dos2unix > /dev/null && dos2unix $@ + command -v dos2unix > /dev/null && dos2unix "$@" return 0 } @@ -68,8 +68,8 @@ function _sanitize() git config --get-all extras.scp.sanitize | while read -r i do case $i in - php_lint) php_lint $@;; # git config --global --add extras.scp.sanitize php_lint - dos2unix) _dos2unix $@;; # git config --global --add extras.scp.sanitize dos2unix + php_lint) php_lint "$@";; # git config --global --add extras.scp.sanitize php_lint + dos2unix) _dos2unix "$@";; # git config --global --add extras.scp.sanitize dos2unix esac done return $? @@ -107,6 +107,7 @@ function scp_and_stage if [ -n "$list" ] then local _TMP=${0///} + # shellcheck disable=SC2086 echo "$list" > "$_TMP" && _sanitize $list && _info "Pushing to $remote ($(git config "remote.$remote.url"))" && @@ -131,7 +132,7 @@ function reverse_scp() shift local _TMP=${0///} - echo $@ > "$_TMP" && + echo "$@" > "$_TMP" && rsync -rlDv --files-from="$_TMP" "$(git config "remote.$remote.url")/" ./ && rm "$_TMP" } @@ -173,8 +174,8 @@ case $(basename "$0") in git-scp) case $1 in ''|-h|'?'|help|--help) shift; _test_git_scp; _usage "$@";; - *) scp_and_stage $@;; + *) scp_and_stage "$@";; esac ;; - git-rscp) reverse_scp $@;; + git-rscp) reverse_scp "$@";; esac