Make the on-disk representation of taps unambiguous

This commit supports "-" and "_" in names of user and repository.

Closes Homebrew/homebrew#28203.

Signed-off-by: Jack Nagel <jacknagel@gmail.com>
This commit is contained in:
Tsukasa OMOTO 2014-04-24 11:26:45 +09:00 committed by Jack Nagel
parent fb27cbe6a7
commit 54004a4759
11 changed files with 95 additions and 53 deletions

View File

@ -61,7 +61,7 @@ module Homebrew extend self
def github_info f def github_info f
if f.path.to_s =~ HOMEBREW_TAP_PATH_REGEX if f.path.to_s =~ HOMEBREW_TAP_PATH_REGEX
user = $1 user = $1
repo = "homebrew-#$2" repo = $2
path = $3 path = $3
else else
user = f.path.parent.cd { github_fork } user = f.path.parent.cd { github_fork }

View File

@ -7,11 +7,6 @@ module Homebrew extend self
SEARCH_ERROR_QUEUE = Queue.new SEARCH_ERROR_QUEUE = Queue.new
# A regular expession to capture the username (one or more char but no `/`,
# which has to be escaped like `\/`), repository, followed by an optional `/`
# and an optional query.
TAP_QUERY_REGEX = /^([^\/]+)\/([^\/]+)\/?(.+)?$/
def search def search
if ARGV.include? '--macports' if ARGV.include? '--macports'
exec_browser "http://www.macports.org/ports.php?by=name&substr=#{ARGV.next}" exec_browser "http://www.macports.org/ports.php?by=name&substr=#{ARGV.next}"
@ -27,17 +22,21 @@ module Homebrew extend self
exec_browser "http://packages.ubuntu.com/search?keywords=#{ARGV.next}&searchon=names&suite=all&section=all" exec_browser "http://packages.ubuntu.com/search?keywords=#{ARGV.next}&searchon=names&suite=all&section=all"
elsif (query = ARGV.first).nil? elsif (query = ARGV.first).nil?
puts_columns Formula.names puts_columns Formula.names
elsif ARGV.first =~ TAP_QUERY_REGEX elsif ARGV.first =~ HOMEBREW_TAP_FORMULA_REGEX
# So look for user/repo/query or list all formulae by the tap # So look for user/repo/query or list all formulae by the tap
# we downcase to avoid case-insensitive filesystem issues. # we downcase to avoid case-insensitive filesystem issues.
user, repo, query = $1.downcase, $2.downcase, $3 user, repo, query = $1.downcase, $2.downcase, $3
tap_dir = HOMEBREW_LIBRARY/"Taps/#{user}-#{repo}" tap_dir = HOMEBREW_LIBRARY/"Taps/#{user}/homebrew-#{repo}"
# If, instead of `user/repo/query` the user wrote `user/repo query`: # If, instead of `user/repo/query` the user wrote `user/repo query`:
query = ARGV[1] if query.nil? query = ARGV[1] if query.nil?
if tap_dir.directory? if tap_dir.directory?
# There is a local tap already: result = ""
result = Dir["#{tap_dir}/*.rb"].map{ |f| File.basename(f).chomp('.rb') } if query
result = result.grep(query_regexp(query)) unless query.nil? tap_dir.find_formula do |child|
basename = child.basename(".rb").to_s
result = basename if basename == query
end
end
else else
# Search online: # Search online:
query = '' if query.nil? query = '' if query.nil?
@ -104,7 +103,7 @@ module Homebrew extend self
end end
def search_tap user, repo, rx def search_tap user, repo, rx
return [] if (HOMEBREW_LIBRARY/"Taps/#{user.downcase}-#{repo.downcase}").directory? return [] if (HOMEBREW_LIBRARY/"Taps/#{user.downcase}/homebrew-#{repo.downcase}").directory?
results = [] results = []
GitHub.open "https://api.github.com/repos/#{user}/homebrew-#{repo}/git/trees/HEAD?recursive=1" do |json| GitHub.open "https://api.github.com/repos/#{user}/homebrew-#{repo}/git/trees/HEAD?recursive=1" do |json|

View File

@ -3,9 +3,11 @@ module Homebrew extend self
def tap def tap
if ARGV.empty? if ARGV.empty?
tapd = HOMEBREW_LIBRARY/"Taps" tapd = HOMEBREW_LIBRARY/"Taps"
tapd.children.each do |tap| tapd.children.each do |user|
# only replace the *last* dash: yes, tap filenames suck next unless user.directory?
puts tap.basename.to_s.reverse.sub('-', '/').reverse if (tap/'.git').directory? user.children.each do |repo|
puts "#{user.basename}/#{repo.basename.sub("homebrew-", "")}" if (repo/".git").directory?
end
end if tapd.directory? end if tapd.directory?
elsif ARGV.first == "--repair" elsif ARGV.first == "--repair"
repair_taps repair_taps
@ -22,12 +24,12 @@ module Homebrew extend self
user = "homebrew" if user == "Homebrew" user = "homebrew" if user == "Homebrew"
# we downcase to avoid case-insensitive filesystem issues # we downcase to avoid case-insensitive filesystem issues
tapd = HOMEBREW_LIBRARY/"Taps/#{user.downcase}-#{repo.downcase}" tapd = HOMEBREW_LIBRARY/"Taps/#{user.downcase}/homebrew-#{repo.downcase}"
return false if tapd.directory? return false if tapd.directory?
abort unless system "git clone https://github.com/#{repouser}/homebrew-#{repo} #{tapd}" abort unless system "git clone https://github.com/#{repouser}/homebrew-#{repo} #{tapd}"
files = [] files = []
tapd.find_formula{ |file| files << tapd.basename.join(file) } tapd.find_formula{ |file| files << tapd.dirname.basename.join(tapd.basename, file) }
link_tap_formula(files) link_tap_formula(files)
puts "Tapped #{files.length} formula" puts "Tapped #{files.length} formula"
@ -88,10 +90,13 @@ module Homebrew extend self
count = 0 count = 0
# check symlinks are all set in each tap # check symlinks are all set in each tap
HOMEBREW_REPOSITORY.join("Library/Taps").children.each do |tap| HOMEBREW_REPOSITORY.join("Library/Taps").children.each do |user|
files = [] next unless user.directory?
tap.find_formula{ |file| files << tap.basename.join(file) } if tap.directory? user.children.each do |repo|
count += link_tap_formula(files) files = []
repo.find_formula{ |file| files << user.basename.join(repo.basename, file) } if repo.directory?
count += link_tap_formula(files)
end
end end
puts "Tapped #{count} formula" puts "Tapped #{count} formula"
@ -100,7 +105,7 @@ module Homebrew extend self
private private
def tap_args def tap_args
ARGV.first =~ %r{^(\S+)/(homebrew-)?(\w+)$} ARGV.first =~ %r{^([\w_-]+)/(homebrew-)?([\w_-]+)$}
raise "Invalid tap name" unless $1 && $3 raise "Invalid tap name" unless $1 && $3
[$1, $3] [$1, $3]
end end
@ -118,7 +123,7 @@ end
class Pathname class Pathname
def tap_ref def tap_ref
case self.to_s case self.to_s
when %r{^#{HOMEBREW_LIBRARY}/Taps/([a-z\-_]+)-(\w+)/(.+)} when %r{^#{HOMEBREW_LIBRARY}/Taps/([\w_-]+)/([\w_-]+)/(.+)}
"#$1/#$2/#{File.basename($3, '.rb')}" "#$1/#$2/#{File.basename($3, '.rb')}"
when %r{^#{HOMEBREW_LIBRARY}/Formula/(.+)} when %r{^#{HOMEBREW_LIBRARY}/Formula/(.+)}
"Homebrew/homebrew/#{File.basename($1, '.rb')}" "Homebrew/homebrew/#{File.basename($1, '.rb')}"

View File

@ -13,14 +13,15 @@ module Homebrew extend self
user.downcase! user.downcase!
repo.downcase! repo.downcase!
tapd = HOMEBREW_LIBRARY/"Taps/#{user}-#{repo}" tapd = HOMEBREW_LIBRARY/"Taps/#{user}/homebrew-#{repo}"
raise "No such tap!" unless tapd.directory? raise "No such tap!" unless tapd.directory?
files = [] files = []
tapd.find_formula{ |file| files << Pathname.new("#{user}-#{repo}").join(file) } tapd.find_formula{ |file| files << Pathname.new("#{user}/homebrew-#{repo}").join(file) }
unlink_tap_formula(files) unlink_tap_formula(files)
rm_rf tapd tapd.rmtree
tapd.dirname.rmdir_if_possible
puts "Untapped #{files.length} formula" puts "Untapped #{files.length} formula"
end end

View File

@ -34,19 +34,23 @@ module Homebrew extend self
end end
report.merge!(master_updater.report) report.merge!(master_updater.report)
Dir["Library/Taps/*"].each do |tapd| # rename Taps directories
next unless File.directory?(tapd) # this procedure will be removed in the future if it seems unnecessasry
rename_taps_dir_if_necessary
cd tapd do Dir["Library/Taps/*/"].each do |user|
begin Dir["#{user}*/"].each do |repo|
updater = Updater.new cd repo do
updater.pull! begin
report.merge!(updater.report) do |key, oldval, newval| updater = Updater.new
oldval.concat(newval) updater.pull!
report.merge!(updater.report) do |key, oldval, newval|
oldval.concat(newval)
end
rescue
repo =~ %r{^Library/Taps/([\w_-]+)/(homebrew-)?([\w_-]+)}
onoe "Failed to update tap: #$1/#$3"
end end
rescue
tapd =~ %r{^Library/Taps/(\w+)-(\w+)}
onoe "Failed to update tap: #$1/#$2"
end end
end end
end end
@ -92,6 +96,39 @@ module Homebrew extend self
raise raise
end end
def rename_taps_dir_if_necessary
need_repair_taps = false
Dir["#{HOMEBREW_LIBRARY}/Taps/*/"].each do |tapd|
begin
tapd_basename = File.basename(tapd)
if File.directory?(tapd + "/.git")
if tapd_basename.include?("-")
# only replace the *last* dash: yes, tap filenames suck
user, repo = tapd_basename.reverse.sub("-", "/").reverse.split("/")
FileUtils.mkdir_p("#{HOMEBREW_LIBRARY}/Taps/#{user.downcase}")
FileUtils.mv(tapd, "#{HOMEBREW_LIBRARY}/Taps/#{user.downcase}/homebrew-#{repo.downcase}")
need_repair_taps = true
if tapd_basename.count("-") >= 2
opoo "Homebrew changed the structure of Taps like <someuser>/<sometap>. "\
+ "So you may need to rename #{HOMEBREW_LIBRARY}/Taps/#{user.downcase}/homebrew-#{repo.downcase} manually."
end
else
opoo "Homebrew changed the structure of Taps like <someuser>/<sometap>. "\
"#{tapd} is incorrect name format. You may need to rename it like <someuser>/<sometap> manually."
end
end
rescue => ex
onoe ex.message
next # next tap directory
end
end
repair_taps if need_repair_taps
end
def load_tap_migrations def load_tap_migrations
require 'tap_migrations' require 'tap_migrations'
rescue LoadError rescue LoadError
@ -184,7 +221,7 @@ class Report < Hash
def tapped_formula_for key def tapped_formula_for key
fetch(key, []).map do |path| fetch(key, []).map do |path|
case path when %r{^Library/Taps/(\w+-\w+/.*)} case path when %r{^Library/Taps/([\w_-]+/[\w_-]+/.*)}
relative_path = $1 relative_path = $1
if valid_formula_location?(relative_path) if valid_formula_location?(relative_path)
Pathname.new(relative_path) Pathname.new(relative_path)
@ -195,7 +232,7 @@ class Report < Hash
def valid_formula_location?(relative_path) def valid_formula_location?(relative_path)
ruby_file = /\A.*\.rb\Z/ ruby_file = /\A.*\.rb\Z/
parts = relative_path.split('/')[1..-1] parts = relative_path.split('/')[2..-1]
[ [
parts.length == 1 && parts.first =~ ruby_file, parts.length == 1 && parts.first =~ ruby_file,
parts.length == 2 && parts.first == 'Formula' && parts.last =~ ruby_file, parts.length == 2 && parts.first == 'Formula' && parts.last =~ ruby_file,
@ -215,8 +252,8 @@ class Report < Hash
fetch(key, []).map do |path| fetch(key, []).map do |path|
case path when %r{^Library/Formula} case path when %r{^Library/Formula}
File.basename(path, ".rb") File.basename(path, ".rb")
when %r{^Library/Taps/(\w+)-(\w+)/(.*)\.rb} when %r{^Library/Taps/([\w_-]+)/(homebrew-)?([\w_-]+)/(.*)\.rb}
"#$1/#$2/#{File.basename(path, '.rb')}" "#$1/#$3/#{File.basename(path, '.rb')}"
end end
end.compact.sort end.compact.sort
end end

View File

@ -66,7 +66,7 @@ class Formula
def repository def repository
@repository ||= begin @repository ||= begin
if path.to_s =~ HOMEBREW_TAP_DIR_REGEX if path.to_s =~ HOMEBREW_TAP_DIR_REGEX
HOMEBREW_REPOSITORY/"Library/Taps/#$1-#$2" HOMEBREW_REPOSITORY/"Library/Taps/#$1/#$2"
else else
HOMEBREW_REPOSITORY HOMEBREW_REPOSITORY
end end

View File

@ -159,7 +159,7 @@ class Formulary
def initialize tapped_name def initialize tapped_name
@tapped_name = tapped_name @tapped_name = tapped_name
user, repo, name = tapped_name.split("/", 3).map(&:downcase) user, repo, name = tapped_name.split("/", 3).map(&:downcase)
tap = Pathname.new("#{HOMEBREW_LIBRARY}/Taps/#{user}-#{repo}") tap = Pathname.new("#{HOMEBREW_LIBRARY}/Taps/#{user}/homebrew-#{repo}")
path = tap.join("#{name}.rb") path = tap.join("#{name}.rb")
if tap.directory? if tap.directory?

View File

@ -85,8 +85,8 @@ HOMEBREW_USER_AGENT = "Homebrew #{HOMEBREW_VERSION} (Ruby #{RUBY_VERSION}-#{RUBY
HOMEBREW_CURL_ARGS = '-f#LA' HOMEBREW_CURL_ARGS = '-f#LA'
HOMEBREW_TAP_FORMULA_REGEX = %r{^(\w+)/(\w+)/([^/]+)$} HOMEBREW_TAP_FORMULA_REGEX = %r{^([\w_-]+)/([\w_-]+)/([\w_-]+)$}
HOMEBREW_TAP_DIR_REGEX = %r{#{HOMEBREW_LIBRARY}/Taps/(\w+)-(\w+)} HOMEBREW_TAP_DIR_REGEX = %r{#{HOMEBREW_LIBRARY}/Taps/([\w_-]+)/([\w_-]+)}
HOMEBREW_TAP_PATH_REGEX = Regexp.new(HOMEBREW_TAP_DIR_REGEX.source \ HOMEBREW_TAP_PATH_REGEX = Regexp.new(HOMEBREW_TAP_DIR_REGEX.source \
+ %r{/(.*)}.source) + %r{/(.*)}.source)

View File

@ -27,10 +27,10 @@ update_git_diff_output_with_formulae_changes: |
:100644 100644 c0084b02faf7ab04abcffb3d5688e9d7c1ff7ae8 25cae981801e21cde048cd28747241d18093ca71 M bin/brew :100644 100644 c0084b02faf7ab04abcffb3d5688e9d7c1ff7ae8 25cae981801e21cde048cd28747241d18093ca71 M bin/brew
update_git_diff_output_with_tapped_formulae_changes: | update_git_diff_output_with_tapped_formulae_changes: |
:100644 100644 9dde0708e2db78e1aa697782bd2ab2ddfd8da984 ee0a81cbe3757e9a4b5aab1c41457f65fed9f258 M Library/Contributions/brew_bash_completion.sh :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/Formula/antiword.rb
:100644 100644 741f11dcd29ec909a94fa54df4bcdd2c1d5bb47b 7535134e865994e8ad66ba02a924a1e6c1271b9e A Library/Taps/someuser-sometap/HomebrewFormula/lua.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/custom-formula.rb
:100644 100644 741f11dcd29ec909a94fa54df4bcdd2c1d5bb47b 7535134e865994e8ad66ba02a924a1e6c1271b9e A Library/Taps/someuser-sometap/lib/not-a-formula.rb :100644 100644 741f11dcd29ec909a94fa54df4bcdd2c1d5bb47b 7535134e865994e8ad66ba02a924a1e6c1271b9e A Library/Taps/someuser/sometap/lib/not-a-formula.rb
update_git_diff_output_with_removed_formulae: | update_git_diff_output_with_removed_formulae: |
:000000 100644 0000000000000000000000000000000000000000 e62589998ef688f64aab10d85c3822dfa5cfb31c A Library/Formula/flac123.rb :000000 100644 0000000000000000000000000000000000000000 e62589998ef688f64aab10d85c3822dfa5cfb31c A Library/Formula/flac123.rb
:100644 100644 bd7d76cf2fa9805ff23b9e8f48ecfb0e569aadd3 0cc2863c9be33ae946268407618f74897961873d M Library/Formula/gdal.rb :100644 100644 bd7d76cf2fa9805ff23b9e8f48ecfb0e569aadd3 0cc2863c9be33ae946268407618f74897961873d M Library/Formula/gdal.rb

View File

@ -85,9 +85,9 @@ class UpdaterTests < Test::Unit::TestCase
perform_update(fixture('update_git_diff_output_with_tapped_formulae_changes')) perform_update(fixture('update_git_diff_output_with_tapped_formulae_changes'))
assert @updater.expectations_met? assert @updater.expectations_met?
assert_equal [ assert_equal [
Pathname('someuser-sometap/Formula/antiword.rb'), Pathname('someuser/sometap/Formula/antiword.rb'),
Pathname('someuser-sometap/HomebrewFormula/lua.rb'), Pathname('someuser/sometap/HomebrewFormula/lua.rb'),
Pathname('someuser-sometap/custom-formula.rb'), Pathname('someuser/sometap/custom-formula.rb'),
], @report.tapped_formula_for(:A) ], @report.tapped_formula_for(:A)
end end

View File

@ -27,7 +27,7 @@ HOMEBREW_WWW = 'http://example.com'
HOMEBREW_CURL_ARGS = '-fsLA' HOMEBREW_CURL_ARGS = '-fsLA'
HOMEBREW_VERSION = '0.9-test' HOMEBREW_VERSION = '0.9-test'
HOMEBREW_TAP_FORMULA_REGEX = %r{^(\w+)/(\w+)/([^/]+)$} HOMEBREW_TAP_FORMULA_REGEX = %r{^([\w_-]+)/([\w_-]+)/([\w_-]+)$}
RUBY_BIN = Pathname.new(RbConfig::CONFIG['bindir']) RUBY_BIN = Pathname.new(RbConfig::CONFIG['bindir'])
RUBY_PATH = RUBY_BIN + RbConfig::CONFIG['ruby_install_name'] + RbConfig::CONFIG['EXEEXT'] RUBY_PATH = RUBY_BIN + RbConfig::CONFIG['ruby_install_name'] + RbConfig::CONFIG['EXEEXT']