Class: Aigcm::GitDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/aigcm/git_diff.rb

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Constructor Details

#initialize(dir:, commit_hash: nil, amend: false) ⇒ GitDiff

Returns a new instance of GitDiff.



7
8
9
10
11
12
# File 'lib/aigcm/git_diff.rb', line 7

def initialize(dir:, commit_hash: nil, amend: false)
  @dir = dir
  @commit_hash = commit_hash
  @amend = amend
  validate_git_repo
end

Instance Method Details

#generate_diffObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/aigcm/git_diff.rb', line 14

def generate_diff
  Dir.chdir(@dir) do
    cmd = if @amend
        "git diff --cached HEAD^ 2>/dev/null || git diff --cached"
      elsif @commit_hash
        "git diff #{@commit_hash}^..#{@commit_hash}"
      else
        "git diff --cached"
      end

    stdout, _, status = Open3.capture3(cmd)

    raise Error, "Git command failed" unless status.success?
    raise Error, "No changes detected" if stdout.strip.empty?

    stdout
  end
end