From 7b24d1d2d1c5e32b29bce772df37c970764e9d39 Mon Sep 17 00:00:00 2001 From: Uladzislau Shablinski Date: Sat, 22 Oct 2016 01:53:19 +0300 Subject: [PATCH 1/5] download_strategy: keep commit short if possible --- Library/Homebrew/download_strategy.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index 1f77fa20c9..9f9b2ababe 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -832,7 +832,12 @@ class GitHubGitDownloadStrategy < GitDownloadStrategy else return true unless commit return true unless @last_commit.start_with?(commit) - multiple_short_commits_exist?(commit) + if multiple_short_commits_exist?(commit) + true + else + version.update_commit(commit) + false + end end end end From 52526c962b235e16da03415dd78cf911afcf8106 Mon Sep 17 00:00:00 2001 From: Uladzislau Shablinski Date: Tue, 1 Nov 2016 03:53:20 +0300 Subject: [PATCH 2/5] formula: add new methods * `Formula#linked?` returns true if formula linked * `Formula#optlinked?` returns true if formula linked to opt formula installed to the Cellar * `Formula#prefix_linked?` returns true if linked keg points to the prefix passed as an argument * `Formula#linked_version` to get linked version of the formula --- Library/Homebrew/formula.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index bae2e3314f..0c19805a3d 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -553,6 +553,28 @@ class Formula Pathname.new("#{HOMEBREW_CELLAR}/#{name}/#{v}") end + # Is the formula linked? + def linked? + linked_keg.symlink? + end + + # Is the formula linked to opt? + def optlinked? + opt_prefix.symlink? + end + + # Is formula's linked keg points to the prefix. + def prefix_linked?(v = pkg_version) + return false unless linked? + linked_keg.resolved_path == prefix(v) + end + + # {PkgVersion} of the linked keg for the formula. + def linked_version + return unless linked? + Keg.for(linked_keg).version + end + # The parent of the prefix; the named directory in the cellar containing all # installed versions of this software # @private From 73a1daa669b13dca9b0467eb89d755ed7d6116bf Mon Sep 17 00:00:00 2001 From: Uladzislau Shablinski Date: Sat, 22 Oct 2016 01:56:01 +0300 Subject: [PATCH 3/5] cmd/install: allow to install any spec * installing HEAD keg_only should be possible only if: 1. - Old version installed differs from new one and optlinked - `--force` flag is passed - HEAD is seriously outdated or outdated with `--fetch-HEAD` or 2. - Old version installed differs from new one and not optlinked or 3. - This formula is not installed * installing keg_only should be possible only if: 1. - Old version installed differs from new one and optlinked - `--force` flag is passed or 2. - Old version installed differs from new one and not optlinked or 3. - This formula is not installed * installing HEAD should be possible only if: 1. - Old HEAD is seriously outdated or outdated with `--fetch-HEAD` or 2. - HEAD is not installed * installing stable or devel should be possible only if: - Old version installed differs from new one --- Library/Homebrew/cmd/install.rb | 47 ++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/Library/Homebrew/cmd/install.rb b/Library/Homebrew/cmd/install.rb index c3c3b4bb93..700cd9c3d0 100644 --- a/Library/Homebrew/cmd/install.rb +++ b/Library/Homebrew/cmd/install.rb @@ -135,14 +135,45 @@ module Homebrew raise "No devel block is defined for #{f.full_name}" end - current = f if f.installed? - current ||= f.old_installed_formulae.first + installed_head_version = f.latest_head_version + new_head_installed = installed_head_version && + !f.head_version_outdated?(installed_head_version, fetch_head: ARGV.fetch_head?) + prefix_installed = f.prefix.exist? && !f.prefix.children.empty? - if current - msg = "#{current.full_name}-#{current.installed_version} already installed" - unless current.linked_keg.symlink? || current.keg_only? - msg << ", it's just not linked" - puts "You can link formula with `brew link #{f}`" + if f.keg_only? && f.any_version_installed? && f.optlinked? && !ARGV.force? + # keg-only install is only possible when no other version is + # linked to opt, because installing without any warnings can break + # dependencies. Therefore before performing other checks we need to be + # sure --force flag is passed. + opoo "#{f.full_name} is a keg-only and another version is linked to opt." + puts "Use `brew install --force` if you want to install this version" + elsif (ARGV.build_head? && new_head_installed) || prefix_installed + # After we're sure that --force flag is passed for linked to opt + # keg-only we need to be sure that the version we're attempting to + # install is not already installed. + + installed_version = if ARGV.build_head? + f.latest_head_version + else + f.pkg_version + end + + msg = "#{f.full_name}-#{installed_version} already installed" + linked_not_equals_installed = f.linked_version != installed_version + if f.linked? && linked_not_equals_installed + msg << ", however linked version is #{f.linked_version}" + opoo msg + puts "You can use `brew switch #{f} #{installed_version}` to link this version." + elsif !f.linked? || f.keg_only? + msg << ", it's just not linked." + opoo msg + else + opoo msg + end + elsif !f.any_version_installed? && old_formula = f.old_installed_formulae.first + msg = "#{old_formula.full_name}-#{old_formula.installed_version} already installed" + if !old_formula.linked? && !old_formula.keg_only? + msg << ", it's just not linked." end opoo msg elsif f.migration_needed? && !ARGV.force? @@ -152,6 +183,8 @@ module Homebrew puts "You can migrate formula with `brew migrate #{f}`" puts "Or you can force install it with `brew install #{f} --force`" else + # If none of the above is true and the formula is linked, then + # FormulaInstaller will handle this case. formulae << f end end From 6ef96d0be499afbbcdd489784eebe48ac7d6c4e6 Mon Sep 17 00:00:00 2001 From: Uladzislau Shablinski Date: Sun, 27 Nov 2016 05:14:28 +0300 Subject: [PATCH 4/5] Update tests for new install behaviour --- Library/Homebrew/test/install_test.rb | 50 +++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/Library/Homebrew/test/install_test.rb b/Library/Homebrew/test/install_test.rb index 9fe5fff0e2..591d659dcb 100644 --- a/Library/Homebrew/test/install_test.rb +++ b/Library/Homebrew/test/install_test.rb @@ -22,6 +22,56 @@ class IntegrationCommandTestInstall < IntegrationCommandTestCase cmd("install", "testball2") end + def test_install_failures + path = setup_test_formula "testball1", "version \"1.0\"" + devel_content = <<-EOS.undent + version "3.0" + devel do + url "#{Formulary.factory("testball1").stable.url}" + sha256 "#{TESTBALL_SHA256}" + version "2.0" + end + EOS + + assert_match "#{HOMEBREW_CELLAR}/testball1/1.0", cmd("install", "testball1") + + FileUtils.rm path + setup_test_formula "testball1", devel_content + + assert_match "first `brew unlink testball1`", cmd_fail("install", "testball1") + assert_match "#{HOMEBREW_CELLAR}/testball1/1.0", cmd("unlink", "testball1") + assert_match "#{HOMEBREW_CELLAR}/testball1/2.0", cmd("install", "testball1", "--devel") + assert_match "#{HOMEBREW_CELLAR}/testball1/2.0", cmd("unlink", "testball1") + assert_match "#{HOMEBREW_CELLAR}/testball1/3.0", cmd("install", "testball1") + + cmd("switch", "testball1", "2.0") + assert_match "already installed, however linked version is", + cmd("install", "testball1") + assert_match "#{HOMEBREW_CELLAR}/testball1/2.0", cmd("unlink", "testball1") + assert_match "just not linked", cmd("install", "testball1") + end + + def test_install_keg_only_outdated + path_keg_only = setup_test_formula "testball1", <<-EOS.undent + version "1.0" + keg_only "test reason" + EOS + + assert_match "#{HOMEBREW_CELLAR}/testball1/1.0", cmd("install", "testball1") + + FileUtils.rm path_keg_only + setup_test_formula "testball1", <<-EOS.undent + version "2.0" + keg_only "test reason" + EOS + + assert_match "keg-only and another version is linked to opt", + cmd("install", "testball1") + + assert_match "#{HOMEBREW_CELLAR}/testball1/2.0", + cmd("install", "testball1", "--force") + end + def test_install_with_invalid_option setup_test_formula "testball1" assert_match "testball1: this formula has no --with-fo option so it will be ignored!", From a24a919a40f09930c5ef9f2fe811d16e83fa963a Mon Sep 17 00:00:00 2001 From: Uladzislau Shablinski Date: Sat, 3 Dec 2016 04:41:51 +0300 Subject: [PATCH 5/5] install_test: add HEAD tests --- Library/Homebrew/test/install_test.rb | 44 +++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/Library/Homebrew/test/install_test.rb b/Library/Homebrew/test/install_test.rb index 591d659dcb..e047c00303 100644 --- a/Library/Homebrew/test/install_test.rb +++ b/Library/Homebrew/test/install_test.rb @@ -72,6 +72,50 @@ class IntegrationCommandTestInstall < IntegrationCommandTestCase cmd("install", "testball1", "--force") end + def test_install_head_installed + initial_env = ENV.to_hash + %w[AUTHOR COMMITTER].each do |role| + ENV["GIT_#{role}_NAME"] = "brew tests" + ENV["GIT_#{role}_EMAIL"] = "brew-tests@localhost" + ENV["GIT_#{role}_DATE"] = "Thu May 21 00:04:11 2009 +0100" + end + + repo_path = HOMEBREW_CACHE.join("repo") + repo_path.join("bin").mkpath + + repo_path.cd do + shutup do + system "git", "init" + system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo" + FileUtils.touch "bin/something.bin" + FileUtils.touch "README" + system "git", "add", "--all" + system "git", "commit", "-m", "Initial repo commit" + end + end + + setup_test_formula "testball1", <<-EOS.undent + version "1.0" + head "file://#{repo_path}", :using => :git + def install + prefix.install Dir["*"] + end + EOS + + # Ignore dependencies, because we'll try to resolve requirements in build.rb + # and there will be the git requirement, but we cannot instantiate git + # formula since we only have testball1 formula. + assert_match "#{HOMEBREW_CELLAR}/testball1/HEAD-2ccdf4f", cmd("install", "testball1", "--HEAD", "--ignore-dependencies") + assert_match "testball1-HEAD-2ccdf4f already installed", + cmd("install", "testball1", "--HEAD", "--ignore-dependencies") + assert_match "#{HOMEBREW_CELLAR}/testball1/HEAD-2ccdf4f", cmd("unlink", "testball1") + assert_match "#{HOMEBREW_CELLAR}/testball1/1.0", cmd("install", "testball1") + + ensure + ENV.replace(initial_env) + repo_path.rmtree + end + def test_install_with_invalid_option setup_test_formula "testball1" assert_match "testball1: this formula has no --with-fo option so it will be ignored!",