Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bump-formula-pr: cleanup #8043

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 36 additions & 58 deletions Library/Homebrew/dev-cmd/bump-formula-pr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ def bump_formula_pr_args
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 "--devel",
description: "Bump the development rather than stable version. The development spec must already exist."
switch "-n", "--dry-run",
description: "Print what would be done rather than doing it."
switch "--write",
Expand Down Expand Up @@ -123,7 +121,7 @@ def bump_formula_pr
formula = args.formulae.first

new_url = args.url
formula ||= determine_formula_from_url(new_url, args.devel?) if new_url
formula ||= determine_formula_from_url(new_url) if new_url
raise FormulaUnspecifiedError unless formula

tap_full_name, origin_branch, previous_branch = use_correct_linux_tap(formula)
Expand All @@ -132,12 +130,8 @@ def bump_formula_pr
new_version = args.version
check_all_pull_requests(formula, tap_full_name, version: new_version) if new_version

requested_spec, formula_spec = if args.devel?
devel_message = " (devel)"
[:devel, formula.devel]
else
[:stable, formula.stable]
end
requested_spec = :stable
formula_spec = formula.stable
odie "#{formula}: no #{requested_spec} specification found!" unless formula_spec

hash_type, old_hash = if (checksum = formula_spec.checksum)
Expand All @@ -149,7 +143,7 @@ def bump_formula_pr
new_revision = args.revision
new_mirrors ||= args.mirror
new_mirror ||= case new_url
when requested_spec != :devel && %r{.*ftp.gnu.org/gnu.*}
when %r{.*ftp.gnu.org/gnu.*}
new_url.sub "ftp.gnu.org/gnu", "ftpmirror.gnu.org"
when %r{.*download.savannah.gnu.org/*}
new_url.sub "download.savannah.gnu.org", "download-mirror.savannah.gnu.org"
Expand All @@ -175,7 +169,7 @@ def bump_formula_pr
new_tag ||= old_tag.gsub(old_version, new_version)
if new_tag == old_tag
odie <<~EOS
You probably need to bump this formula manually since the new tag
You need to bump this formula manually since the new tag
and old tag are both #{new_tag}.
EOS
end
Expand All @@ -190,7 +184,7 @@ def bump_formula_pr
new_url ||= old_url.gsub(old_version, new_version)
if new_url == old_url
odie <<~EOS
You probably need to bump this formula manually since the new URL
You need to bump this formula manually since the new URL
and old URL are both:
#{new_url}
EOS
Expand Down Expand Up @@ -272,43 +266,27 @@ def bump_formula_pr
end

if forced_version && new_version != "0"
case requested_spec
when :stable
replacement_pairs << if File.read(formula.path).include?("version \"#{old_formula_version}\"")
[
old_formula_version.to_s,
new_version,
]
elsif new_mirrors
[
/^( +)(mirror "#{Regexp.escape(new_mirrors.last)}"\n)/m,
"\\1\\2\\1version \"#{new_version}\"\n",
]
else
[
/^( +)(url "#{Regexp.escape(new_url)}"\n)/m,
"\\1\\2\\1version \"#{new_version}\"\n",
]
end
when :devel
replacement_pairs << [
/( devel do.+?version ")#{old_formula_version}("\n.+?end\n)/m,
"\\1#{new_version}\\2",
replacement_pairs << if File.read(formula.path).include?("version \"#{old_formula_version}\"")
[
old_formula_version.to_s,
new_version,
]
end
elsif forced_version && new_version == "0"
case requested_spec
when :stable
replacement_pairs << [
/^ version "[\w.\-+]+"\n/m,
"",
elsif new_mirrors
[
/^( +)(mirror "#{Regexp.escape(new_mirrors.last)}"\n)/m,
"\\1\\2\\1version \"#{new_version}\"\n",
]
when :devel
replacement_pairs << [
/( devel do.+?)^ +version "[^\n]+"\n(.+?end\n)/m,
"\\1\\2",
else
[
/^( +)(url "#{Regexp.escape(new_url)}"\n)/m,
"\\1\\2\\1version \"#{new_version}\"\n",
]
end
elsif forced_version && new_version == "0"
replacement_pairs << [
/^ version "[\w.\-+]+"\n/m,
"",
]
end
new_contents = inreplace_pairs(formula.path, replacement_pairs.uniq)

Expand All @@ -328,13 +306,13 @@ def bump_formula_pr
if new_formula_version < old_formula_version
formula.path.atomic_write(old_contents) unless args.dry_run?
odie <<~EOS
You probably need to bump this formula manually since changing the
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
formula.path.atomic_write(old_contents) unless args.dry_run?
odie <<~EOS
You probably need to bump this formula manually since the new version
You need to bump this formula manually since the new version
and old version are both #{new_formula_version}.
EOS
end
Expand All @@ -360,7 +338,7 @@ def bump_formula_pr
ohai "git add #{alias_rename.first} #{alias_rename.last}" if alias_rename.present?
ohai "git checkout --no-track -b #{branch} #{origin_branch}"
ohai "git commit --no-edit --verbose --message='#{formula.name} " \
"#{new_formula_version}#{devel_message}' -- #{changed_files.join(" ")}"
"#{new_formula_version}' -- #{changed_files.join(" ")}"
ohai "git push --set-upstream $HUB_REMOTE #{branch}:#{branch}"
ohai "git checkout --quiet #{previous_branch}"
ohai "create pull request with GitHub API"
Expand All @@ -377,7 +355,7 @@ def bump_formula_pr
safe_system "git", "add", *alias_rename if alias_rename.present?
safe_system "git", "checkout", "--no-track", "-b", branch, origin_branch
safe_system "git", "commit", "--no-edit", "--verbose",
"--message=#{formula.name} #{new_formula_version}#{devel_message}",
"--message=#{formula.name} #{new_formula_version}",
"--", *changed_files
safe_system "git", "push", "--set-upstream", remote_url, "#{branch}:#{branch}"
safe_system "git", "checkout", "--quiet", previous_branch
Expand All @@ -392,7 +370,7 @@ def bump_formula_pr
#{user_message}
EOS
end
pr_title = "#{formula.name} #{new_formula_version}#{devel_message}"
pr_title = "#{formula.name} #{new_formula_version}"

begin
url = GitHub.create_pull_request(tap_full_name, pr_title,
Expand All @@ -409,7 +387,7 @@ def bump_formula_pr
end
end

def determine_formula_from_url(url, is_devel)
def determine_formula_from_url(url)
# Split the new URL on / and find any formulae that have the same URL
# except for the last component, but don't try to match any more than the
# first five components since sometimes the last component isn't the only
Expand All @@ -421,14 +399,10 @@ def determine_formula_from_url(url, is_devel)
base_url = /#{Regexp.escape(base_url)}/
guesses = []
Formula.each do |f|
if is_devel && f.devel && f.devel.url && f.devel.url.match(base_url)
guesses << f
elsif f.stable&.url && f.stable.url.match(base_url)
guesses << f
end
guesses << f if f.stable&.url && f.stable.url.match(base_url)
end
return guesses.shift if guesses.count == 1
return unless guesses.count > 1
return if guesses.count <= 1

odie "Couldn't guess formula for sure; could be one of these:\n#{guesses.map(&:name).join(", ")}"
end
Expand Down Expand Up @@ -518,7 +492,11 @@ def check_open_pull_requests(formula, tap_full_name)
end

def check_all_pull_requests(formula, tap_full_name, version: nil, url: nil, tag: nil)
version ||= Version.detect(url, tag ? { tag: tag } : {})
unless version
specs = {}
specs[:tag] = tag if tag
version = Version.detect(url, specs)
end
# if we haven't already found open requests, try for an exact match across all requests
pull_requests = fetch_pull_requests("#{formula.name} #{version}", tap_full_name) if pull_requests.blank?
check_for_duplicate_pull_requests(pull_requests)
Expand Down
2 changes: 0 additions & 2 deletions docs/Manpage.md
Original file line number Diff line number Diff line change
Expand Up @@ -719,8 +719,6 @@ 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.

* `--devel`:
Bump the development rather than stable version. The development spec must already exist.
* `-n`, `--dry-run`:
Print what would be done rather than doing it.
* `--write`:
Expand Down
4 changes: 0 additions & 4 deletions manpages/brew.1
Original file line number Diff line number Diff line change
Expand Up @@ -926,10 +926,6 @@ If a \fIversion\fR is specified, a best effort to determine the \fIURL\fR and \f
\fINote:\fR 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\.
.
.TP
\fB\-\-devel\fR
Bump the development rather than stable version\. The development spec must already exist\.
.
.TP
\fB\-n\fR, \fB\-\-dry\-run\fR
Print what would be done rather than doing it\.
.
Expand Down