From 9a94a77fa3438277d453674bb745ddcdd639a0a8 Mon Sep 17 00:00:00 2001
From: phinze <paul.t.hinze@gmail.com>
Date: Mon, 8 Jul 2013 19:56:43 -0500
Subject: [PATCH] Properly define tapped formulae in update

A tapped formula is a ruby file present:

  - in the root of the tap
  - in directory of the tap called Formula
  - in a directory of the tap called HomebrewFormula

And nowhere else. This corrects an overzealous definition of tapped
formula in the updater. (the correct definition has been in Pathname
since e613cbe5783cea2abb8100b56c22126a1ab6b9f2)

Refs Homebrew/homebrew#19743.
Closes Homebrew/homebrew#21087.

Signed-off-by: Jack Nagel <jacknagel@gmail.com>
---
 Library/Homebrew/cmd/update.rb                | 15 +++++++++++-
 .../test/fixtures/updater_fixture.yaml        |  6 +++++
 Library/Homebrew/test/test_updater.rb         | 24 +++++++++++++++++++
 3 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/Library/Homebrew/cmd/update.rb b/Library/Homebrew/cmd/update.rb
index 6f4992deee..b3248c5cb3 100644
--- a/Library/Homebrew/cmd/update.rb
+++ b/Library/Homebrew/cmd/update.rb
@@ -146,11 +146,24 @@ class Report < Hash
   def tapped_formula_for key
     fetch(key, []).map do |path|
       case path when %r{^Library/Taps/(\w+-\w+/.*)}
-        Pathname.new($1)
+        relative_path = $1
+        if valid_formula_location?(relative_path)
+          Pathname.new(relative_path)
+        end
       end
     end.compact
   end
 
+  def valid_formula_location?(relative_path)
+    ruby_file = /\A.*\.rb\Z/
+    parts = relative_path.split('/')[1..-1]
+    [
+      parts.length == 1 && parts.first =~ ruby_file,
+      parts.length == 2 && parts.first == 'Formula' && parts.last =~ ruby_file,
+      parts.length == 2 && parts.first == 'HomebrewFormula' && parts.last =~ ruby_file,
+    ].any?
+  end
+
   def new_tapped_formula
     tapped_formula_for :A
   end
diff --git a/Library/Homebrew/test/fixtures/updater_fixture.yaml b/Library/Homebrew/test/fixtures/updater_fixture.yaml
index cbe50d051c..e761db6ed7 100644
--- a/Library/Homebrew/test/fixtures/updater_fixture.yaml
+++ b/Library/Homebrew/test/fixtures/updater_fixture.yaml
@@ -25,3 +25,9 @@ update_git_diff_output_with_formulae_changes: |
   :100644 100644 cc176d388507c7ed9381a36fd42ae0feceaad593 6a7a2385d89a12b5c7369fa0a87980b285ee5fd8 M	Library/Homebrew/utils.rb
   :100644 100644 d1d9c62e6280bcb06c8b884c188d7bd37f8754e3 4aa0473705264cac864bd09f8a9d419d805e4774 M	README
   :100644 100644 c0084b02faf7ab04abcffb3d5688e9d7c1ff7ae8 25cae981801e21cde048cd28747241d18093ca71 M	bin/brew
+update_git_diff_output_with_tapped_formulae_changes: |
+  :100644 100644 9dde0708e2db78e1aa697782bd2ab2ddfd8da984 ee0a81cbe3757e9a4b5aab1c41457f65fed9f258 M	Library/Contributions/brew_bash_completion.sh
+  :100644 100644 741f11dcd29ec909a94fa54df4bcdd2c1d5bb47b 7535134e865994e8ad66ba02a924a1e6c1271b9e A	Library/Taps/someuser-sometap/Formula/antiword.rb
+  :100644 100644 741f11dcd29ec909a94fa54df4bcdd2c1d5bb47b 7535134e865994e8ad66ba02a924a1e6c1271b9e A	Library/Taps/someuser-sometap/HomebrewFormula/lua.rb
+  :100644 100644 741f11dcd29ec909a94fa54df4bcdd2c1d5bb47b 7535134e865994e8ad66ba02a924a1e6c1271b9e A	Library/Taps/someuser-sometap/custom-formula.rb
+  :100644 100644 741f11dcd29ec909a94fa54df4bcdd2c1d5bb47b 7535134e865994e8ad66ba02a924a1e6c1271b9e A	Library/Taps/someuser-sometap/lib/not-a-formula.rb
diff --git a/Library/Homebrew/test/test_updater.rb b/Library/Homebrew/test/test_updater.rb
index 20aba015a7..1464e1084d 100644
--- a/Library/Homebrew/test/test_updater.rb
+++ b/Library/Homebrew/test/test_updater.rb
@@ -102,4 +102,28 @@ class UpdaterTests < Test::Unit::TestCase
       assert_equal %w{ shapelib }, report.select_formula(:R)
     end
   end
+
+  def test_update_homebrew_with_tapped_formula_changes
+    diff_output = fixture('update_git_diff_output_with_tapped_formulae_changes')
+    HOMEBREW_REPOSITORY.cd do
+      updater = UpdaterMock.new
+      updater.in_repo_expect("git checkout -q master")
+      updater.in_repo_expect("git rev-parse -q --verify HEAD", "1234abcd")
+      updater.in_repo_expect("git config core.autocrlf false")
+      updater.in_repo_expect("git pull -q origin refs/heads/master:refs/remotes/origin/master")
+      updater.in_repo_expect("git rev-parse -q --verify HEAD", "3456cdef")
+      updater.in_repo_expect("git diff-tree -r --raw -M85% 1234abcd 3456cdef", diff_output)
+      updater.pull!
+      report = Report.new
+      report.merge!(updater.report)
+
+      assert updater.expectations_met?
+      assert_equal [
+        Pathname('someuser-sometap/Formula/antiword.rb'),
+        Pathname('someuser-sometap/HomebrewFormula/lua.rb'),
+        Pathname('someuser-sometap/custom-formula.rb'),
+      ], report.tapped_formula_for(:A)
+    end
+
+  end
 end
-- 
GitLab