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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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?('"') ? "'" : '"'

  commands =
    filename_by_most_recent_containing_commit.map do |commit, path_at_commit|
      commit_without_caret = commit.first(40)

      start_of_log =
        filtered_renames.
          drop_while { it.first != commit_without_caret }.
          detect { it.last == path_at_commit }&.
          first ||
        first_commit_sha

      <<~COMMAND.squish
        git rev-list #{start_of_log} #{commit} |

        git log
          --format="commit %s|%H|%an|%cr (%ci)"
          --patch
          --full-diff
          --diff-algorithm=default
          --topo-order
          --no-textconv
          --stdin
          #{%(--author="#{author}") if author}
          #{days_limiter}
          --
          #{path_at_commit}
          |

        rg #{quote}(#{expression_to_match})|(^commit )|(^diff )#{quote}
          --color never
          #{'--ignore-case' if ignore_case?}
          #{@options[:rg_options]}
          |

        #{File.join(__dir__, '../../exe/') if development?}fcom #{quote}#{search_string}#{quote}
          #{"--days #{days}" if days}
          #{'--regex' if regex_mode?}
          #{'--debug' if debug?}
          #{'--ignore-case' if ignore_case?}
          --path #{path_at_commit}
          --parse-mode
          --repo #{repo}
      COMMAND
    end

  a_previous_command_generated_output_not_yet_spaced = false

  commands.each do |command|
    Fcom.logger.debug("Executing command: #{command}")

    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 { puts(it) }
      end
    rescue Errno::EIO
      # The command produced no output.
    end
  end
end