Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search back through renames [FCOM-4] #639

Merged
merged 4 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## Unreleased
[no unreleased changes yet]
- Search back through renames

## v0.10.0 (2024-06-28)
- Enforce only major and minor parts of required Ruby version (loosening the
Expand Down
113 changes: 83 additions & 30 deletions lib/fcom/querier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# 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.
class Fcom::Querier
prepend MemoWise
include ::Fcom::OptionsHelpers

def initialize(options)
Expand All @@ -13,6 +14,7 @@ def initialize(options)

# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
# rubocop:disable Metrics/MethodLength
def query
expression_to_match = search_string
expression_to_match = Regexp.escape(expression_to_match).gsub('\\ ', ' ') unless regex_mode?
Expand All @@ -24,37 +26,88 @@ def query

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

command = <<~COMMAND.squish
git log
--format="commit %s|%H|%an|%cr (%ci)"
--patch
--full-diff
--no-textconv
#{%(--author="#{author}") if author}
#{"--since=#{days}.day" unless days.nil?}
--
#{path}
|

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

#{'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}
--parse-mode
--repo #{repo}
COMMAND

Fcom.logger.debug("Executing command: #{command}")
system(command)
commands =
filename_by_most_recent_containing_commit.map do |commit, path_at_commit|
<<~COMMAND.squish
git log
--format="commit %s|%H|%an|%cr (%ci)"
--patch
--full-diff
--no-textconv
#{%(--author="#{author}") if author}
#{"--since=#{days}.day" unless days.nil?}
#{commit}
--
#{path_at_commit}
|

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

#{'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

previous_command_generated_output = false

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

if output.empty?
previous_command_generated_output = false
else
if previous_command_generated_output
puts("\n\n") # print blank lines for spacing
end

previous_command_generated_output = true

puts(output)
end
end
end
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Metrics/CyclomaticComplexity

private

memo_wise \
def filename_by_most_recent_containing_commit
{
most_recent_commit_with_file => path,
}.merge(renames.transform_keys { "#{_1}^" })
end

memo_wise \
def most_recent_commit_with_file
if system(%(test -e "#{path}"))
'HEAD'
else
`git log --all -1 --format="%H" -- "#{path}"`.rstrip
end
end

memo_wise \
def renames
`git log HEAD --format=%H --name-status --follow --diff-filter=R -- '#{path}'`.
split(/\n(?=[0-9a-f]{40})/).
to_h do |sha_and_name_info|
sha_and_name_info.
match(/(?<sha>[0-9a-f]{40})\n\nR\d+\s+(?<previous_name>\S+)?/).
named_captures.
values_at('sha', 'previous_name')
end
end
end
82 changes: 50 additions & 32 deletions spec/fcom/querier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,58 @@
describe '#query' do
subject(:query) { querier.query }

it 'executes a #system call with the expected command' do
# rubocop:disable RSpec/AnyInstance
expect_any_instance_of(Kernel).
to receive(:system).
with(<<~COMMAND.squish)
git log --format="commit %s|%H|%an|%cr (%ci)" --patch --full-diff --no-textconv -- . |
rg "(the_search_string)|(^commit )|(^diff )" --color never |
fcom "the_search_string" --path . --parse-mode --repo testuser/testrepo
COMMAND
# rubocop:enable RSpec/AnyInstance

query
end
context 'when a system call indicates that the current directory exists' do
before do
expect(querier).
to receive(:system).
with('test -e "."').
and_return(true)
end

context 'when an author option is provided' do
let(:options) { stubbed_slop_options('the_search_string --author "David Runger"') }
context 'when there have been no file renames' do
before do
expect(querier).
to receive(:`).
with("git log HEAD --format=%H --name-status --follow --diff-filter=R -- '.'").
and_return('')
end

it 'executes a #system call with the expected command' do
# rubocop:disable RSpec/AnyInstance
expect_any_instance_of(Kernel).
to receive(:system).
with(<<~COMMAND.squish)
git log
--format="commit %s|%H|%an|%cr (%ci)"
--patch --full-diff --no-textconv
--author="David Runger"
-- .
|
rg "(the_search_string)|(^commit )|(^diff )" --color never |
fcom "the_search_string" --path . --parse-mode --repo testuser/testrepo
COMMAND
# rubocop:enable RSpec/AnyInstance

query
it 'executes a backticks system call with the expected command' do
expect(querier).
to receive(:`).
with(<<~COMMAND.squish).
git log --format="commit %s|%H|%an|%cr (%ci)" --patch --full-diff --no-textconv
HEAD -- . |
rg "(the_search_string)|(^commit )|(^diff )" --color never |
fcom "the_search_string" --path . --parse-mode --repo testuser/testrepo
COMMAND
and_return('')

query
end

context 'when an author option is provided' do
let(:options) { stubbed_slop_options('the_search_string --author "David Runger"') }

it 'executes a backticks system call with the expected command' do
expect(querier).
to receive(:`).
with(<<~COMMAND.squish).
git log
--format="commit %s|%H|%an|%cr (%ci)"
--patch --full-diff --no-textconv
--author="David Runger"
HEAD
-- .
|
rg "(the_search_string)|(^commit )|(^diff )" --color never |
fcom "the_search_string" --path . --parse-mode --repo testuser/testrepo
COMMAND
and_return('')

query
end
end
end
end
end
Expand Down