<commit_before>module TestLauncher
  module Searchers
    class Git

      def initialize(shell)
        @shell = shell
        Dir.chdir(root_path)
      end

      def find_files(pattern)
        shell.run("git ls-files '*#{pattern}*'").map {|f| system_path(f)}
      end

      def grep(regex, file_pattern: '*')
        results = shell.run("git grep --untracked --extended-regexp '#{regex}' -- '#{file_pattern}'")
        results.map do |result|
          interpret_grep_result(result)
        end
      end

      private

      def interpret_grep_result(grep_result)
        splits = grep_result.split(/:/)
        file = splits.shift.strip
        # we rejoin on ':' because our
        # code may have colons inside of it.
        #
        # example:
        # path/to/file: run_method(a: A, b: B)
        #
        # so shift the first one out, then
        # rejoin the rest
        line = splits.join(':').strip

        {
          :file => system_path(file),
          :line => line,
        }
      end

      def system_path(file)
        File.join(root_path, file)
      end

      def root_path
        @root_path ||= %x[ git rev-parse --show-toplevel ].chomp
      end

      def shell
        @shell
      end
    end
  end
end<commit_msg>Add better messaging around using a git repo<commit_after>module TestLauncher
  module Searchers
    class Git

      def self.valid?

      end

      def initialize(shell)
        @shell = shell
        Dir.chdir(root_path)
      end

      def find_files(pattern)
        shell.run("git ls-files '*#{pattern}*'").map {|f| system_path(f)}
      end

      def grep(regex, file_pattern: '*')
        results = shell.run("git grep --untracked --extended-regexp '#{regex}' -- '#{file_pattern}'")
        results.map do |result|
          interpret_grep_result(result)
        end
      end

      private

      def interpret_grep_result(grep_result)
        splits = grep_result.split(/:/)
        file = splits.shift.strip
        # we rejoin on ':' because our
        # code may have colons inside of it.
        #
        # example:
        # path/to/file: run_method(a: A, b: B)
        #
        # so shift the first one out, then
        # rejoin the rest
        line = splits.join(':').strip

        {
          :file => system_path(file),
          :line => line,
        }
      end

      def system_path(file)
        File.join(root_path, file)
      end

      def root_path
        @root_path ||= %x[ git rev-parse --show-toplevel ].chomp

        if $? != 0
          shell.warn "test_launcher must be used in a git repository"
          exit
        end
      end

      def shell
        @shell
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10703703703703704, 'fraction_numerical': 0.00037037037037037035, 'mean_word_length': 1.6769078295341924, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '795120', 'n_tokens_mistral': 863, 'n_tokens_neox': 822, 'n_words': 220}