From fa85d00b957126ac56c53d4eb5faaa7f4d6f1ac2 Mon Sep 17 00:00:00 2001 From: XuehaiPan Date: Sun, 7 Nov 2021 21:00:01 +0800 Subject: [PATCH 1/5] style: implement shellcheck autofix in `brew style --fix` --- Library/Homebrew/style.rb | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/style.rb b/Library/Homebrew/style.rb index 872004000..1eb36b338 100644 --- a/Library/Homebrew/style.rb +++ b/Library/Homebrew/style.rb @@ -62,7 +62,7 @@ module Homebrew shellcheck_result = if ruby_files.any? && shell_files.none? output_type == :json ? [] : true else - run_shellcheck(shell_files, output_type) + run_shellcheck(shell_files, output_type, fix: fix) end shfmt_result = if ruby_files.any? && shell_files.none? @@ -164,17 +164,24 @@ module Homebrew end end - def run_shellcheck(files, output_type) + def run_shellcheck(files, output_type, fix: false) files = shell_scripts if files.blank? - args = ["--shell=bash", "--enable=all", "--external-sources", "--source-path=#{HOMEBREW_LIBRARY}", "--", *files] + files = files.map(&:realpath) + + args = ["--shell=bash", "--enable=all", "--external-sources", "--source-path=#{HOMEBREW_LIBRARY}", "--"] + + if fix + patch = system_command shellcheck, args: ["--format=diff", *args, *files] + system_command "patch", args: ["-d", "/", "-p0"], input: patch.stdout + end case output_type when :print - system shellcheck, "--format=tty", *args + system shellcheck, "--format=tty", *args, *files $CHILD_STATUS.success? when :json - result = system_command shellcheck, args: ["--format=json", *args] + result = system_command shellcheck, args: ["--format=json", *args, *files] json = json_result!(result) # Convert to same format as RuboCop offenses. From 00ebd8581ff3cdb1ac547d3a97cda3cb3a0aa012 Mon Sep 17 00:00:00 2001 From: XuehaiPan Date: Sun, 7 Nov 2021 21:21:42 +0800 Subject: [PATCH 2/5] style: apply shellcheck autofix --- Library/Homebrew/cmd/update.sh | 2 +- Library/Homebrew/completions/bash.erb | 2 +- bin/brew | 1 + completions/bash/brew | 2 +- 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Library/Homebrew/cmd/update.sh b/Library/Homebrew/cmd/update.sh index 0f9b6355f..8dd610a60 100644 --- a/Library/Homebrew/cmd/update.sh +++ b/Library/Homebrew/cmd/update.sh @@ -657,7 +657,7 @@ EOS if [[ "${UPSTREAM_SHA_HTTP_CODE}" == "404" ]] then - TAP="${DIR#${HOMEBREW_LIBRARY}/Taps/}" + TAP="${DIR#"${HOMEBREW_LIBRARY}"/Taps/}" echo "${TAP} does not exist! Run \`brew untap ${TAP}\` to remove it." >>"${update_failed_file}" else echo "Fetching ${DIR} failed!" >>"${update_failed_file}" diff --git a/Library/Homebrew/completions/bash.erb b/Library/Homebrew/completions/bash.erb index 71a72e662..b9f52a2cd 100644 --- a/Library/Homebrew/completions/bash.erb +++ b/Library/Homebrew/completions/bash.erb @@ -105,7 +105,7 @@ __brew_complete_tapped() { for dir in "${taplib}"/*/* do [[ -d "${dir}" ]] || continue - dir="${dir#${taplib}/}" + dir="${dir#"${taplib}"/}" dir="${dir/homebrew-/}" taps="${taps} ${dir}" done diff --git a/bin/brew b/bin/brew index ac635ce4f..1c8fe3c6e 100755 --- a/bin/brew +++ b/bin/brew @@ -2,6 +2,7 @@ # Fail fast with concise message when not using bash # Single brackets is needed here for POSIX compatibility +# shellcheck disable=SC2292 if [ -z "${BASH_VERSION:-}" ] then echo "Error: Bash is required to run brew." >&2 diff --git a/completions/bash/brew b/completions/bash/brew index 55b139d0a..561175ff3 100644 --- a/completions/bash/brew +++ b/completions/bash/brew @@ -92,7 +92,7 @@ __brew_complete_tapped() { for dir in "${taplib}"/*/* do [[ -d "${dir}" ]] || continue - dir="${dir#${taplib}/}" + dir="${dir#"${taplib}"/}" dir="${dir/homebrew-/}" taps="${taps} ${dir}" done From 67c619d4f28b27ba9e33fadc149123fe237df4a1 Mon Sep 17 00:00:00 2001 From: XuehaiPan Date: Mon, 8 Nov 2021 20:10:30 +0800 Subject: [PATCH 3/5] style: use popen_write to apply patches --- Library/Homebrew/style.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/style.rb b/Library/Homebrew/style.rb index fdef92b30..3375ba311 100644 --- a/Library/Homebrew/style.rb +++ b/Library/Homebrew/style.rb @@ -181,8 +181,9 @@ module Homebrew ] if fix - patch = system_command shellcheck, args: ["--format=diff", *args] - system_command "patch", args: ["-d", "/", "-p0"], input: patch.stdout + patches = system_command(shellcheck, args: ["--format=diff", *args]).stdout + patch_command = %w[patch -g 0 -f -d / -p0] + Utils.popen_write(*patch_command) { |p| p.write(patches) } end case output_type From b86fdfeb09e55678f5d35a738f55d7bc04dbfd54 Mon Sep 17 00:00:00 2001 From: XuehaiPan Date: Mon, 8 Nov 2021 22:33:53 +0800 Subject: [PATCH 4/5] style: use long options for `patch` command --- Library/Homebrew/style.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/style.rb b/Library/Homebrew/style.rb index 3375ba311..486e44cdd 100644 --- a/Library/Homebrew/style.rb +++ b/Library/Homebrew/style.rb @@ -167,7 +167,7 @@ module Homebrew def run_shellcheck(files, output_type, fix: false) files = shell_scripts if files.blank? - files = files.map(&:realpath) + files = files.map(&:realpath) # use absolute file paths args = [ "--shell=bash", @@ -181,8 +181,13 @@ module Homebrew ] if fix + # patch options: + # --get=0 : suppress environment variable `PATCH_GET`, ignore RCS, ClearCase, Perforce, and SCCS + # --force : we know what we are doing, force apply patches + # --directory=/ : change to root directory, since we use absolute file paths + # --strip=0 : do not strip path prefixes, since we are at root directory + patch_command = %w[patch --get=0 --force --directory=/ --strip=0] patches = system_command(shellcheck, args: ["--format=diff", *args]).stdout - patch_command = %w[patch -g 0 -f -d / -p0] Utils.popen_write(*patch_command) { |p| p.write(patches) } end From c1d2f8a2c672ef889f32016c762a24c7d5ee3f16 Mon Sep 17 00:00:00 2001 From: Xuehai Pan Date: Mon, 8 Nov 2021 22:50:20 +0800 Subject: [PATCH 5/5] style: catch failures while applying patches Co-authored-by: Mike McQuaid --- Library/Homebrew/style.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/style.rb b/Library/Homebrew/style.rb index 486e44cdd..9ed0cda22 100644 --- a/Library/Homebrew/style.rb +++ b/Library/Homebrew/style.rb @@ -188,7 +188,7 @@ module Homebrew # --strip=0 : do not strip path prefixes, since we are at root directory patch_command = %w[patch --get=0 --force --directory=/ --strip=0] patches = system_command(shellcheck, args: ["--format=diff", *args]).stdout - Utils.popen_write(*patch_command) { |p| p.write(patches) } + Utils.safe_popen_write(*patch_command) { |p| p.write(patches) } end case output_type