Class: RubyCoded::Tools::GitBaseTool

Inherits:
BaseTool
  • Object
show all
Defined in:
lib/ruby_coded/tools/git_base_tool.rb

Overview

Shared helpers for git-specific tools.

Direct Known Subclasses

GitAddTool, GitCommitTool, GitDiffTool, GitStatusTool

Constant Summary collapse

GIT_ENV =
{
  "GIT_EDITOR" => "true",
  "EDITOR" => "true",
  "VISUAL" => "true",
  "GIT_PAGER" => "cat",
  "PAGER" => "cat"
}.freeze
MAX_OUTPUT_CHARS =
5000

Constants inherited from BaseTool

BaseTool::CONFIRM_RISK, BaseTool::DANGEROUS_RISK, BaseTool::SAFE_RISK

Instance Method Summary collapse

Methods inherited from BaseTool

#initialize

Constructor Details

This class inherits a constructor from RubyCoded::Tools::BaseTool

Instance Method Details

#ensure_git_repo!Object



25
26
27
28
29
# File 'lib/ruby_coded/tools/git_base_tool.rb', line 25

def ensure_git_repo!
  return nil if git_repo?

  { error: "Not a git repository: #{@project_root}" }
end

#format_git_result(stdout, stderr, status) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ruby_coded/tools/git_base_tool.rb', line 43

def format_git_result(stdout, stderr, status)
  output = String.new
  output << stdout unless stdout.empty?
  output << "\nSTDERR:\n#{stderr}" unless stderr.empty?
  output = output.strip
  output = "(no output)" if output.empty?
  output = truncate_output(output)

  return output if status.success?

  { error: output }
end

#git_repo?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/ruby_coded/tools/git_base_tool.rb', line 21

def git_repo?
  Dir.exist?(File.join(@project_root, ".git"))
end

#run_git_commandObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ruby_coded/tools/git_base_tool.rb', line 31

def run_git_command(*)
  repo_error = ensure_git_repo!
  return repo_error if repo_error

  stdout, stderr, status = Open3.capture3(GIT_ENV, "git", *, chdir: @project_root)
  format_git_result(stdout, stderr, status)
rescue Errno::ENOENT => e
  { error: "Git executable not found: #{e.message}" }
rescue StandardError => e
  { error: "Git command failed: #{e.message}" }
end

#shell_join(parts) ⇒ Object



62
63
64
# File 'lib/ruby_coded/tools/git_base_tool.rb', line 62

def shell_join(parts)
  parts.map { |part| Shellwords.escape(part.to_s) }.join(" ")
end

#truncate_output(output) ⇒ Object



56
57
58
59
60
# File 'lib/ruby_coded/tools/git_base_tool.rb', line 56

def truncate_output(output)
  return output if output.length <= MAX_OUTPUT_CHARS

  "#{output[0, MAX_OUTPUT_CHARS]}...(truncated, #{output.length} total characters)"
end