| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: false | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 18:25:08 +09:00
										 |  |  | require "cli/parser" | 
					
						
							| 
									
										
										
										
											2018-03-25 21:39:02 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-06 23:08:04 +08:00
										 |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   extend T::Sig | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-26 01:44:51 +02:00
										 |  |  |   module_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   sig { returns(CLI::Parser) } | 
					
						
							| 
									
										
										
										
											2018-07-30 18:25:38 +05:30
										 |  |  |   def update_test_args | 
					
						
							|  |  |  |     Homebrew::CLI::Parser.new do | 
					
						
							| 
									
										
										
										
											2021-01-15 15:04:02 -05:00
										 |  |  |       description <<~EOS | 
					
						
							| 
									
										
										
										
											2018-10-08 22:49:03 -04:00
										 |  |  |         Run a test of `brew update` with a new repository clone. | 
					
						
							| 
									
										
										
										
											2019-08-19 22:44:50 -04:00
										 |  |  |         If no options are passed, use `origin/master` as the start commit. | 
					
						
							| 
									
										
										
										
											2018-09-28 21:39:52 +05:30
										 |  |  |       EOS | 
					
						
							|  |  |  |       switch "--to-tag", | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |              description: "Set `HOMEBREW_UPDATE_TO_TAG` to test updating between tags." | 
					
						
							| 
									
										
										
										
											2018-09-28 21:39:52 +05:30
										 |  |  |       switch "--keep-tmp", | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |              description: "Retain the temporary directory containing the new repository clone." | 
					
						
							| 
									
										
										
										
											2018-09-28 21:39:52 +05:30
										 |  |  |       flag   "--commit=", | 
					
						
							| 
									
										
										
										
											2019-08-20 00:04:14 -04:00
										 |  |  |              description: "Use the specified <commit> as the start commit." | 
					
						
							| 
									
										
										
										
											2018-09-28 21:39:52 +05:30
										 |  |  |       flag   "--before=", | 
					
						
							| 
									
										
										
										
											2019-08-20 00:04:14 -04:00
										 |  |  |              description: "Use the commit at the specified <date> as the start commit." | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  |       named_args :none | 
					
						
							| 
									
										
										
										
											2018-03-25 21:39:02 +05:30
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-07-30 18:25:38 +05:30
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def update_test | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |     args = update_test_args.parse | 
					
						
							| 
									
										
										
										
											2018-03-25 21:39:02 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-04 12:13:49 +00:00
										 |  |  |     # Avoid `update-report.rb` tapping Homebrew/homebrew-core | 
					
						
							| 
									
										
										
										
											2016-10-02 17:16:35 +01:00
										 |  |  |     ENV["HOMEBREW_UPDATE_TEST"] = "1" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-04 12:13:49 +00:00
										 |  |  |     # Avoid accidentally updating when we don't expect it. | 
					
						
							|  |  |  |     ENV["HOMEBREW_NO_AUTO_UPDATE"] = "1" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Use default behaviours | 
					
						
							|  |  |  |     ENV["HOMEBREW_AUTO_UPDATE_SECS"] = nil | 
					
						
							|  |  |  |     ENV["HOMEBREW_DEVELOPER"] = nil | 
					
						
							|  |  |  |     ENV["HOMEBREW_DEV_CMD_RUN"] = nil | 
					
						
							|  |  |  |     ENV["HOMEBREW_MERGE"] = nil | 
					
						
							|  |  |  |     ENV["HOMEBREW_NO_UPDATE_CLEANUP"] = nil | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-03 15:41:33 +01:00
										 |  |  |     branch = if args.to_tag? | 
					
						
							| 
									
										
										
										
											2016-10-02 17:16:35 +01:00
										 |  |  |       ENV["HOMEBREW_UPDATE_TO_TAG"] = "1" | 
					
						
							| 
									
										
										
										
											2018-07-03 15:41:33 +01:00
										 |  |  |       "stable" | 
					
						
							| 
									
										
										
										
											2016-10-02 17:16:35 +01:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2020-11-04 12:13:49 +00:00
										 |  |  |       ENV["HOMEBREW_UPDATE_TO_TAG"] = nil | 
					
						
							| 
									
										
										
										
											2018-07-03 15:41:33 +01:00
										 |  |  |       "master" | 
					
						
							| 
									
										
										
										
											2016-10-02 17:16:35 +01:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-10-01 13:29:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-03 15:41:33 +01:00
										 |  |  |     start_commit, end_commit = nil | 
					
						
							|  |  |  |     cd HOMEBREW_REPOSITORY do | 
					
						
							| 
									
										
										
										
											2021-02-12 18:33:37 +05:30
										 |  |  |       start_commit = if (commit = args.commit) | 
					
						
							| 
									
										
										
										
											2018-07-03 15:41:33 +01:00
										 |  |  |         commit | 
					
						
							| 
									
										
										
										
											2021-02-12 18:33:37 +05:30
										 |  |  |       elsif (date = args.before) | 
					
						
							| 
									
										
										
										
											2018-07-03 15:41:33 +01:00
										 |  |  |         Utils.popen_read("git", "rev-list", "-n1", "--before=#{date}", "origin/master").chomp | 
					
						
							|  |  |  |       elsif args.to_tag? | 
					
						
							|  |  |  |         tags = Utils.popen_read("git", "tag", "--list", "--sort=-version:refname") | 
					
						
							| 
									
										
										
										
											2019-08-19 13:49:32 +01:00
										 |  |  |         if tags.blank? | 
					
						
							|  |  |  |           tags = if (HOMEBREW_REPOSITORY/".git/shallow").exist? | 
					
						
							| 
									
										
										
										
											2018-07-03 15:41:33 +01:00
										 |  |  |             safe_system "git", "fetch", "--tags", "--depth=1" | 
					
						
							| 
									
										
										
										
											2019-08-19 13:49:32 +01:00
										 |  |  |             Utils.popen_read("git", "tag", "--list", "--sort=-version:refname") | 
					
						
							| 
									
										
										
										
											2018-07-03 15:41:33 +01:00
										 |  |  |           elsif OS.linux? | 
					
						
							| 
									
										
										
										
											2019-08-19 13:49:32 +01:00
										 |  |  |             Utils.popen_read("git tag --list | sort -rV") | 
					
						
							| 
									
										
										
										
											2018-07-03 15:41:33 +01:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-05-29 15:13:08 +01:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2019-08-19 13:49:32 +01:00
										 |  |  |         current_tag, previous_tag, = tags.lines | 
					
						
							|  |  |  |         current_tag = current_tag.to_s.chomp | 
					
						
							|  |  |  |         odie "Could not find current tag in:\n#{tags}" if current_tag.empty? | 
					
						
							|  |  |  |         # ^0 ensures this points to the commit rather than the tag object. | 
					
						
							|  |  |  |         end_commit = "#{current_tag}^0" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-03 15:41:33 +01:00
										 |  |  |         previous_tag = previous_tag.to_s.chomp | 
					
						
							|  |  |  |         odie "Could not find previous tag in:\n#{tags}" if previous_tag.empty? | 
					
						
							| 
									
										
										
										
											2019-08-19 13:49:32 +01:00
										 |  |  |         # ^0 ensures this points to the commit rather than the tag object. | 
					
						
							|  |  |  |         "#{previous_tag}^0" | 
					
						
							| 
									
										
										
										
											2018-07-03 15:41:33 +01:00
										 |  |  |       else | 
					
						
							|  |  |  |         Utils.popen_read("git", "rev-parse", "origin/master").chomp | 
					
						
							| 
									
										
										
										
											2017-03-28 15:31:16 +01:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-07-03 15:41:33 +01:00
										 |  |  |       odie "Could not find start commit!" if start_commit.empty? | 
					
						
							| 
									
										
										
										
											2017-03-28 15:31:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-03 15:41:33 +01:00
										 |  |  |       start_commit = Utils.popen_read("git", "rev-parse", start_commit).chomp | 
					
						
							|  |  |  |       odie "Could not find start commit!" if start_commit.empty? | 
					
						
							| 
									
										
										
										
											2017-03-28 15:31:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 13:49:32 +01:00
										 |  |  |       end_commit ||= "HEAD" | 
					
						
							|  |  |  |       end_commit = Utils.popen_read("git", "rev-parse", end_commit).chomp | 
					
						
							| 
									
										
										
										
											2018-07-03 15:41:33 +01:00
										 |  |  |       odie "Could not find end commit!" if end_commit.empty? | 
					
						
							| 
									
										
										
										
											2019-08-19 13:49:32 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if Utils.popen_read("git", "branch", "--list", "master").blank? | 
					
						
							|  |  |  |         safe_system "git", "branch", "master", "origin/master" | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-07-03 15:41:33 +01:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2015-09-16 15:15:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-24 21:25:12 -05:00
										 |  |  |     puts <<~EOS | 
					
						
							|  |  |  |       Start commit: #{start_commit} | 
					
						
							|  |  |  |         End commit: #{end_commit} | 
					
						
							|  |  |  |     EOS | 
					
						
							| 
									
										
										
										
											2015-12-19 18:18:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-27 12:01:47 -08:00
										 |  |  |     mkdir "update-test" | 
					
						
							|  |  |  |     chdir "update-test" do | 
					
						
							| 
									
										
										
										
											2015-09-06 23:08:04 +08:00
										 |  |  |       curdir = Pathname.new(Dir.pwd) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-13 15:39:55 -05:00
										 |  |  |       oh1 "Preparing test environment..." | 
					
						
							| 
									
										
										
										
											2015-09-06 23:08:04 +08:00
										 |  |  |       # copy Homebrew installation | 
					
						
							| 
									
										
										
										
											2019-08-19 13:49:32 +01:00
										 |  |  |       safe_system "git", "clone", "#{HOMEBREW_REPOSITORY}/.git", ".", | 
					
						
							| 
									
										
										
										
											2019-11-16 09:39:18 +00:00
										 |  |  |                   "--branch", "master", "--single-branch" | 
					
						
							| 
									
										
										
										
											2015-09-14 15:19:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       # set git origin to another copy | 
					
						
							| 
									
										
										
										
											2019-08-19 13:49:32 +01:00
										 |  |  |       safe_system "git", "clone", "#{HOMEBREW_REPOSITORY}/.git", "remote.git", | 
					
						
							| 
									
										
										
										
											2019-11-16 09:39:18 +00:00
										 |  |  |                   "--bare", "--branch", "master", "--single-branch" | 
					
						
							| 
									
										
										
										
											2015-09-16 15:15:04 +08:00
										 |  |  |       safe_system "git", "config", "remote.origin.url", "#{curdir}/remote.git" | 
					
						
							| 
									
										
										
										
											2020-11-04 12:13:49 +00:00
										 |  |  |       ENV["HOMEBREW_BREW_GIT_REMOTE"] = "#{curdir}/remote.git" | 
					
						
							| 
									
										
										
										
											2015-09-14 15:19:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-22 19:26:41 +01:00
										 |  |  |       # force push origin to end_commit | 
					
						
							|  |  |  |       safe_system "git", "checkout", "-B", "master", end_commit | 
					
						
							| 
									
										
										
										
											2015-09-14 15:19:05 +08:00
										 |  |  |       safe_system "git", "push", "--force", "origin", "master" | 
					
						
							| 
									
										
										
										
											2015-09-06 23:08:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-22 19:26:41 +01:00
										 |  |  |       # set test copy to start_commit | 
					
						
							|  |  |  |       safe_system "git", "reset", "--hard", start_commit | 
					
						
							| 
									
										
										
										
											2015-09-06 23:08:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       # update ENV["PATH"] | 
					
						
							| 
									
										
										
										
											2017-09-29 22:10:44 +02:00
										 |  |  |       ENV["PATH"] = PATH.new(ENV["PATH"]).prepend(curdir/"bin") | 
					
						
							| 
									
										
										
										
											2015-09-06 23:08:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-04 12:13:49 +00:00
										 |  |  |       # run brew help to install portable-ruby (if needed) | 
					
						
							|  |  |  |       quiet_system "brew", "help" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-06 23:08:04 +08:00
										 |  |  |       # run brew update | 
					
						
							| 
									
										
										
										
											2021-01-24 21:40:41 -05:00
										 |  |  |       oh1 "Running `brew update`..." | 
					
						
							| 
									
										
										
										
											2020-11-04 12:13:49 +00:00
										 |  |  |       safe_system "brew", "update", "--verbose", "--debug" | 
					
						
							| 
									
										
										
										
											2016-10-02 17:16:35 +01:00
										 |  |  |       actual_end_commit = Utils.popen_read("git", "rev-parse", branch).chomp | 
					
						
							| 
									
										
										
										
											2019-08-19 13:49:32 +01:00
										 |  |  |       if actual_end_commit != end_commit | 
					
						
							| 
									
										
										
										
											2020-10-26 12:16:35 +00:00
										 |  |  |         start_log = Utils.popen_read("git", "log", "-1", "--decorate", "--oneline", start_commit).chomp | 
					
						
							|  |  |  |         end_log = Utils.popen_read("git", "log", "-1", "--decorate", "--oneline", end_commit).chomp | 
					
						
							|  |  |  |         actual_log = Utils.popen_read("git", "log", "-1", "--decorate", "--oneline", actual_end_commit).chomp | 
					
						
							| 
									
										
										
										
											2020-12-03 00:02:22 -05:00
										 |  |  |         odie <<~EOS | 
					
						
							| 
									
										
										
										
											2021-01-24 21:40:41 -05:00
										 |  |  |           `brew update` didn't update #{branch}! | 
					
						
							| 
									
										
										
										
											2020-10-26 12:16:35 +00:00
										 |  |  |           Start commit:        #{start_log} | 
					
						
							|  |  |  |           Expected end commit: #{end_log} | 
					
						
							|  |  |  |           Actual end commit:   #{actual_log} | 
					
						
							| 
									
										
										
										
											2016-08-11 11:24:03 +01:00
										 |  |  |         EOS | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-09-06 23:08:04 +08:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-07-03 15:41:33 +01:00
										 |  |  |   ensure | 
					
						
							| 
									
										
										
										
											2020-07-23 02:58:12 +02:00
										 |  |  |     FileUtils.rm_rf "update-test" unless args.keep_tmp? | 
					
						
							| 
									
										
										
										
											2015-09-06 23:08:04 +08:00
										 |  |  |   end | 
					
						
							|  |  |  | end |