diff --git a/Library/Homebrew/cmd/list.rb b/Library/Homebrew/cmd/list.rb
index 4fdd8a85e67afd9d4b82a3cf96609406bde2ac92..d2eebb5f1b48ecc7790e1e7c6514a99f3ae1613b 100644
--- a/Library/Homebrew/cmd/list.rb
+++ b/Library/Homebrew/cmd/list.rb
@@ -140,6 +140,8 @@ class PrettyListing
           # dylibs have multiple symlinks and we don't care about them
           (pnn.extname == ".dylib" || pnn.extname == ".pc") && !pnn.symlink?
         end
+      when ".brew"
+        # Ignore .brew
       else
         if pn.directory?
           if pn.symlink?
diff --git a/Library/Homebrew/formula_installer.rb b/Library/Homebrew/formula_installer.rb
index 3738c378432b34699574d9b98e5c7dc4f4517ceb..652f87ae3758a35d6e295c8c8d0f73739e538a36 100644
--- a/Library/Homebrew/formula_installer.rb
+++ b/Library/Homebrew/formula_installer.rb
@@ -259,6 +259,12 @@ class FormulaInstaller
       compute_and_install_dependencies if not_pouring && !ignore_deps?
       build
       clean
+
+      # Store the formula used to build the keg in the keg.
+      s = formula.path.read.gsub(/  bottle do.+?end\n\n?/m, "")
+      brew_prefix = formula.prefix/".brew"
+      brew_prefix.mkdir
+      Pathname(brew_prefix/"#{formula.name}.rb").atomic_write(s)
     end
 
     build_bottle_postinstall if build_bottle?
diff --git a/Library/Homebrew/test/test_formula_installer.rb b/Library/Homebrew/test/test_formula_installer.rb
index da0404c359839fda276cfca9cbc3c5de6c109727..5b937d1df39a41adf03c97dd725a2e4691b5632f 100644
--- a/Library/Homebrew/test/test_formula_installer.rb
+++ b/Library/Homebrew/test/test_formula_installer.rb
@@ -58,6 +58,7 @@ class InstallTests < Homebrew::TestCase
       bin = HOMEBREW_PREFIX+"bin"
       assert_predicate bin, :directory?
       assert_equal 3, bin.children.length
+      assert_predicate f.prefix/".brew/testball.rb", :readable?
     end
   end