<commit_before>class Spork::Diagnoser
  class << self
    def loaded_files
      @loaded_files ||= {}
    end
    
    def install_hook!(entry_file = nil, dir = Dir.pwd)
      @dir = File.expand_path(Dir.pwd, dir)
      @entry_file = entry_file
      
      Kernel.class_eval do
        alias :require_without_diagnoser :require
        alias :load_without_diagnoser :load
        
        def require(string)
          ::Spork::Diagnoser.add_included_file(string, caller)
          require_without_diagnoser(string)
        end
        
        def load(string)
          ::Spork::Diagnoser.add_included_file(string, caller)
          load_without_diagnoser(string)
        end
      end
    end
    
    def add_included_file(filename, callstack)
      filename = expand_filename(filename)
      return unless File.exist?(filename)
      loaded_files[filename] = filter_callstack(caller) if subdirectory?(filename)
    end
    
    def remove_hook!
      return unless Kernel.private_instance_methods.include?('require_without_diagnoser')
      Kernel.class_eval do
        alias :require :require_without_diagnoser
        alias :load :load_without_diagnoser
        
        undef_method(:require_without_diagnoser)
        undef_method(:load_without_diagnoser)
      end
      true
    end
    
    def output_results(stdout)
      project_prefix = Dir.pwd + "/"
      minimify = lambda { |f| f.gsub(project_prefix, '')}
      stdout.puts "- Spork Diagnosis -\n"
      stdout.puts "-- Summary --"
      stdout.puts loaded_files.keys.sort.map(&minimify)
      stdout.puts "\n\n\n"
      stdout.puts "-- Detail --"
      loaded_files.keys.sort.each do |file|
        stdout.puts "\n\n\n--- #{minimify.call(file)} ---\n"
        stdout.puts loaded_files[file].map(&minimify)
      end
    end
    
    private
      def filter_callstack(callstack, entry_file = @entry_file)
        callstack = callstack.select { |f| ! f.include?('lib/spork/diagnoser.rb')} 
        callstack.pop until callstack.empty? || callstack.last.include?(@entry_file) if @entry_file
        callstack
      end
    
      def expand_filename(filename)
        ([Dir.pwd] + $:).each do |attempted_path|
          attempted_filename = File.expand_path(filename, attempted_path)
          return attempted_filename if File.file?(attempted_filename)
          attempted_filename = attempted_filename + ".rb"
          return attempted_filename if File.file?(attempted_filename)
        end
        filename
      end
    
      def subdirectory?(directory)
        File.expand_path(directory, Dir.pwd).include?(@dir)<commit_msg>make diagnoser leave less footprint on the callstack<commit_after>class Spork::Diagnoser
  class << self
    def loaded_files
      @loaded_files ||= {}
    end
    
    def install_hook!(entry_file = nil, dir = Dir.pwd)
      @dir = File.expand_path(Dir.pwd, dir)
      @entry_file = entry_file
      
      Kernel.class_eval do
        alias :require_without_diagnoser :require
        alias :load_without_diagnoser :load
        
        def require(string)
          ::Spork::Diagnoser.add_included_file(string, caller)
          require_without_diagnoser(string)
        end
        
        def load(string)
          ::Spork::Diagnoser.add_included_file(string, caller)
          load_without_diagnoser(string)
        end
      end
    end
    
    def add_included_file(filename, callstack)
      filename = expand_filename(filename)
      return unless File.exist?(filename)
      loaded_files[filename] = filter_callstack(caller) if subdirectory?(filename)
    end
    
    def remove_hook!
      return unless Kernel.private_instance_methods.include?('require_without_diagnoser')
      Kernel.class_eval do
        alias :require :require_without_diagnoser
        alias :load :load_without_diagnoser
        
        undef_method(:require_without_diagnoser)
        undef_method(:load_without_diagnoser)
      end
      true
    end
    
    def output_results(stdout)
      project_prefix = Dir.pwd + "/"
      minimify = lambda { |f| f.gsub(project_prefix, '')}
      stdout.puts "- Spork Diagnosis -\n"
      stdout.puts "-- Summary --"
      stdout.puts loaded_files.keys.sort.map(&minimify)
      stdout.puts "\n\n\n"
      stdout.puts "-- Detail --"
      loaded_files.keys.sort.each do |file|
        stdout.puts "\n\n\n--- #{minimify.call(file)} ---\n"
        stdout.puts loaded_files[file].map(&minimify)
      end
    end
    
    private
      def filter_callstack(callstack, entry_file = @entry_file)
        callstack.pop until callstack.empty? || callstack.last.include?(@entry_file) if @entry_file
        callstack.map do |line|
          next if line.include?('lib/spork/diagnoser.rb')
          line.gsub!('require_without_diagnoser', 'require')
          line
        end.compact
      end
    
      def expand_filename(filename)
        ([Dir.pwd] + $:).each do |attempted_path|
          attempted_filename = File.expand_path(filename, attempted_path)
          return attempted_filename if File.file?(attempted_filename)
          attempted_filename = attempted_filename + ".rb"
          return attempted_filename if File.file?(attempted_filename)
        end
        filename
      end
    
      def subdirectory?(directory)
        File.expand_path(directory, Dir.pwd).include?(@dir)
{'fraction_non_alphanumeric': 0.09690140845070423, 'fraction_numerical': 0.0, 'mean_word_length': 2.4651919323357188, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5965913', 'n_tokens_mistral': 1653, 'n_tokens_neox': 1597, 'n_words': 303}