Merge pull request #12391 from XuehaiPan/shellcheck-autofix

style: implement shellcheck autofix in `brew style --fix`
This commit is contained in:
Mike McQuaid 2021-11-08 15:18:59 +00:00 committed by GitHub
commit ff5e68ad20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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,9 +164,11 @@ module Homebrew
end
end
def run_shellcheck(files, output_type)
def run_shellcheck(files, output_type, fix: false)
files = shell_scripts if files.blank?
files = files.map(&:realpath) # use absolute file paths
args = [
"--shell=bash",
"--enable=all",
@ -178,6 +180,17 @@ module Homebrew
*files,
]
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
Utils.safe_popen_write(*patch_command) { |p| p.write(patches) }
end
case output_type
when :print
system shellcheck, "--format=tty", *args