build_greps

in lib/gitlab_git/repository.rb [1175:1239]


      def build_greps(file_contents, query, ref, filename)
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        lines_with_index = Enumerator.new do |yielder|
          
          (-SEARCH_CONTEXT_LINES..-1).each do |i|
            yielder.yield [nil, i]
          end

          
          count = 0
          file_contents.each_line do |line|
            line.chomp!
            yielder.yield [line, count]
            count += 1
          end

          
          (count + 1..count + SEARCH_CONTEXT_LINES).each do |i|
            yielder.yield [nil, i]
          end
        end

        greps = []

        
        lines_with_index.each_cons(2 * SEARCH_CONTEXT_LINES + 1) do |line_block|
          
          line, _ = line_block[SEARCH_CONTEXT_LINES]

          next unless line && line.match(/

          
          
          match_with_context = line_block.map(&:first)
          
          match_with_context.compact!

          
          first_context_line_number = line_block[0][1] + 1

          greps << Gitlab::Git::BlobSnippet.new(
            ref,
            match_with_context,
            first_context_line_number,
            filename
          )
        end

        greps
      end