Skip to content

Commit 4dc95b5

Browse files
committed
Fetch ask upgrades together
- Avoid splitting `brew upgrade --ask` downloads by package type - Reuse the shared prefetch queue after confirmation so casks and formulae download together before the existing install order runs - Keep no-op ask runs from creating an empty shared queue
1 parent 7c63ed4 commit 4dc95b5

2 files changed

Lines changed: 79 additions & 4 deletions

File tree

Library/Homebrew/cmd/upgrade.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ def run
208208

209209
formulae_prefetched = T.let(false, T::Boolean)
210210
prefetched_casks = T.let(false, T::Boolean)
211+
ask_upgrade_planned = T.let(false, T::Boolean)
211212
shared_download_queue = T.let(nil, T.nilable(Homebrew::DownloadQueue))
212213
if args.ask?
213214
unless only_upgrade_casks
@@ -239,10 +240,11 @@ def run
239240
)
240241
Install.ask(action: "upgrade")
241242
end
243+
ask_upgrade_planned = final_upgrade_summary.version_changes.present?
242244
@final_upgrade_summary = FinalUpgradeSummary.new
243245
end
244246

245-
if !args.dry_run? && !args.ask? && !only_upgrade_formulae && !only_upgrade_casks
247+
if !args.dry_run? && (!args.ask? || ask_upgrade_planned) && !only_upgrade_formulae && !only_upgrade_casks
246248
shared_download_queue = Homebrew::DownloadQueue.new(pour: true)
247249
begin
248250
formulae_prefetched = upgrade_outdated_formulae!(
@@ -708,7 +710,6 @@ def prefetch_outdated_casks!(casks, download_queue:, prefetch_names: nil,
708710
prefetch_upgrades: nil,
709711
show_downloads_heading: true)
710712
return false if args.formula?
711-
return false if args.ask?
712713

713714
casks = minimum_version_casks(casks, quiet: true)
714715
return false if minimum_version.present? && casks.empty?

Library/Homebrew/test/cmd/upgrade_spec.rb

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ class #{Formulary.class_s(name)} < Formula
226226
# upgrades with asking for user prompts
227227
it "prints formula and cask ask plans before upgrading" do
228228
cmd = klass.new(["--ask"])
229+
download_queue = instance_double(Homebrew::DownloadQueue, fetch: nil, fetch_failed: false, shutdown: nil)
229230

230231
expect(cmd).to receive(:upgrade_outdated_formulae!)
231232
.with([], dry_run: true, show_upgrade_summary: false)
@@ -241,12 +242,36 @@ class #{Formulary.class_s(name)} < Formula
241242
expect(cmd).to receive(:show_final_upgrade_summary).with(dry_run: true).ordered
242243
expect(Homebrew::Install).to receive(:ask).with(action: "upgrade")
243244
.ordered
245+
expect(Homebrew::DownloadQueue).to receive(:new).ordered.and_return(download_queue)
244246
expect(cmd).to receive(:upgrade_outdated_formulae!)
245-
.with([], use_prefetched: false, show_upgrade_summary: false)
247+
.with(
248+
[],
249+
prefetch_only: true,
250+
download_queue:,
251+
prefetch_names: [],
252+
prefetch_upgrades: [],
253+
show_upgrade_summary: false,
254+
show_downloads_heading: false,
255+
)
256+
.ordered
257+
.and_return(true)
258+
expect(cmd).to receive(:prefetch_outdated_casks!)
259+
.with(
260+
[],
261+
download_queue:,
262+
prefetch_names: [],
263+
prefetch_upgrades: [],
264+
show_downloads_heading: false,
265+
)
266+
.ordered
267+
.and_return(true)
268+
expect(download_queue).to receive(:fetch).ordered
269+
expect(cmd).to receive(:upgrade_outdated_formulae!)
270+
.with([], use_prefetched: true, show_upgrade_summary: false)
246271
.ordered
247272
.and_return(true)
248273
expect(cmd).to receive(:upgrade_outdated_casks!)
249-
.with([], skip_prefetch: false, show_upgrade_summary: false, download_queue: nil)
274+
.with([], skip_prefetch: true, show_upgrade_summary: false, download_queue: nil)
250275
.ordered
251276
.and_return(true)
252277
allow(Homebrew::Cleanup).to receive(:periodic_clean!)
@@ -481,6 +506,55 @@ class #{Formulary.class_s(name)} < Formula
481506
EOS
482507
end
483508

509+
it "asks before fetching formulae and casks in the same download queue" do
510+
cmd = klass.new(["--ask"])
511+
download_queue = instance_double(Homebrew::DownloadQueue, fetch: nil, fetch_failed: false, shutdown: nil)
512+
cask = instance_double(
513+
Cask::Cask,
514+
artifacts: [],
515+
full_name: "codex",
516+
installed_version: "0.117.0",
517+
version: "0.118.0",
518+
)
519+
installer = instance_double(Cask::Installer, enqueue_downloads: nil, source_download_requires_pre_fetch?: false)
520+
521+
allow(cmd).to receive(:upgrade_outdated_formulae!) do |_, dry_run: false, prefetch_only: false,
522+
use_prefetched: false, prefetch_names: nil,
523+
prefetch_upgrades: nil, **|
524+
if dry_run
525+
cmd.send(:final_upgrade_summary).version_changes << "deno 2.7.10 -> 2.7.11"
526+
elsif prefetch_only
527+
prefetch_names&.replace(["deno"])
528+
prefetch_upgrades&.replace(["deno 2.7.10 -> 2.7.11"])
529+
else
530+
expect(use_prefetched).to be(true)
531+
end
532+
533+
true
534+
end
535+
allow(Cask::Upgrade).to receive(:outdated_casks).and_return([cask])
536+
allow(Cask::Installer).to receive(:new).and_return(installer)
537+
allow(Cask::Upgrade).to receive(:upgrade_casks!) do |*_, **kwargs|
538+
if kwargs[:dry_run]
539+
kwargs[:summary_upgrades] << "codex 0.117.0 -> 0.118.0"
540+
else
541+
expect(kwargs[:skip_prefetch]).to be(true)
542+
end
543+
544+
true
545+
end
546+
allow(Homebrew::Cleanup).to receive(:periodic_clean!)
547+
allow(Homebrew::Reinstall).to receive(:reinstall_pkgconf_if_needed!)
548+
allow(Homebrew.messages).to receive(:display_messages)
549+
550+
expect(Homebrew::Install).to receive(:ask).with(action: "upgrade").ordered
551+
expect(Homebrew::DownloadQueue).to receive(:new).ordered.and_return(download_queue)
552+
expect(Homebrew::Install).to receive(:enqueue_cask_installers).ordered
553+
expect(download_queue).to receive(:fetch).ordered
554+
555+
cmd.run
556+
end
557+
484558
it "prefetches language cask files before fetching combined downloads" do
485559
cmd = klass.new([])
486560
download_queue = instance_double(Homebrew::DownloadQueue, fetch_failed: false, shutdown: nil)

0 commit comments

Comments
 (0)