<commit_before>require "formula"
require "blacklist"

module Homebrew extend self
  def search
    if ARGV.include? '--macports'
      exec "open", "http://www.macports.org/ports.php?by=name&substr=#{ARGV.next}"
    elsif ARGV.include? '--fink'
      exec "open", "http://pdb.finkproject.org/pdb/browse.php?summary=#{ARGV.next}"
    else
      query = ARGV.first
      rx = case query
      when nil then ""
      when %r{^/(.*)/$} then Regexp.new($1)
      else
        /.*#{Regexp.escape query}.*/i
      end

      search_results = search_brews rx
      puts_columns search_results

      if not query.to_s.empty? and $stdout.tty? and msg = blacklisted?(query)
        unless search_results.empty?
          puts
          puts "If you meant `#{query}' precisely:"
          puts
        end
        puts msg
      end

      $found = search_results.length
      puts_columns search_tap "adamv", "alt", rx
      puts_columns search_tap "josegonzalez", "php", rx
      puts_columns search_tap "Homebrew", "versions", rx
      puts_columns search_tap "Homebrew", "dupes", rx

      if $found == 0 and not blacklisted? query
        puts "No formula found for \"#{query}\". Searching open pull requests..."
        GitHub.find_pull_requests(rx) { |pull| puts pull }
      end
    end
  end

  def search_tap user, repo, rx
    return [] if (HOMEBREW_LIBRARY/"Taps/#{user}-#{repo}").directory?

    require 'open-uri'
    require 'yaml'

    results = []
    open "http://github.com/api/v2/yaml/blob/all/#{user}/homebrew-#{repo}/master" do |f|
      YAML::load(f.read)["blobs"].each do |file, _|
        name = File.basename(file, '.rb')
        if file =~ /\.rb$/ and name =~ rx
          results << "#{user}/#{repo}/#{name}"
          $found += 1
        end
      end
    end
    results
  rescue
    []
  end

  def search_brews rx<commit_msg>Special case "Homebrew" in brew-search<commit_after>require "formula"
require "blacklist"

module Homebrew extend self
  def search
    if ARGV.include? '--macports'
      exec "open", "http://www.macports.org/ports.php?by=name&substr=#{ARGV.next}"
    elsif ARGV.include? '--fink'
      exec "open", "http://pdb.finkproject.org/pdb/browse.php?summary=#{ARGV.next}"
    else
      query = ARGV.first
      rx = case query
      when nil then ""
      when %r{^/(.*)/$} then Regexp.new($1)
      else
        /.*#{Regexp.escape query}.*/i
      end

      search_results = search_brews rx
      puts_columns search_results

      if not query.to_s.empty? and $stdout.tty? and msg = blacklisted?(query)
        unless search_results.empty?
          puts
          puts "If you meant `#{query}' precisely:"
          puts
        end
        puts msg
      end

      $found = search_results.length
      puts_columns search_tap "adamv", "alt", rx
      puts_columns search_tap "josegonzalez", "php", rx
      puts_columns search_tap "Homebrew", "versions", rx
      puts_columns search_tap "Homebrew", "dupes", rx

      if $found == 0 and not blacklisted? query
        puts "No formula found for \"#{query}\". Searching open pull requests..."
        GitHub.find_pull_requests(rx) { |pull| puts pull }
      end
    end
  end

  def search_tap user, repo, rx
    return [] if (HOMEBREW_LIBRARY/"Taps/#{user}-#{repo}").directory?

    require 'open-uri'
    require 'yaml'

    results = []
    open "http://github.com/api/v2/yaml/blob/all/#{user}/homebrew-#{repo}/master" do |f|
      user.downcase! if user == "Homebrew" # special handling for the Homebrew organization
      YAML::load(f.read)["blobs"].each do |file, _|
        name = File.basename(file, '.rb')
        if file =~ /\.rb$/ and name =~ rx
          results << "#{user}/#{repo}/#{name}"
          $found += 1
        end
      end
    end
    results
  rescue
    []
  end

  def search_brews rx
{'fraction_non_alphanumeric': 0.13914413231819375, 'fraction_numerical': 0.002100288789708585, 'mean_word_length': 2.3333333333333335, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3284486', 'n_tokens_mistral': 1282, 'n_tokens_neox': 1265, 'n_words': 321}