| 
									
										
										
										
											2016-06-17 10:10:47 -07:00
										 |  |  | #:  * `bump-formula-pr` [`--devel`] [`--dry-run`] [`--audit`|`--strict`] `--url=`<url> `--sha256=`<sha-256> <formula>: | 
					
						
							|  |  |  | #:  * `bump-formula-pr` [`--devel`] [`--dry-run`] [`--audit`|`--strict`] `--tag=`<tag> `--revision=`<revision> <formula>: | 
					
						
							| 
									
										
										
										
											2016-08-08 03:42:40 -04:00
										 |  |  | #:    Creates a pull request to update the formula with a new url or a new tag. | 
					
						
							|  |  |  | #: | 
					
						
							|  |  |  | #:    If a <url> is specified, the <sha-256> checksum of the new download must | 
					
						
							|  |  |  | #:    also be specified. | 
					
						
							|  |  |  | #: | 
					
						
							|  |  |  | #:    If a <tag> is specified, the git commit <revision> corresponding to that | 
					
						
							|  |  |  | #:    tag must also be specified. | 
					
						
							|  |  |  | #: | 
					
						
							|  |  |  | #:    If `--devel` is passed, bump the development rather than stable version. | 
					
						
							|  |  |  | #:    The development spec must already exist. | 
					
						
							|  |  |  | #: | 
					
						
							|  |  |  | #:    If `--dry-run` is passed, print what would be done rather than doing it. | 
					
						
							|  |  |  | #: | 
					
						
							| 
									
										
										
										
											2016-06-17 10:10:47 -07:00
										 |  |  | #:    If `--audit` is passed, run `brew audit` before opening the PR. | 
					
						
							|  |  |  | #: | 
					
						
							|  |  |  | #:    If `--strict` is passed, run `brew audit --strict` before opening the PR. | 
					
						
							|  |  |  | #: | 
					
						
							| 
									
										
										
										
											2016-08-08 03:42:40 -04:00
										 |  |  | #:    Note that this command cannot be used to transition a formula from a | 
					
						
							|  |  |  | #:    url-and-sha256 style specification into a tag-and-revision style | 
					
						
							|  |  |  | #:    specification, nor vice versa. It must use whichever style specification | 
					
						
							|  |  |  | #:    the preexisting formula already uses. | 
					
						
							| 
									
										
										
										
											2016-03-17 05:58:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | require "formula" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2016-06-16 14:25:53 -07:00
										 |  |  |   def inreplace_pairs(path, replacement_pairs) | 
					
						
							|  |  |  |     if ARGV.dry_run? | 
					
						
							|  |  |  |       contents = path.open("r") { |f| Formulary.set_encoding(f).read } | 
					
						
							|  |  |  |       contents.extend(StringInreplaceExtension) | 
					
						
							|  |  |  |       replacement_pairs.each do |old, new| | 
					
						
							| 
									
										
										
										
											2016-06-17 08:17:56 -07:00
										 |  |  |         unless ARGV.flag?("--quiet") | 
					
						
							|  |  |  |           ohai "replace #{old.inspect} with #{new.inspect}" | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2016-06-16 14:25:53 -07:00
										 |  |  |         contents.gsub!(old, new) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-05 22:01:32 +08:00
										 |  |  |       unless contents.errors.empty? | 
					
						
							| 
									
										
										
										
											2016-06-16 14:25:53 -07:00
										 |  |  |         raise Utils::InreplaceError, path => contents.errors | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       contents | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       Utils::Inreplace.inreplace(path) do |s| | 
					
						
							|  |  |  |         replacement_pairs.each do |old, new| | 
					
						
							| 
									
										
										
										
											2016-06-17 08:17:56 -07:00
										 |  |  |           unless ARGV.flag?("--quiet") | 
					
						
							|  |  |  |             ohai "replace #{old.inspect} with #{new.inspect}" | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2016-06-16 14:25:53 -07:00
										 |  |  |           s.gsub!(old, new) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       path.open("r") { |f| Formulary.set_encoding(f).read } | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def formula_version(formula, spec, contents = nil) | 
					
						
							|  |  |  |     name = formula.name | 
					
						
							|  |  |  |     path = formula.path | 
					
						
							|  |  |  |     if contents | 
					
						
							|  |  |  |       Formulary.from_contents(name, path, contents, spec).version | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       Formulary::FormulaLoader.new(name, path).get_formula(spec).version | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-17 05:58:14 +00:00
										 |  |  |   def bump_formula_pr | 
					
						
							|  |  |  |     formula = ARGV.formulae.first | 
					
						
							|  |  |  |     odie "No formula found!" unless formula | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     requested_spec, formula_spec = if ARGV.include?("--devel") | 
					
						
							|  |  |  |       devel_message = " (devel)" | 
					
						
							|  |  |  |       [:devel, formula.devel] | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       [:stable, formula.stable] | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-03-28 00:29:48 +02:00
										 |  |  |     odie "#{formula}: no #{requested_spec} specification found!" unless formula_spec | 
					
						
							| 
									
										
										
										
											2016-03-17 05:58:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     hash_type, old_hash = if (checksum = formula_spec.checksum) | 
					
						
							|  |  |  |       [checksum.hash_type.to_s, checksum.hexdigest] | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     new_url = ARGV.value("url") | 
					
						
							|  |  |  |     new_hash = ARGV.value(hash_type) | 
					
						
							|  |  |  |     new_tag = ARGV.value("tag") | 
					
						
							|  |  |  |     new_revision = ARGV.value("revision") | 
					
						
							|  |  |  |     new_url_hash = if new_url && new_hash | 
					
						
							|  |  |  |       true | 
					
						
							|  |  |  |     elsif new_tag && new_revision | 
					
						
							|  |  |  |       false | 
					
						
							|  |  |  |     elsif !hash_type | 
					
						
							|  |  |  |       odie "#{formula}: no tag/revision specified!" | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       odie "#{formula}: no url/#{hash_type} specified!" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ARGV.dry_run? | 
					
						
							|  |  |  |       ohai "brew update" | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       safe_system "brew", "update" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-16 14:25:53 -07:00
										 |  |  |     old_formula_version = formula_version(formula, requested_spec) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-17 08:17:56 -07:00
										 |  |  |     replacement_pairs = [] | 
					
						
							|  |  |  |     if requested_spec == :stable && formula.revision != 0
 | 
					
						
							|  |  |  |       replacement_pairs << [/^  revision \d+\n(\n(  head "))?/m, "\\2"] | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     replacement_pairs += if new_url_hash | 
					
						
							| 
									
										
										
										
											2016-06-16 14:25:53 -07:00
										 |  |  |       [ | 
					
						
							|  |  |  |         [formula_spec.url, new_url], | 
					
						
							|  |  |  |         [old_hash, new_hash], | 
					
						
							|  |  |  |       ] | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       [ | 
					
						
							|  |  |  |         [formula_spec.specs[:tag], new_tag], | 
					
						
							|  |  |  |         [formula_spec.specs[:revision], new_revision], | 
					
						
							|  |  |  |       ] | 
					
						
							| 
									
										
										
										
											2016-03-17 05:58:14 +00:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-17 10:10:47 -07:00
										 |  |  |     backup_file = File.read(formula.path) unless ARGV.dry_run? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-16 14:25:53 -07:00
										 |  |  |     new_contents = inreplace_pairs(formula.path, replacement_pairs) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     new_formula_version = formula_version(formula, requested_spec, new_contents) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if new_formula_version < old_formula_version | 
					
						
							| 
									
										
										
										
											2016-06-17 10:10:47 -07:00
										 |  |  |       formula.path.atomic_write(backup_file) unless ARGV.dry_run? | 
					
						
							| 
									
										
										
										
											2016-06-16 14:25:53 -07:00
										 |  |  |       odie <<-EOS.undent
 | 
					
						
							|  |  |  |         You probably 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 | 
					
						
							| 
									
										
										
										
											2016-06-17 10:10:47 -07:00
										 |  |  |       formula.path.atomic_write(backup_file) unless ARGV.dry_run? | 
					
						
							| 
									
										
										
										
											2016-06-16 14:25:53 -07:00
										 |  |  |       odie <<-EOS.undent
 | 
					
						
							|  |  |  |         You probably need to bump this formula manually since the new version | 
					
						
							|  |  |  |         and old version are both #{new_formula_version}. | 
					
						
							|  |  |  |       EOS | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-03-17 05:58:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-17 10:10:47 -07:00
										 |  |  |     if ARGV.dry_run? | 
					
						
							|  |  |  |       if ARGV.include? "--strict" | 
					
						
							|  |  |  |         ohai "brew audit --strict #{formula.path.basename}" | 
					
						
							|  |  |  |       elsif ARGV.include? "--audit" | 
					
						
							|  |  |  |         ohai "brew audit #{formula.path.basename}" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       failed_audit = false | 
					
						
							|  |  |  |       if ARGV.include? "--strict" | 
					
						
							|  |  |  |         system HOMEBREW_BREW_FILE, "audit", "--strict", formula.path | 
					
						
							|  |  |  |         failed_audit = !$?.success? | 
					
						
							|  |  |  |       elsif ARGV.include? "--audit" | 
					
						
							|  |  |  |         system HOMEBREW_BREW_FILE, "audit", formula.path | 
					
						
							|  |  |  |         failed_audit = !$?.success? | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       if failed_audit | 
					
						
							|  |  |  |         formula.path.atomic_write(backup_file) | 
					
						
							|  |  |  |         odie "brew audit failed!" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-17 05:58:14 +00:00
										 |  |  |     unless Formula["hub"].any_version_installed? | 
					
						
							|  |  |  |       if ARGV.dry_run? | 
					
						
							|  |  |  |         ohai "brew install hub" | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         safe_system "brew", "install", "hub" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     formula.path.parent.cd do | 
					
						
							|  |  |  |       branch = "#{formula.name}-#{new_formula_version}" | 
					
						
							|  |  |  |       if ARGV.dry_run? | 
					
						
							| 
									
										
										
										
											2016-08-19 10:32:54 -04:00
										 |  |  |         ohai "git checkout --no-track -b #{branch} origin/master" | 
					
						
							| 
									
										
										
										
											2016-03-17 05:58:14 +00:00
										 |  |  |         ohai "git commit --no-edit --verbose --message='#{formula.name} #{new_formula_version}#{devel_message}' -- #{formula.path}" | 
					
						
							|  |  |  |         ohai "hub fork --no-remote" | 
					
						
							|  |  |  |         ohai "hub fork" | 
					
						
							|  |  |  |         ohai "hub fork (to read $HUB_REMOTE)" | 
					
						
							| 
									
										
										
										
											2016-08-19 10:32:54 -04:00
										 |  |  |         ohai "git push --set-upstream $HUB_REMOTE #{branch}:#{branch}" | 
					
						
							| 
									
										
										
										
											2016-03-17 05:58:14 +00:00
										 |  |  |         ohai "hub pull-request --browse -m '#{formula.name} #{new_formula_version}#{devel_message}'" | 
					
						
							| 
									
										
										
										
											2016-08-19 15:59:55 -04:00
										 |  |  |         ohai "git checkout -" | 
					
						
							| 
									
										
										
										
											2016-03-17 05:58:14 +00:00
										 |  |  |       else | 
					
						
							| 
									
										
										
										
											2016-08-19 10:32:54 -04:00
										 |  |  |         safe_system "git", "checkout", "--no-track", "-b", branch, "origin/master" | 
					
						
							| 
									
										
										
										
											2016-03-17 05:58:14 +00:00
										 |  |  |         safe_system "git", "commit", "--no-edit", "--verbose", | 
					
						
							|  |  |  |           "--message=#{formula.name} #{new_formula_version}#{devel_message}", | 
					
						
							|  |  |  |           "--", formula.path | 
					
						
							|  |  |  |         safe_system "hub", "fork", "--no-remote" | 
					
						
							|  |  |  |         quiet_system "hub", "fork" | 
					
						
							| 
									
										
										
										
											2016-07-13 16:19:51 +08:00
										 |  |  |         remote = Utils.popen_read("hub fork 2>&1")[/fatal: remote (.+) already exists\./, 1] | 
					
						
							| 
									
										
										
										
											2016-03-17 05:58:14 +00:00
										 |  |  |         odie "cannot get remote from 'hub'!" if remote.to_s.empty? | 
					
						
							| 
									
										
										
										
											2016-08-19 10:32:54 -04:00
										 |  |  |         safe_system "git", "push", "--set-upstream", remote, "#{branch}:#{branch}" | 
					
						
							| 
									
										
										
										
											2016-03-17 05:58:14 +00:00
										 |  |  |         safe_system "hub", "pull-request", "--browse", "-m", | 
					
						
							|  |  |  |           "#{formula.name} #{new_formula_version}#{devel_message}\n\nCreated with `brew bump-formula-pr`." | 
					
						
							| 
									
										
										
										
											2016-08-19 15:59:55 -04:00
										 |  |  |         safe_system "git", "checkout", "-" | 
					
						
							| 
									
										
										
										
											2016-03-17 05:58:14 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |