Class: OllamaAgent::Indexing::DiffSummarizer

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama_agent/indexing/diff_summarizer.rb

Overview

Parses and summarizes unified diffs. Provides human-readable change summaries and statistics without needing the full file contents.

Defined Under Namespace

Classes: FileDiff

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(diff) ⇒ Array<FileDiff>

Parse a unified diff string and return structured FileDiff objects.

Parameters:

  • diff (String)

    unified diff content

Returns:



14
15
16
# File 'lib/ollama_agent/indexing/diff_summarizer.rb', line 14

def self.parse(diff)
  new.parse(diff)
end

.summarize(diff) ⇒ String

Return a short human-readable summary of a diff.

Parameters:

  • diff (String)

Returns:

  • (String)


21
22
23
# File 'lib/ollama_agent/indexing/diff_summarizer.rb', line 21

def self.summarize(diff)
  new.summarize(diff)
end

Instance Method Details

#one_liner(diff) ⇒ Object

Brief one-liner for embedding in prompts.



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ollama_agent/indexing/diff_summarizer.rb', line 89

def one_liner(diff)
  parsed = parse(diff)
  return "empty diff" if parsed.empty?

  paths   = parsed.map(&:path).first(3).join(", ")
  suffix  = parsed.size > 3 ? " (+#{parsed.size - 3} more)" : ""
  total_a = parsed.sum(&:additions)
  total_d = parsed.sum(&:deletions)

  "#{paths}#{suffix} [+#{total_a}/-#{total_d}]"
end

#parse(diff) ⇒ Object



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
# File 'lib/ollama_agent/indexing/diff_summarizer.rb', line 25

def parse(diff)
  return [] if diff.nil? || diff.strip.empty?

  file_diffs = []
  current    = nil

  diff.each_line do |line|
    line = line.rstrip

    if line.start_with?("diff --git ")
      file_diffs << current if current
      current = new_file_diff(line)

    elsif line.start_with?("new file mode")
      current[:is_new] = true if current

    elsif line.start_with?("deleted file mode")
      current[:is_deleted] = true if current

    elsif line.start_with?("rename to ")
      current[:is_rename] = true if current

    elsif line.start_with?("--- ") || line.start_with?("+++ ")
      next # skip --- +++ headers

    elsif line.start_with?("@@")
      current[:hunks] += 1 if current

    elsif line.start_with?("+") && !line.start_with?("+++")
      current[:additions] += 1 if current

    elsif line.start_with?("-") && !line.start_with?("---")
      current[:deletions] += 1 if current
    end
  end

  file_diffs << current if current
  file_diffs.compact.map { |d| build_entry(d) }
end

#summarize(diff) ⇒ Object

Human-readable multi-line summary.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ollama_agent/indexing/diff_summarizer.rb', line 66

def summarize(diff)
  parsed = parse(diff)
  return "Empty diff" if parsed.empty?

  total_add = parsed.sum(&:additions)
  total_del = parsed.sum(&:deletions)
  header    = "#{parsed.size} file(s) changed: +#{total_add} -#{total_del}"

  lines = [header]
  parsed.each do |fd|
    tag = if fd.is_new then "[new]"
          elsif fd.is_deleted then "[deleted]"
          elsif fd.is_rename  then "[renamed]"
          else                     ""
          end

    lines << "  #{fd.path} #{tag} +#{fd.additions} -#{fd.deletions}"
  end

  lines.join("\n")
end