Add brew style --changed

This emulates `brew tests --changed` but for style checks. Should make
things a bit quicker for the case of wanting to quickly fix up
local files.
This commit is contained in:
Mike McQuaid 2025-08-22 14:33:20 +01:00
parent 7878c73b85
commit 621114488e
No known key found for this signature in database
7 changed files with 40 additions and 1 deletions

View File

@ -24,6 +24,8 @@ module Homebrew
hidden: true hidden: true
switch "--reset-cache", switch "--reset-cache",
description: "Reset the RuboCop cache." description: "Reset the RuboCop cache."
switch "--changed",
description: "Check files that were changed from the `main` branch."
switch "--formula", "--formulae", switch "--formula", "--formulae",
description: "Treat all named arguments as formulae." description: "Treat all named arguments as formulae."
switch "--cask", "--casks", switch "--cask", "--casks",
@ -45,12 +47,23 @@ module Homebrew
def run def run
Homebrew.install_bundler_gems!(groups: ["style"]) Homebrew.install_bundler_gems!(groups: ["style"])
target = if args.no_named? if args.changed? && !args.no_named?
raise UsageError, "`--changed` and named arguments are mutually exclusive!"
end
target = if args.changed?
changed_ruby_or_shell_files
elsif args.no_named?
nil nil
else else
args.named.to_paths args.named.to_paths
end end
if target.blank? && args.changed?
opoo "No style checks are available for the changed files!"
return
end
only_cops = args.only_cops only_cops = args.only_cops
except_cops = args.except_cops except_cops = args.except_cops
@ -70,6 +83,19 @@ module Homebrew
Homebrew.failed = !Style.check_style_and_print(target, **options) Homebrew.failed = !Style.check_style_and_print(target, **options)
end end
sig { returns(T::Array[String]) }
def changed_ruby_or_shell_files
changed_files = Utils.popen_read("git", "diff", "--name-only", "main")
raise UsageError, "No files have been changed from the `main` branch!" if changed_files.blank?
changed_files.split("\n").filter_map do |file|
next if !file.end_with?(".rb", ".sh", ".yml", ".rbi") && file != "bin/brew"
Pathname(file)
end.select(&:exist?)
end
end end
end end
end end

View File

@ -17,6 +17,9 @@ class Homebrew::DevCmd::StyleCmd::Args < Homebrew::CLI::Args
sig { returns(T::Boolean) } sig { returns(T::Boolean) }
def casks?; end def casks?; end
sig { returns(T::Boolean) }
def changed?; end
sig { returns(T::Boolean) } sig { returns(T::Boolean) }
def display_cop_names?; end def display_cop_names?; end

View File

@ -2429,6 +2429,7 @@ _brew_style() {
-*) -*)
__brewcomp " __brewcomp "
--cask --cask
--changed
--debug --debug
--except-cops --except-cops
--fix --fix

View File

@ -1598,6 +1598,7 @@ __fish_brew_complete_arg 'shellenv' -l verbose -d 'Make some output more verbose
__fish_brew_complete_cmd 'style' 'Check formulae or files for conformance to Homebrew style guidelines' __fish_brew_complete_cmd 'style' 'Check formulae or files for conformance to Homebrew style guidelines'
__fish_brew_complete_arg 'style' -l cask -d 'Treat all named arguments as casks' __fish_brew_complete_arg 'style' -l cask -d 'Treat all named arguments as casks'
__fish_brew_complete_arg 'style' -l changed -d 'Check files that were changed from the `main` branch'
__fish_brew_complete_arg 'style' -l debug -d 'Display any debugging information' __fish_brew_complete_arg 'style' -l debug -d 'Display any debugging information'
__fish_brew_complete_arg 'style' -l except-cops -d 'Specify a comma-separated cops list to skip checking for violations of the listed RuboCop cops' __fish_brew_complete_arg 'style' -l except-cops -d 'Specify a comma-separated cops list to skip checking for violations of the listed RuboCop cops'
__fish_brew_complete_arg 'style' -l fix -d 'Fix style violations automatically using RuboCop\'s auto-correct feature' __fish_brew_complete_arg 'style' -l fix -d 'Fix style violations automatically using RuboCop\'s auto-correct feature'

View File

@ -1960,6 +1960,7 @@ _brew_shellenv() {
# brew style # brew style
_brew_style() { _brew_style() {
_arguments \ _arguments \
'--changed[Check files that were changed from the `main` branch]' \
'--debug[Display any debugging information]' \ '--debug[Display any debugging information]' \
'(--only-cops)--except-cops[Specify a comma-separated cops list to skip checking for violations of the listed RuboCop cops]' \ '(--only-cops)--except-cops[Specify a comma-separated cops list to skip checking for violations of the listed RuboCop cops]' \
'--fix[Fix style violations automatically using RuboCop'\''s auto-correct feature]' \ '--fix[Fix style violations automatically using RuboCop'\''s auto-correct feature]' \

View File

@ -3121,6 +3121,10 @@ core code and all formulae.
: Reset the RuboCop cache. : Reset the RuboCop cache.
`--changed`
: Check files that were changed from the `main` branch.
`--formula` `--formula`
: Treat all named arguments as formulae. : Treat all named arguments as formulae.

View File

@ -1977,6 +1977,9 @@ Fix style violations automatically using RuboCop\[u2019]s auto\-correct feature\
\fB\-\-reset\-cache\fP \fB\-\-reset\-cache\fP
Reset the RuboCop cache\. Reset the RuboCop cache\.
.TP .TP
\fB\-\-changed\fP
Check files that were changed from the \fBmain\fP branch\.
.TP
\fB\-\-formula\fP \fB\-\-formula\fP
Treat all named arguments as formulae\. Treat all named arguments as formulae\.
.TP .TP