Class: RubyLLM::Toolbox::Tools::GitGrep

Inherits:
Base
  • Object
show all
Includes:
GitHelpers
Defined in:
lib/ruby_llm/toolbox/tools/git_grep.rb

Overview

SAFE. Searches the repo’s tracked files with ‘git grep` and returns matching lines with file:line prefixes. Read-only. Faster and cleaner than grep_files in a git repo because it only looks at tracked content and skips binary files.

Constant Summary

Constants included from GitHelpers

RubyLLM::Toolbox::Tools::GitHelpers::GIT_ENV, RubyLLM::Toolbox::Tools::GitHelpers::REF_RE

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods included from GitHelpers

#git_result, #repo_relative, #run_git, #valid_ref?

Methods inherited from Base

#call, exec_tool!, exec_tool?, #initialize, #name

Constructor Details

This class inherits a constructor from RubyLLM::Toolbox::Base

Instance Method Details

#execute(pattern:, path: nil, ignore_case: false, fixed: false) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby_llm/toolbox/tools/git_grep.rb', line 33

def execute(pattern:, path: nil, ignore_case: false, fixed: false)
  pat = pattern.to_s
  return error("pattern must not be empty", code: :empty_pattern) if pat.empty?

  rel = repo_relative(path)
  args = ["grep", "-n", "-I", "--no-color"]
  args << "-i" if ignore_case
  args << "-F" if fixed
  args += ["-e", pat] # -e guards against a pattern that begins with "-"
  args += ["--", rel] if rel

  out, err, status = run_git(*args)
  interpret(out, err, status)
rescue Safety::PathJail::Jailbreak => e
  error(e.message, code: :path_denied)
end