grep

in lib/rugged_adapter/git_layer_rugged.rb [166:185]


      def grep(query, options={})
        ref = options[:ref] ? options[:ref] : "HEAD"
        tree = @repo.lookup(sha_from_ref(ref)).tree
        tree = @repo.lookup(tree[options[:path]][:oid]) if options[:path]
        enc = options.fetch(:encoding, 'utf-8')
        results = []
        tree.walk_blobs(:postorder) do |root, entry|
          blob = @repo.lookup(entry[:oid])
          count = 0
          next if blob.binary?
          blob.content.force_encoding(enc).each_line do |line|
            next unless line.match(/
            count += 1
          end
          path = options[:path] ? ::File.join(options[:path], root, entry[:name]) : "#{root}#{entry[:name]}"
          results << {:name => path, :count => count} unless count == 0
        end
        results
      end