| 
									
										
										
										
											2024-07-01 13:14:47 +01:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  | require "abstract_command" | 
					
						
							| 
									
										
										
										
											2024-03-21 21:31:25 -07:00
										 |  |  | require "fileutils" | 
					
						
							| 
									
										
										
										
											2016-03-17 05:58:14 +00:00
										 |  |  | require "formula" | 
					
						
							| 
									
										
										
										
											2024-07-14 23:17:45 -04:00
										 |  |  | require "utils/inreplace" | 
					
						
							| 
									
										
										
										
											2020-07-27 10:39:09 -04:00
										 |  |  | require "utils/pypi" | 
					
						
							| 
									
										
										
										
											2020-09-04 14:13:43 -07:00
										 |  |  | require "utils/tar" | 
					
						
							| 
									
										
										
										
											2016-03-17 05:58:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |   module DevCmd | 
					
						
							|  |  |  |     class BumpFormulaPr < AbstractCommand | 
					
						
							|  |  |  |       cmd_args do | 
					
						
							|  |  |  |         description <<~EOS | 
					
						
							|  |  |  |           Create a pull request to update <formula> with a new URL or a new tag. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           If a <URL> is specified, the <SHA-256> checksum of the new download should also | 
					
						
							|  |  |  |           be specified. A best effort to determine the <SHA-256> will be made if not supplied | 
					
						
							|  |  |  |           by the user. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           If a <tag> is specified, the Git commit <revision> corresponding to that tag | 
					
						
							|  |  |  |           should also be specified. A best effort to determine the <revision> will be made | 
					
						
							|  |  |  |           if the value is not supplied by the user. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           If a <version> is specified, a best effort to determine the <URL> and <SHA-256> or | 
					
						
							|  |  |  |           the <tag> and <revision> will be made if both values are not supplied by the user. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           *Note:* this command cannot be used to transition a formula from a | 
					
						
							|  |  |  |           URL-and-SHA-256 style specification into a tag-and-revision style specification, | 
					
						
							|  |  |  |           nor vice versa. It must use whichever style specification the formula already uses. | 
					
						
							|  |  |  |         EOS | 
					
						
							|  |  |  |         switch "-n", "--dry-run", | 
					
						
							|  |  |  |                description: "Print what would be done rather than doing it." | 
					
						
							|  |  |  |         switch "--write-only", | 
					
						
							|  |  |  |                description: "Make the expected file modifications without taking any Git actions." | 
					
						
							|  |  |  |         switch "--commit", | 
					
						
							|  |  |  |                depends_on:  "--write-only", | 
					
						
							|  |  |  |                description: "When passed with `--write-only`, generate a new commit after writing changes " \ | 
					
						
							|  |  |  |                             "to the formula file." | 
					
						
							|  |  |  |         switch "--no-audit", | 
					
						
							|  |  |  |                description: "Don't run `brew audit` before opening the PR." | 
					
						
							|  |  |  |         switch "--strict", | 
					
						
							|  |  |  |                description: "Run `brew audit --strict` before opening the PR." | 
					
						
							|  |  |  |         switch "--online", | 
					
						
							|  |  |  |                description: "Run `brew audit --online` before opening the PR." | 
					
						
							|  |  |  |         switch "--no-browse", | 
					
						
							|  |  |  |                description: "Print the pull request URL instead of opening in a browser." | 
					
						
							|  |  |  |         switch "--no-fork", | 
					
						
							|  |  |  |                description: "Don't try to fork the repository." | 
					
						
							|  |  |  |         comma_array "--mirror", | 
					
						
							|  |  |  |                     description: "Use the specified <URL> as a mirror URL. If <URL> is a comma-separated list " \ | 
					
						
							|  |  |  |                                  "of URLs, multiple mirrors will be added." | 
					
						
							|  |  |  |         flag   "--fork-org=", | 
					
						
							|  |  |  |                description: "Use the specified GitHub organization for forking." | 
					
						
							|  |  |  |         flag   "--version=", | 
					
						
							|  |  |  |                description: "Use the specified <version> to override the value parsed from the URL or tag. Note " \ | 
					
						
							|  |  |  |                             "that `--version=0` can be used to delete an existing version override from a " \ | 
					
						
							|  |  |  |                             "formula if it has become redundant." | 
					
						
							|  |  |  |         flag   "--message=", | 
					
						
							|  |  |  |                description: "Prepend <message> to the default pull request message." | 
					
						
							|  |  |  |         flag   "--url=", | 
					
						
							|  |  |  |                description: "Specify the <URL> for the new download. If a <URL> is specified, the <SHA-256> " \ | 
					
						
							|  |  |  |                             "checksum of the new download should also be specified." | 
					
						
							|  |  |  |         flag   "--sha256=", | 
					
						
							|  |  |  |                depends_on:  "--url=", | 
					
						
							|  |  |  |                description: "Specify the <SHA-256> checksum of the new download." | 
					
						
							|  |  |  |         flag   "--tag=", | 
					
						
							|  |  |  |                description: "Specify the new git commit <tag> for the formula." | 
					
						
							|  |  |  |         flag   "--revision=", | 
					
						
							|  |  |  |                description: "Specify the new commit <revision> corresponding to the specified git <tag> " \ | 
					
						
							|  |  |  |                             "or specified <version>." | 
					
						
							|  |  |  |         switch "-f", "--force", | 
					
						
							|  |  |  |                description: "Remove all mirrors if `--mirror` was not specified." | 
					
						
							|  |  |  |         switch "--install-dependencies", | 
					
						
							|  |  |  |                description: "Install missing dependencies required to update resources." | 
					
						
							|  |  |  |         flag   "--python-package-name=", | 
					
						
							|  |  |  |                description: "Use the specified <package-name> when finding Python resources for <formula>. " \ | 
					
						
							|  |  |  |                             "If no package name is specified, it will be inferred from the formula's stable URL." | 
					
						
							|  |  |  |         comma_array "--python-extra-packages=", | 
					
						
							|  |  |  |                     description: "Include these additional Python packages when finding resources." | 
					
						
							|  |  |  |         comma_array "--python-exclude-packages=", | 
					
						
							|  |  |  |                     description: "Exclude these Python packages when finding resources." | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |         comma_array "--bump-synced=", | 
					
						
							|  |  |  |                     hidden: true | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |         conflicts "--dry-run", "--write-only" | 
					
						
							|  |  |  |         conflicts "--no-audit", "--strict" | 
					
						
							|  |  |  |         conflicts "--no-audit", "--online" | 
					
						
							|  |  |  |         conflicts "--url", "--tag" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |         named_args :formula, max: 1, without_api: true | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-07-30 18:25:38 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |       sig { override.void } | 
					
						
							|  |  |  |       def run | 
					
						
							|  |  |  |         if args.revision.present? && args.tag.nil? && args.version.nil? | 
					
						
							|  |  |  |           raise UsageError, "`--revision` must be passed with either `--tag` or `--version`!" | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2017-02-12 09:22:26 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |         # As this command is simplifying user-run commands then let's just use a | 
					
						
							|  |  |  |         # user path, too. | 
					
						
							|  |  |  |         ENV["PATH"] = PATH.new(ORIGINAL_PATHS).to_s | 
					
						
							| 
									
										
										
										
											2020-12-08 21:54:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |         # Use the user's browser, too. | 
					
						
							|  |  |  |         ENV["BROWSER"] = Homebrew::EnvConfig.browser | 
					
						
							| 
									
										
										
										
											2017-10-28 21:46:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |         formula = args.named.to_formulae.first | 
					
						
							|  |  |  |         raise FormulaUnspecifiedError if formula.blank? | 
					
						
							| 
									
										
										
										
											2017-11-07 07:48:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |         odie "This formula is disabled!" if formula.disabled? | 
					
						
							|  |  |  |         odie "This formula is deprecated and does not build!" if formula.deprecation_reason == :does_not_build | 
					
						
							|  |  |  |         tap = formula.tap | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |         odie "This formula is not in a tap!" if tap.blank? | 
					
						
							|  |  |  |         odie "This formula's tap is not a Git repository!" unless tap.git? | 
					
						
							| 
									
										
										
										
											2016-03-17 05:58:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |         odie <<~EOS unless tap.allow_bump?(formula.name) | 
					
						
							|  |  |  |           Whoops, the #{formula.name} formula has its version update | 
					
						
							| 
									
										
										
										
											2024-05-31 10:23:13 +01:00
										 |  |  |           pull requests automatically opened by BrewTestBot every ~3 hours! | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |           We'd still love your contributions, though, so try another one | 
					
						
							| 
									
										
										
										
											2025-04-26 16:43:48 +02:00
										 |  |  |           that is excluded from autobump list (i.e. it has 'no_autobump!' | 
					
						
							|  |  |  |           method or 'livecheck' block with 'skip'.) | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |         EOS | 
					
						
							| 
									
										
										
										
											2020-09-15 12:29:11 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-23 16:47:06 +01:00
										 |  |  |         if !args.write_only? && GitHub.too_many_open_prs?(tap) | 
					
						
							|  |  |  |           odie "You have too many PRs open: close or merge some first!" | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2024-03-28 11:56:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |         formula_spec = formula.stable | 
					
						
							|  |  |  |         odie "#{formula}: no stable specification found!" if formula_spec.blank? | 
					
						
							| 
									
										
										
										
											2024-03-01 09:15:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |         # This will be run by `brew audit` later so run it first to not start | 
					
						
							|  |  |  |         # spamming during normal output. | 
					
						
							|  |  |  |         Homebrew.install_bundler_gems!(groups: ["audit", "style"]) unless args.no_audit? | 
					
						
							| 
									
										
										
										
											2021-01-10 09:47:20 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |         tap_remote_repo = T.must(tap.remote_repository) | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |         remote = "origin" | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |         remote_branch = tap.git_repository.origin_branch_name | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |         previous_branch = "-" | 
					
						
							| 
									
										
										
										
											2021-04-30 12:00:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-23 16:47:06 +01:00
										 |  |  |         check_pull_requests(formula, tap_remote_repo, state: "open") unless args.write_only? | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |         all_formulae = [] | 
					
						
							|  |  |  |         if args.bump_synced.present? | 
					
						
							|  |  |  |           Array(args.bump_synced).each do |formula_name| | 
					
						
							|  |  |  |             all_formulae << formula_name | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           all_formulae << args.named.first.to_s | 
					
						
							| 
									
										
										
										
											2025-04-22 12:33:59 +01:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |         return if all_formulae.empty? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         commits = all_formulae.filter_map do |formula_name| | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |           commit_formula = Formula[formula_name] | 
					
						
							|  |  |  |           raise FormulaUnspecifiedError if commit_formula.blank? | 
					
						
							| 
									
										
										
										
											2020-08-13 16:05:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |           commit_formula_spec = commit_formula.stable | 
					
						
							|  |  |  |           odie "#{commit_formula}: no stable specification found!" if commit_formula_spec.blank? | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |           formula_pr_message = "" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           new_url = args.url | 
					
						
							|  |  |  |           new_version = args.version | 
					
						
							| 
									
										
										
										
											2025-05-19 00:04:16 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |           check_new_version(commit_formula, tap_remote_repo, version: new_version) if new_version.present? | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |           opoo "This formula has patches that may be resolved upstream." if commit_formula.patchlist.present? | 
					
						
							|  |  |  |           if commit_formula.resources.any? { |resource| !resource.name.start_with?("homebrew-") } | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |             opoo "This formula has resources that may need to be updated." | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |           old_mirrors = commit_formula_spec.mirrors | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |           new_mirrors ||= args.mirror | 
					
						
							|  |  |  |           if new_url.present? && (new_mirror = determine_mirror(new_url)) | 
					
						
							|  |  |  |             new_mirrors ||= [new_mirror] | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |             check_for_mirrors(commit_formula.name, old_mirrors, new_mirrors) | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |           old_hash = commit_formula_spec.checksum&.hexdigest | 
					
						
							| 
									
										
										
										
											2025-05-19 00:04:16 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |           new_hash = args.sha256 | 
					
						
							|  |  |  |           new_tag = args.tag | 
					
						
							|  |  |  |           new_revision = args.revision | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |           old_url = T.must(commit_formula_spec.url) | 
					
						
							|  |  |  |           old_tag = commit_formula_spec.specs[:tag] | 
					
						
							|  |  |  |           old_formula_version = formula_version(commit_formula) | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |           old_version = old_formula_version.to_s | 
					
						
							|  |  |  |           forced_version = new_version.present? | 
					
						
							|  |  |  |           new_url_hash = if new_url.present? && new_hash.present? | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |             check_new_version(commit_formula, tap_remote_repo, url: new_url) if new_version.blank? | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |             true | 
					
						
							|  |  |  |           elsif new_tag.present? && new_revision.present? | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |             check_new_version(commit_formula, tap_remote_repo, url: old_url, tag: new_tag) if new_version.blank? | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |             false | 
					
						
							|  |  |  |           elsif old_hash.blank? | 
					
						
							|  |  |  |             if new_tag.blank? && new_version.blank? && new_revision.blank? | 
					
						
							|  |  |  |               raise UsageError, "#{formula}: no `--tag` or `--version` argument specified!" | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if old_tag.present? | 
					
						
							|  |  |  |               new_tag ||= old_tag.gsub(old_version, new_version) | 
					
						
							|  |  |  |               if new_tag == old_tag | 
					
						
							|  |  |  |                 odie <<~EOS | 
					
						
							|  |  |  |                   You need to bump this formula manually since the new tag | 
					
						
							|  |  |  |                   and old tag are both #{new_tag}. | 
					
						
							|  |  |  |                 EOS | 
					
						
							| 
									
										
										
										
											2025-04-22 12:33:59 +01:00
										 |  |  |               end | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |               check_new_version(commit_formula, tap_remote_repo, url: old_url, tag: new_tag) if new_version.blank? | 
					
						
							|  |  |  |               resource_path, forced_version = fetch_resource_and_forced_version(commit_formula, new_version, old_url, | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |                                                                                 tag: new_tag) | 
					
						
							|  |  |  |               new_revision = Utils.popen_read("git", "-C", resource_path.to_s, "rev-parse", "-q", "--verify", "HEAD") | 
					
						
							|  |  |  |               new_revision = new_revision.strip | 
					
						
							|  |  |  |             elsif new_revision.blank? | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |               odie "#{commit_formula}: the current URL requires specifying a `--revision=` argument." | 
					
						
							| 
									
										
										
										
											2025-04-22 12:33:59 +01:00
										 |  |  |             end | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |             false | 
					
						
							|  |  |  |           elsif new_url.blank? && new_version.blank? | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |             raise UsageError, "#{commit_formula}: no `--url` or `--version` argument specified!" | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |           else | 
					
						
							| 
									
										
										
										
											2025-05-19 00:04:16 +10:00
										 |  |  |             new_url ||= PyPI.update_pypi_url(old_url, new_version) if new_version.present? | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-19 00:04:16 +10:00
										 |  |  |             if new_url.blank? && new_version.present? | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |               new_url = update_url(old_url, old_version, new_version) | 
					
						
							|  |  |  |               if new_mirrors.blank? && old_mirrors.present? | 
					
						
							|  |  |  |                 new_mirrors = old_mirrors.map do |old_mirror| | 
					
						
							|  |  |  |                   update_url(old_mirror, old_version, new_version) | 
					
						
							|  |  |  |                 end | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |             if new_url == old_url | 
					
						
							|  |  |  |               odie <<~EOS | 
					
						
							|  |  |  |                 You need to bump this formula manually since the new URL | 
					
						
							|  |  |  |                 and old URL are both: | 
					
						
							|  |  |  |                   #{new_url} | 
					
						
							|  |  |  |               EOS | 
					
						
							|  |  |  |             end | 
					
						
							| 
									
										
										
										
											2025-05-19 00:04:16 +10:00
										 |  |  |             if new_url.blank? | 
					
						
							|  |  |  |               odie "There was an issue generating the updated url, you may need to create the PR manually" | 
					
						
							|  |  |  |             end | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |             check_new_version(commit_formula, tap_remote_repo, url: new_url) if new_version.blank? | 
					
						
							|  |  |  |             resource_path, forced_version = fetch_resource_and_forced_version(commit_formula, new_version, new_url) | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |             Utils::Tar.validate_file(resource_path) | 
					
						
							|  |  |  |             new_hash = resource_path.sha256 | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2025-04-22 12:33:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |           replacement_pairs = [] | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |           if commit_formula.revision.nonzero? | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |             replacement_pairs << [ | 
					
						
							|  |  |  |               /^  revision \d+\n(\n(  head "))?/m, | 
					
						
							|  |  |  |               "\\2", | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2025-04-22 12:33:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |           replacement_pairs += commit_formula_spec.mirrors.map do |mirror| | 
					
						
							| 
									
										
										
										
											2025-04-22 12:33:59 +01:00
										 |  |  |             [ | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |               / +mirror "#{Regexp.escape(mirror)}"\n/m, | 
					
						
							|  |  |  |               "", | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2016-03-17 05:58:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |           replacement_pairs += if new_url_hash.present? | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |             [ | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |               [ | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |                 /#{Regexp.escape(T.must(commit_formula_spec.url))}/, | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |                 new_url, | 
					
						
							|  |  |  |               ], | 
					
						
							|  |  |  |               [ | 
					
						
							|  |  |  |                 old_hash, | 
					
						
							|  |  |  |                 new_hash, | 
					
						
							|  |  |  |               ], | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |             ] | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |           elsif new_tag.present? | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |             [ | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |               [ | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |                 /tag:(\s+")#{commit_formula_spec.specs[:tag]}(?=")/, | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |                 "tag:\\1#{new_tag}\\2", | 
					
						
							|  |  |  |               ], | 
					
						
							|  |  |  |               [ | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |                 commit_formula_spec.specs[:revision], | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |                 new_revision, | 
					
						
							|  |  |  |               ], | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |             ] | 
					
						
							|  |  |  |           elsif new_url.present? | 
					
						
							|  |  |  |             [ | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |               [ | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |                 /#{Regexp.escape(T.must(commit_formula_spec.url))}/, | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |                 new_url, | 
					
						
							|  |  |  |               ], | 
					
						
							|  |  |  |               [ | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |                 commit_formula_spec.specs[:revision], | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |                 new_revision, | 
					
						
							|  |  |  |               ], | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |             ] | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |           else | 
					
						
							| 
									
										
										
										
											2025-04-22 12:33:59 +01:00
										 |  |  |             [ | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |               [ | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |                 commit_formula_spec.specs[:revision], | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |                 new_revision, | 
					
						
							|  |  |  |               ], | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |             ] | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |           old_contents = commit_formula.path.read | 
					
						
							| 
									
										
										
										
											2023-03-07 19:41:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |           if new_mirrors.present? && new_url.present? | 
					
						
							|  |  |  |             replacement_pairs << [ | 
					
						
							|  |  |  |               /^( +)(url "#{Regexp.escape(new_url)}"[^\n]*?\n)/m, | 
					
						
							|  |  |  |               "\\1\\2\\1mirror \"#{new_mirrors.join("\"\n\\1mirror \"")}\"\n", | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |           if forced_version && new_version != "0" | 
					
						
							|  |  |  |             replacement_pairs << if old_contents.include?("version \"#{old_formula_version}\"") | 
					
						
							|  |  |  |               [ | 
					
						
							|  |  |  |                 "version \"#{old_formula_version}\"", | 
					
						
							|  |  |  |                 "version \"#{new_version}\"", | 
					
						
							|  |  |  |               ] | 
					
						
							|  |  |  |             elsif new_mirrors.present? | 
					
						
							|  |  |  |               [ | 
					
						
							|  |  |  |                 /^( +)(mirror "#{Regexp.escape(new_mirrors.last)}"\n)/m, | 
					
						
							|  |  |  |                 "\\1\\2\\1version \"#{new_version}\"\n", | 
					
						
							|  |  |  |               ] | 
					
						
							|  |  |  |             elsif new_url.present? | 
					
						
							|  |  |  |               [ | 
					
						
							|  |  |  |                 /^( +)(url "#{Regexp.escape(new_url)}"[^\n]*?\n)/m, | 
					
						
							|  |  |  |                 "\\1\\2\\1version \"#{new_version}\"\n", | 
					
						
							|  |  |  |               ] | 
					
						
							|  |  |  |             elsif new_revision.present? | 
					
						
							|  |  |  |               [ | 
					
						
							|  |  |  |                 /^( {2})( +)(:revision => "#{new_revision}"\n)/m, | 
					
						
							|  |  |  |                 "\\1\\2\\3\\1version \"#{new_version}\"\n", | 
					
						
							|  |  |  |               ] | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           elsif forced_version && new_version == "0" | 
					
						
							|  |  |  |             replacement_pairs << [ | 
					
						
							|  |  |  |               /^  version "[\w.\-+]+"\n/m, | 
					
						
							|  |  |  |               "", | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |           new_contents = Utils::Inreplace.inreplace_pairs(commit_formula.path, | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |                                                           replacement_pairs.uniq.compact, | 
					
						
							|  |  |  |                                                           read_only_run: args.dry_run?, | 
					
						
							|  |  |  |                                                           silent:        args.quiet?) | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |           new_formula_version = formula_version(commit_formula, new_contents) | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |           if new_formula_version < old_formula_version | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |             commit_formula.path.atomic_write(old_contents) unless args.dry_run? | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |             odie <<~EOS | 
					
						
							|  |  |  |               You need to bump this formula manually since changing the version | 
					
						
							|  |  |  |               from #{old_formula_version} to #{new_formula_version} would be a downgrade. | 
					
						
							|  |  |  |             EOS | 
					
						
							|  |  |  |           elsif new_formula_version == old_formula_version | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |             commit_formula.path.atomic_write(old_contents) unless args.dry_run? | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |             odie <<~EOS | 
					
						
							|  |  |  |               You need to bump this formula manually since the new version | 
					
						
							|  |  |  |               and old version are both #{new_formula_version}. | 
					
						
							|  |  |  |             EOS | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |           alias_rename = alias_update_pair(commit_formula, new_formula_version) | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |           if alias_rename.present? | 
					
						
							|  |  |  |             ohai "Renaming alias #{alias_rename.first} to #{alias_rename.last}" | 
					
						
							|  |  |  |             alias_rename.map! { |a| tap.alias_dir/a } | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |           unless args.dry_run? | 
					
						
							|  |  |  |             resources_checked = PyPI.update_python_resources! formula, | 
					
						
							|  |  |  |                                                               version:                  new_formula_version.to_s, | 
					
						
							|  |  |  |                                                               package_name:             args.python_package_name, | 
					
						
							|  |  |  |                                                               extra_packages:           args.python_extra_packages, | 
					
						
							|  |  |  |                                                               exclude_packages:         args.python_exclude_packages, | 
					
						
							|  |  |  |                                                               install_dependencies:     args.install_dependencies?, | 
					
						
							|  |  |  |                                                               silent:                   args.quiet?, | 
					
						
							|  |  |  |                                                               ignore_non_pypi_packages: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |             update_matching_version_resources! commit_formula, | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |                                                version: new_formula_version.to_s | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2025-04-22 12:33:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |           if resources_checked.nil? && commit_formula.resources.any? do |resource| | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |             resource.livecheck.formula != :parent && !resource.name.start_with?("homebrew-") | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |             formula_pr_message += <<~EOS | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |               - [ ] `resource` blocks have been checked for updates. | 
					
						
							|  |  |  |             EOS | 
					
						
							| 
									
										
										
										
											2025-04-22 12:33:59 +01:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |           if new_url =~ %r{^https://github\.com/([\w-]+)/([\w-]+)/archive/refs/tags/(v?[.0-9]+)\.tar\.} | 
					
						
							|  |  |  |             owner = Regexp.last_match(1) | 
					
						
							|  |  |  |             repo = Regexp.last_match(2) | 
					
						
							|  |  |  |             tag = Regexp.last_match(3) | 
					
						
							|  |  |  |             github_release_data = begin | 
					
						
							|  |  |  |               GitHub::API.open_rest("#{GitHub::API_URL}/repos/#{owner}/#{repo}/releases/tags/#{tag}") | 
					
						
							|  |  |  |             rescue GitHub::API::HTTPNotFoundError | 
					
						
							|  |  |  |               # If this is a 404: we can't do anything. | 
					
						
							|  |  |  |               nil | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-12 04:48:15 +01:00
										 |  |  |             if github_release_data.present? && github_release_data["body"].present? | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |               pre = "pre" if github_release_data["prerelease"].present? | 
					
						
							| 
									
										
										
										
											2025-05-21 12:45:47 +00:00
										 |  |  |               # maximum length of PR body is 65,536 characters so let's truncate release notes to half of that. | 
					
						
							| 
									
										
										
										
											2025-05-21 17:02:18 +01:00
										 |  |  |               body = Formatter.truncate(github_release_data["body"], max: 32_768) | 
					
						
							| 
									
										
										
										
											2025-05-21 12:45:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-27 18:52:43 +02:00
										 |  |  |               # Ensure the URL is properly HTML encoded to handle any quotes or other special characters | 
					
						
							|  |  |  |               html_url = CGI.escapeHTML(github_release_data["html_url"]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |               formula_pr_message += <<~XML | 
					
						
							|  |  |  |                 <details> | 
					
						
							|  |  |  |                   <summary>#{pre}release notes</summary> | 
					
						
							| 
									
										
										
										
											2025-05-21 12:45:47 +00:00
										 |  |  |                   <pre>#{body}</pre> | 
					
						
							| 
									
										
										
										
											2025-06-27 18:52:43 +02:00
										 |  |  |                   <p>View the full release notes at <a href="#{html_url}">#{html_url}</a>.</p> | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |                 </details>
 | 
					
						
							|  |  |  |               XML | 
					
						
							|  |  |  |             end | 
					
						
							| 
									
										
										
										
											2025-04-22 12:33:59 +01:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |           { | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |             sourcefile_path:    commit_formula.path, | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |             old_contents:, | 
					
						
							| 
									
										
										
										
											2025-05-19 00:04:16 +10:00
										 |  |  |             commit_message:     "#{commit_formula.name} #{new_formula_version}", | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |             additional_files:   alias_rename, | 
					
						
							|  |  |  |             formula_pr_message:, | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |             formula_name:       commit_formula.name, | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |             new_version:        new_formula_version, | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         commits.each do |commit| | 
					
						
							|  |  |  |           commit_formula = Formula[commit[:formula_name]] | 
					
						
							|  |  |  |           # For each formula, run `brew audit` to check for any issues. | 
					
						
							|  |  |  |           audit_result = run_audit(commit_formula, commit[:additional_files], | 
					
						
							|  |  |  |                                    skip_synced_versions: args.bump_synced.present?) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           next unless audit_result | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           # If `brew audit` fails, revert the changes made to any formula. | 
					
						
							|  |  |  |           commits.each do |revert| | 
					
						
							|  |  |  |             revert_formula = Formula[revert[:formula_name]] | 
					
						
							| 
									
										
										
										
											2025-05-26 08:40:14 +01:00
										 |  |  |             revert_formula.path.atomic_write(revert[:old_contents]) if !args.dry_run? && !args.write_only? | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |             revert_alias_rename = revert[:additional_files] | 
					
						
							|  |  |  |             if revert_alias_rename && (source = revert_alias_rename.first) && (destination = revert_alias_rename.last) | 
					
						
							|  |  |  |               FileUtils.mv source, destination | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           odie "`brew audit` failed for #{commit[:formula_name]}!" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         new_formula_version = T.must(commits.first)[:new_version] | 
					
						
							| 
									
										
										
										
											2025-05-19 00:04:16 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |         pr_title = if args.bump_synced.nil? | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |           "#{formula.name} #{new_formula_version}" | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |         else | 
					
						
							|  |  |  |           "#{Array(args.bump_synced).join(" ")} #{new_formula_version}" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-23 14:45:07 +10:00
										 |  |  |         pr_message = "Created with `brew bump-formula-pr`." | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |         commits.each do |commit| | 
					
						
							|  |  |  |           next if commit[:formula_pr_message].empty? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           pr_message += "<h4>#{commit[:formula_name]}</h4>" if commits.length != 1
 | 
					
						
							|  |  |  |           pr_message += "#{commit[:formula_pr_message]}<hr>" | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         pr_info = { | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |           commits:, | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |           remote:, | 
					
						
							|  |  |  |           remote_branch:, | 
					
						
							| 
									
										
										
										
											2025-04-22 20:30:29 +10:00
										 |  |  |           branch_name:     "bump-#{formula.name}-#{new_formula_version}", | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |           pr_title:, | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |           previous_branch:, | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |           tap:             tap, | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |           tap_remote_repo:, | 
					
						
							|  |  |  |           pr_message:, | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-05-23 16:47:06 +01:00
										 |  |  |         GitHub.create_bump_pr(pr_info, args:) unless args.write_only? | 
					
						
							| 
									
										
										
										
											2023-03-08 14:28:34 +01:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2023-03-07 19:41:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |       private | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-01 13:14:47 +01:00
										 |  |  |       sig { params(url: String).returns(T.nilable(String)) } | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |       def determine_mirror(url) | 
					
						
							|  |  |  |         case url | 
					
						
							|  |  |  |         when %r{.*ftp\.gnu\.org/gnu.*} | 
					
						
							|  |  |  |           url.sub "ftp.gnu.org/gnu", "ftpmirror.gnu.org" | 
					
						
							|  |  |  |         when %r{.*download\.savannah\.gnu\.org/*} | 
					
						
							|  |  |  |           url.sub "download.savannah.gnu.org", "download-mirror.savannah.gnu.org" | 
					
						
							|  |  |  |         when %r{.*www\.apache\.org/dyn/closer\.lua\?path=.*} | 
					
						
							|  |  |  |           url.sub "www.apache.org/dyn/closer.lua?path=", "archive.apache.org/dist/" | 
					
						
							|  |  |  |         when %r{.*mirrors\.ocf\.berkeley\.edu/debian.*} | 
					
						
							|  |  |  |           url.sub "mirrors.ocf.berkeley.edu/debian", "mirrorservice.org/sites/ftp.debian.org/debian" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-03-25 23:49:54 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-01 13:14:47 +01:00
										 |  |  |       sig { params(formula: String, old_mirrors: T::Array[String], new_mirrors: T::Array[String]).void } | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |       def check_for_mirrors(formula, old_mirrors, new_mirrors) | 
					
						
							|  |  |  |         return if new_mirrors.present? || old_mirrors.empty? | 
					
						
							| 
									
										
										
										
											2020-08-13 16:05:09 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |         if args.force? | 
					
						
							|  |  |  |           opoo "#{formula}: Removing all mirrors because a `--mirror=` argument was not specified." | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           odie <<~EOS | 
					
						
							|  |  |  |             #{formula}: a `--mirror=` argument for updating the mirror URL(s) was not specified. | 
					
						
							|  |  |  |             Use `--force` to remove all mirrors. | 
					
						
							|  |  |  |           EOS | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-09-07 10:22:08 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |       sig { params(old_url: String, old_version: String, new_version: String).returns(String) } | 
					
						
							|  |  |  |       def update_url(old_url, old_version, new_version) | 
					
						
							|  |  |  |         new_url = old_url.gsub(old_version, new_version) | 
					
						
							|  |  |  |         return new_url if (old_version_parts = old_version.split(".")).length < 2
 | 
					
						
							|  |  |  |         return new_url if (new_version_parts = new_version.split(".")).length != old_version_parts.length | 
					
						
							| 
									
										
										
										
											2020-09-07 10:22:08 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-02 15:24:01 +01:00
										 |  |  |         partial_old_version = old_version_parts[0..-2]&.join(".") | 
					
						
							|  |  |  |         partial_new_version = new_version_parts[0..-2]&.join(".") | 
					
						
							|  |  |  |         return new_url if partial_old_version.blank? || partial_new_version.blank? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |         new_url.gsub(%r{/(v?)#{Regexp.escape(partial_old_version)}/}, "/\\1#{partial_new_version}/") | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2024-03-16 22:42:52 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-01 19:13:38 +01:00
										 |  |  |       sig { | 
					
						
							| 
									
										
										
										
											2025-01-22 01:39:41 +05:30
										 |  |  |         params(formula_or_resource: T.any(Formula, Resource), new_version: T.nilable(String), url: String, | 
					
						
							| 
									
										
										
										
											2024-07-08 18:46:21 +01:00
										 |  |  |                specs: String).returns(T::Array[T.untyped]) | 
					
						
							| 
									
										
										
										
											2024-07-01 19:13:38 +01:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2025-01-22 01:39:41 +05:30
										 |  |  |       def fetch_resource_and_forced_version(formula_or_resource, new_version, url, **specs) | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |         resource = Resource.new | 
					
						
							|  |  |  |         resource.url(url, **specs) | 
					
						
							| 
									
										
										
										
											2025-01-22 01:39:41 +05:30
										 |  |  |         resource.owner = if formula_or_resource.is_a?(Formula) | 
					
						
							|  |  |  |           Resource.new(formula_or_resource.name) | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           Resource.new(formula_or_resource.owner.name) | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |         forced_version = new_version && new_version != resource.version.to_s | 
					
						
							|  |  |  |         resource.version(new_version) if forced_version | 
					
						
							|  |  |  |         odie "Couldn't identify version, specify it using `--version=`." if resource.version.blank? | 
					
						
							|  |  |  |         [resource.fetch, forced_version] | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2024-03-16 22:42:52 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-22 01:39:41 +05:30
										 |  |  |       sig { | 
					
						
							|  |  |  |         params( | 
					
						
							|  |  |  |           formula: Formula, | 
					
						
							| 
									
										
										
										
											2025-01-22 01:45:19 +05:30
										 |  |  |           version: String, | 
					
						
							| 
									
										
										
										
											2025-01-22 01:39:41 +05:30
										 |  |  |         ).void | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       def update_matching_version_resources!(formula, version:) | 
					
						
							|  |  |  |         formula.resources.select { |r| r.livecheck.formula == :parent }.each do |resource| | 
					
						
							|  |  |  |           new_url = update_url(resource.url, resource.version.to_s, version) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           if new_url == resource.url | 
					
						
							|  |  |  |             opoo <<~EOS | 
					
						
							|  |  |  |               You need to bump resource "#{resource.name}" manually since the new URL | 
					
						
							|  |  |  |               and old URL are both: | 
					
						
							|  |  |  |                 #{new_url} | 
					
						
							|  |  |  |             EOS | 
					
						
							|  |  |  |             next | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           new_mirrors = resource.mirrors.map do |mirror| | 
					
						
							|  |  |  |             update_url(mirror, resource.version.to_s, version) | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |           resource_path, forced_version = fetch_resource_and_forced_version(resource, version, new_url) | 
					
						
							|  |  |  |           Utils::Tar.validate_file(resource_path) | 
					
						
							|  |  |  |           new_hash = resource_path.sha256 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           inreplace_regex = /
 | 
					
						
							|  |  |  |             [ ]+resource\ "#{resource.name}"\ do\s+ | 
					
						
							|  |  |  |               url\ .*\s+ | 
					
						
							|  |  |  |               (mirror\ .*\s+)* | 
					
						
							|  |  |  |               sha256\ .*\s+ | 
					
						
							|  |  |  |               (version\ .*\s+)? | 
					
						
							|  |  |  |               (\#.*\s+)* | 
					
						
							|  |  |  |               livecheck\ do\s+ | 
					
						
							|  |  |  |                 formula\ :parent\s+ | 
					
						
							|  |  |  |               end\s+ | 
					
						
							|  |  |  |               ((\#.*\s+)* | 
					
						
							|  |  |  |               patch\ (.*\ )?do\s+ | 
					
						
							|  |  |  |                 url\ .*\s+ | 
					
						
							|  |  |  |                 sha256\ .*\s+ | 
					
						
							|  |  |  |               end\s+)* | 
					
						
							|  |  |  |             end\s | 
					
						
							|  |  |  |           /x
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-22 01:45:19 +05:30
										 |  |  |           leading_spaces = T.must(formula.path.read.match(/^([ ]+)resource "#{resource.name}"/)).captures.first | 
					
						
							| 
									
										
										
										
											2025-01-22 01:39:41 +05:30
										 |  |  |           new_resource_block = <<~EOS | 
					
						
							|  |  |  |             #{leading_spaces}resource "#{resource.name}" do | 
					
						
							|  |  |  |             #{leading_spaces}  url "#{new_url}"#{new_mirrors.map { |m| "\n#{leading_spaces}  mirror \"#{m}\"" }.join} | 
					
						
							|  |  |  |             #{leading_spaces}  sha256 "#{new_hash}" | 
					
						
							| 
									
										
										
										
											2025-07-14 14:48:08 +01:00
										 |  |  |             #{"#{leading_spaces}  version \"#{version}\"\n" if forced_version} | 
					
						
							| 
									
										
										
										
											2025-01-22 01:39:41 +05:30
										 |  |  |             #{leading_spaces}  livecheck do | 
					
						
							|  |  |  |             #{leading_spaces}    formula :parent | 
					
						
							|  |  |  |             #{leading_spaces}  end | 
					
						
							|  |  |  |             #{leading_spaces}end | 
					
						
							|  |  |  |           EOS | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           Utils::Inreplace.inreplace formula.path do |s| | 
					
						
							|  |  |  |             s.sub! inreplace_regex, new_resource_block | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-04 17:49:10 +01:00
										 |  |  |       sig { params(formula: Formula, contents: T.nilable(String)).returns(Version) } | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |       def formula_version(formula, contents = nil) | 
					
						
							|  |  |  |         spec = :stable | 
					
						
							|  |  |  |         name = formula.name | 
					
						
							|  |  |  |         path = formula.path | 
					
						
							|  |  |  |         if contents.present? | 
					
						
							|  |  |  |           Formulary.from_contents(name, path, contents, spec).version | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           Formulary::FormulaLoader.new(name, path).get_formula(spec).version | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-07-15 08:16:33 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-30 08:55:13 +01:00
										 |  |  |       sig { | 
					
						
							|  |  |  |         params(formula: Formula, tap_remote_repo: String, state: T.nilable(String), | 
					
						
							| 
									
										
										
										
											2024-08-31 16:42:43 -04:00
										 |  |  |                version: T.nilable(String)).void | 
					
						
							| 
									
										
										
										
											2024-08-30 08:55:13 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |       def check_pull_requests(formula, tap_remote_repo, state: nil, version: nil) | 
					
						
							| 
									
										
										
										
											2024-07-02 15:24:01 +01:00
										 |  |  |         tap = formula.tap | 
					
						
							|  |  |  |         return if tap.nil? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-30 08:55:13 +01:00
										 |  |  |         # if we haven't already found open requests, try for an exact match across all pull requests | 
					
						
							| 
									
										
										
										
											2024-07-01 19:13:38 +01:00
										 |  |  |         GitHub.check_for_duplicate_pull_requests( | 
					
						
							|  |  |  |           formula.name, tap_remote_repo, | 
					
						
							| 
									
										
										
										
											2024-08-30 08:55:13 +01:00
										 |  |  |           version:, | 
					
						
							|  |  |  |           state:, | 
					
						
							| 
									
										
										
										
											2025-04-01 11:01:17 +01:00
										 |  |  |           file:         formula.path.relative_path_from(tap.path).to_s, | 
					
						
							|  |  |  |           quiet:        args.quiet?, | 
					
						
							|  |  |  |           official_tap: tap.official? | 
					
						
							| 
									
										
										
										
											2024-07-01 19:13:38 +01:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-03-25 23:49:54 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-01 19:13:38 +01:00
										 |  |  |       sig { | 
					
						
							|  |  |  |         params(formula: Formula, tap_remote_repo: String, version: T.nilable(String), url: T.nilable(String), | 
					
						
							|  |  |  |                tag: T.nilable(String)).void | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |       def check_new_version(formula, tap_remote_repo, version: nil, url: nil, tag: nil) | 
					
						
							|  |  |  |         if version.nil? | 
					
						
							|  |  |  |           specs = {} | 
					
						
							|  |  |  |           specs[:tag] = tag if tag.present? | 
					
						
							| 
									
										
										
										
											2024-07-03 16:20:36 +01:00
										 |  |  |           return if url.blank? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           version = Version.detect(url, **specs).to_s | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |           return if version.blank? | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2020-06-14 20:49:52 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |         check_throttle(formula, version) | 
					
						
							| 
									
										
										
										
											2024-08-30 08:55:13 +01:00
										 |  |  |         check_pull_requests(formula, tap_remote_repo, version:) | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2022-01-20 19:19:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-04 10:35:00 +01:00
										 |  |  |       sig { params(formula: Formula, new_version: String).void } | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |       def check_throttle(formula, new_version) | 
					
						
							| 
									
										
										
										
											2024-07-02 15:24:01 +01:00
										 |  |  |         tap = formula.tap | 
					
						
							|  |  |  |         return if tap.nil? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 09:12:14 -04:00
										 |  |  |         throttled_rate = formula.livecheck.throttle | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |         return if throttled_rate.blank? | 
					
						
							| 
									
										
										
										
											2021-01-24 09:34:53 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |         formula_suffix = Version.new(new_version).patch.to_i | 
					
						
							|  |  |  |         return if formula_suffix.modulo(throttled_rate).zero? | 
					
						
							| 
									
										
										
										
											2021-01-24 09:34:53 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |         odie "#{formula} should only be updated every #{throttled_rate} releases on multiples of #{throttled_rate}" | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2021-01-24 09:34:53 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-04 17:49:10 +01:00
										 |  |  |       sig { params(formula: Formula, new_formula_version: Version).returns(T.nilable(T::Array[String])) } | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |       def alias_update_pair(formula, new_formula_version) | 
					
						
							|  |  |  |         versioned_alias = formula.aliases.grep(/^.*@\d+(\.\d+)?$/).first | 
					
						
							|  |  |  |         return if versioned_alias.nil? | 
					
						
							| 
									
										
										
										
											2019-07-14 02:02:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |         name, old_alias_version = versioned_alias.split("@") | 
					
						
							| 
									
										
										
										
											2024-07-02 15:24:01 +01:00
										 |  |  |         return if old_alias_version.blank? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         new_alias_regex = (old_alias_version.split(".").length == 1) ? /^\d+/ : /^\d+\.\d+/ | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |         new_alias_version, = *new_formula_version.to_s.match(new_alias_regex) | 
					
						
							| 
									
										
										
										
											2024-07-02 15:24:01 +01:00
										 |  |  |         return if new_alias_version.blank? | 
					
						
							|  |  |  |         return if Version.new(new_alias_version) <= Version.new(old_alias_version) | 
					
						
							| 
									
										
										
										
											2019-07-14 02:02:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |         [versioned_alias, "#{name}@#{new_alias_version}"] | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2019-07-14 02:02:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |       sig { | 
					
						
							|  |  |  |         params(formula: Formula, alias_rename: T.nilable(T::Array[String]), | 
					
						
							|  |  |  |                skip_synced_versions: T::Boolean).returns(T::Boolean) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       def run_audit(formula, alias_rename, skip_synced_versions: false) | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |         audit_args = ["--formula"] | 
					
						
							|  |  |  |         audit_args << "--strict" if args.strict? | 
					
						
							|  |  |  |         audit_args << "--online" if args.online? | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |         audit_args << "--except=synced_versions_formulae" if skip_synced_versions | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |         if args.dry_run? | 
					
						
							|  |  |  |           if args.no_audit? | 
					
						
							|  |  |  |             ohai "Skipping `brew audit`" | 
					
						
							|  |  |  |           elsif audit_args.present? | 
					
						
							|  |  |  |             ohai "brew audit #{audit_args.join(" ")} #{formula.path.basename}" | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             ohai "brew audit #{formula.path.basename}" | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |           return true | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2024-07-03 14:09:11 +01:00
										 |  |  |         if alias_rename && (source = alias_rename.first) && (destination = alias_rename.last) | 
					
						
							|  |  |  |           FileUtils.mv source, destination | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2024-03-18 16:11:28 -07:00
										 |  |  |         failed_audit = false | 
					
						
							|  |  |  |         if args.no_audit? | 
					
						
							|  |  |  |           ohai "Skipping `brew audit`" | 
					
						
							|  |  |  |         elsif audit_args.present? | 
					
						
							|  |  |  |           system HOMEBREW_BREW_FILE, "audit", *audit_args, formula.full_name | 
					
						
							|  |  |  |           failed_audit = !$CHILD_STATUS.success? | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           system HOMEBREW_BREW_FILE, "audit", formula.full_name | 
					
						
							|  |  |  |           failed_audit = !$CHILD_STATUS.success? | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2025-04-09 22:55:56 +10:00
										 |  |  |         failed_audit | 
					
						
							| 
									
										
										
										
											2019-07-16 23:05:36 +02:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2016-03-17 05:58:14 +00:00
										 |  |  | end |