From 621114488e937db445196882241856842e00600c Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Fri, 22 Aug 2025 14:33:20 +0100 Subject: [PATCH] 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. --- Library/Homebrew/dev-cmd/style.rb | 28 ++++++++++++++++++- .../rbi/dsl/homebrew/dev_cmd/style_cmd.rbi | 3 ++ completions/bash/brew | 1 + completions/fish/brew.fish | 1 + completions/zsh/_brew | 1 + docs/Manpage.md | 4 +++ manpages/brew.1 | 3 ++ 7 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/dev-cmd/style.rb b/Library/Homebrew/dev-cmd/style.rb index f9db3f42f1..11df1e4f7f 100644 --- a/Library/Homebrew/dev-cmd/style.rb +++ b/Library/Homebrew/dev-cmd/style.rb @@ -24,6 +24,8 @@ module Homebrew hidden: true switch "--reset-cache", description: "Reset the RuboCop cache." + switch "--changed", + description: "Check files that were changed from the `main` branch." switch "--formula", "--formulae", description: "Treat all named arguments as formulae." switch "--cask", "--casks", @@ -45,12 +47,23 @@ module Homebrew def run 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 else args.named.to_paths end + if target.blank? && args.changed? + opoo "No style checks are available for the changed files!" + return + end + only_cops = args.only_cops except_cops = args.except_cops @@ -70,6 +83,19 @@ module Homebrew Homebrew.failed = !Style.check_style_and_print(target, **options) 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 diff --git a/Library/Homebrew/sorbet/rbi/dsl/homebrew/dev_cmd/style_cmd.rbi b/Library/Homebrew/sorbet/rbi/dsl/homebrew/dev_cmd/style_cmd.rbi index 14eaec8d46..90ec98403c 100644 --- a/Library/Homebrew/sorbet/rbi/dsl/homebrew/dev_cmd/style_cmd.rbi +++ b/Library/Homebrew/sorbet/rbi/dsl/homebrew/dev_cmd/style_cmd.rbi @@ -17,6 +17,9 @@ class Homebrew::DevCmd::StyleCmd::Args < Homebrew::CLI::Args sig { returns(T::Boolean) } def casks?; end + sig { returns(T::Boolean) } + def changed?; end + sig { returns(T::Boolean) } def display_cop_names?; end diff --git a/completions/bash/brew b/completions/bash/brew index 9aad59b04d..0d895ff0ee 100644 --- a/completions/bash/brew +++ b/completions/bash/brew @@ -2429,6 +2429,7 @@ _brew_style() { -*) __brewcomp " --cask + --changed --debug --except-cops --fix diff --git a/completions/fish/brew.fish b/completions/fish/brew.fish index 2fbe4652d0..b2a400c092 100644 --- a/completions/fish/brew.fish +++ b/completions/fish/brew.fish @@ -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_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 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' diff --git a/completions/zsh/_brew b/completions/zsh/_brew index ce88bff9d3..9c8697fb54 100644 --- a/completions/zsh/_brew +++ b/completions/zsh/_brew @@ -1960,6 +1960,7 @@ _brew_shellenv() { # brew style _brew_style() { _arguments \ + '--changed[Check files that were changed from the `main` branch]' \ '--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]' \ '--fix[Fix style violations automatically using RuboCop'\''s auto-correct feature]' \ diff --git a/docs/Manpage.md b/docs/Manpage.md index 12bd072da0..103eb92760 100644 --- a/docs/Manpage.md +++ b/docs/Manpage.md @@ -3121,6 +3121,10 @@ core code and all formulae. : Reset the RuboCop cache. +`--changed` + +: Check files that were changed from the `main` branch. + `--formula` : Treat all named arguments as formulae. diff --git a/manpages/brew.1 b/manpages/brew.1 index 3032c85dfd..086875ea55 100644 --- a/manpages/brew.1 +++ b/manpages/brew.1 @@ -1977,6 +1977,9 @@ Fix style violations automatically using RuboCop\[u2019]s auto\-correct feature\ \fB\-\-reset\-cache\fP Reset the RuboCop cache\. .TP +\fB\-\-changed\fP +Check files that were changed from the \fBmain\fP branch\. +.TP \fB\-\-formula\fP Treat all named arguments as formulae\. .TP