Module: OllamaAgent::Runtime::UnifiedDiffApply

Defined in:
lib/ollama_agent/runtime/unified_diff_apply.rb

Overview

Pure-Ruby application of a unified diff to a single file (no shell). Supports typical git-style hunks; multiple hunks are applied bottom-up.

Defined Under Namespace

Classes: Hunk

Class Method Summary collapse

Class Method Details

.apply(old_content, diff_text) ⇒ :ok, String

rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity – diff engine

Parameters:

  • old_content (String)

    current file bytes (UTF-8 or binary)

  • diff_text (String)

    unified diff (may include headers)

Returns:

  • (:ok, String)

    or [:error, String]



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ollama_agent/runtime/unified_diff_apply.rb', line 22

def apply(old_content, diff_text)
  hunks = parse_hunks(diff_text.to_s)
  return [:error, "no hunk in patch"] if hunks.empty?

  lines = to_line_array(old_content)
  had_trailing_nl = old_content.end_with?("\n")
  sorted = hunks.sort_by { |hunk| -hunk.old_start }
  sorted.each do |hunk|
    lines = apply_hunk(lines, hunk)
    return [:error, "patch did not apply cleanly"] unless lines
  end

  out = lines.join("\n")
  out += "\n" if had_trailing_nl || !old_content.empty?
  [:ok, out]
end

.apply_hunk(lines, hunk) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ollama_agent/runtime/unified_diff_apply.rb', line 73

def apply_hunk(lines, hunk)
  work = lines.dup
  idx = hunk.old_start - 1
  return nil if idx.negative?

  hunk.body.each do |raw|
    op = raw[0]
    text = raw[1..]
    case op
    when " "
      return nil unless work[idx] == text

      idx += 1
    when "-"
      return nil unless work[idx] == text

      work.delete_at(idx)
    when "+"
      work.insert(idx, text)
      idx += 1
    else
      return nil
    end
  end
  work
end

.parse_hunks(diff_text) ⇒ Object



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
64
65
66
67
68
69
70
71
# File 'lib/ollama_agent/runtime/unified_diff_apply.rb', line 39

def parse_hunks(diff_text)
  raw = diff_text.lines.map(&:chomp)
  hunks = []
  i = 0
  while i < raw.length
    line = raw[i]
    if line.start_with?("@@")
      m = line.match(/@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/)
      unless m
        i += 1
        next
      end
      old_start = m[1].to_i
      old_count = m[2] ? m[2].to_i : 1
      i += 1
      body = []
      while i < raw.length && !raw[i].start_with?("@@") && !raw[i].start_with?("diff --git")
        l = raw[i]
        i += 1
        next if l.empty?

        first = l[0]
        next if first == "\\"

        body << l if " +-".include?(first)
      end
      hunks << Hunk.new(old_start: old_start, old_count: old_count, body: body)
    else
      i += 1
    end
  end
  hunks
end

.to_line_array(content) ⇒ Object



12
13
14
15
16
# File 'lib/ollama_agent/runtime/unified_diff_apply.rb', line 12

def to_line_array(content)
  return [] if content.nil? || content.empty?

  content.each_line.map { |l| l.delete_suffix("\n") }
end