Class: Fcom::Querier

Inherits:
Object
  • Object
show all
Includes:
OptionsHelpers, MemoWise
Defined in:
lib/fcom/querier.rb

Overview

This class executes a system command to retrieve the git history, which is passed through rg (ripgrep), and then ultimately is fed back to fcom for parsing.

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Querier

Returns a new instance of Querier.



14
15
16
# File 'lib/fcom/querier.rb', line 14

def initialize(options)
  @options = options
end

Instance Method Details

#queryObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fcom/querier.rb', line 18

def query
  expression_to_match = search_string
  expression_to_match = Regexp.escape(expression_to_match).gsub('\\ ', ' ') unless regex_mode?

  if expression_to_match.nil? || expression_to_match.empty?
    puts('provide expression to match as first argument')
    exit
  end

  quote = expression_to_match.include?('"') ? "'" : '"'

  a_previous_command_generated_output_not_yet_spaced = false
  remaining_commits = commits

  filename_by_most_recent_containing_commit.each do |commit, path_at_commit|
    break if remaining_commits && remaining_commits <= 0

    command = query_command(commit, path_at_commit, expression_to_match, quote, remaining_commits)
    Fcom.logger.debug("Executing command: #{command}")

    commits_printed = 0
    PTY.spawn(command) do |stdout, _stdin, _pid|
      # Read first byte to detect any output
      first_byte = stdout.read(1)

      if first_byte
        # Add spacing if needed
        if a_previous_command_generated_output_not_yet_spaced
          print "\n\n"
        end

        a_previous_command_generated_output_not_yet_spaced = true

        print(first_byte)

        # Now read the rest line by line
        stdout.each_line do |line|
          puts(line)
          commits_printed += 1 if line.chomp == Fcom::Parser::COMMIT_HEADER_SEPARATOR
        end
      end
    rescue Errno::EIO
      # The command produced no output.
    end

    remaining_commits -= commits_printed if remaining_commits
  end
end