Module: Ace::Git::Atoms::DiffParser

Defined in:
lib/ace/git/atoms/diff_parser.rb

Overview

Pure functions for parsing diff output Migrated from ace-git-diff

Class Method Summary collapse

Class Method Details

.count_changes(diff) ⇒ Hash

Count significant changes (additions and deletions)

Parameters:

  • diff (String)

    The diff content

Returns:

  • (Hash)

    Statistics about the diff



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ace/git/atoms/diff_parser.rb', line 22

def count_changes(diff)
  return {additions: 0, deletions: 0, files: 0, total_changes: 0} if diff.nil? || diff.empty?

  additions = 0
  deletions = 0
  files = 0

  diff.split("\n").each do |line|
    if file_header_line?(line)
      files += 1
    elsif line.start_with?("+") && !line.start_with?("+++")
      additions += 1
    elsif line.start_with?("-") && !line.start_with?("---")
      deletions += 1
    end
  end

  {
    additions: additions,
    deletions: deletions,
    files: files,
    total_changes: additions + deletions
  }
end

.count_lines(diff) ⇒ Integer

Estimate the size of a diff in lines

Parameters:

  • diff (String)

    The diff content

Returns:

  • (Integer)

    Number of lines



13
14
15
16
17
# File 'lib/ace/git/atoms/diff_parser.rb', line 13

def count_lines(diff)
  return 0 if diff.nil? || diff.empty?

  diff.count("\n") + 1
end

.exceeds_limit?(diff, max_lines) ⇒ Boolean

Check if diff exceeds a size limit

Parameters:

  • diff (String)

    The diff content

  • max_lines (Integer)

    Maximum allowed lines

Returns:

  • (Boolean)

    True if diff exceeds limit



51
52
53
# File 'lib/ace/git/atoms/diff_parser.rb', line 51

def exceeds_limit?(diff, max_lines)
  count_lines(diff) > max_lines
end

.extract_files(diff) ⇒ Array<String>

Extract list of files from diff

Parameters:

  • diff (String)

    The diff content

Returns:

  • (Array<String>)

    List of file paths



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ace/git/atoms/diff_parser.rb', line 58

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

  files = []
  diff.split("\n").each do |line|
    if line.start_with?("diff --git")
      # Extract file path from "diff --git a/path b/path"
      if line =~ /^diff --git a\/(.+) b\/(.+)$/
        files << Regexp.last_match(2) # Use 'b/' path (new file)
      end
    end
  end

  files.uniq
end

.has_changes?(diff) ⇒ Boolean

Check if diff contains any actual changes

Parameters:

  • diff (String)

    The diff content

Returns:

  • (Boolean)

    True if diff has changes



90
91
92
93
94
95
96
97
98
# File 'lib/ace/git/atoms/diff_parser.rb', line 90

def has_changes?(diff)
  return false if diff.nil? || diff.strip.empty?

  # Check for addition or deletion lines
  diff.split("\n").any? do |line|
    (line.start_with?("+") && !line.start_with?("+++")) ||
      (line.start_with?("-") && !line.start_with?("---"))
  end
end

.parse(diff) ⇒ Hash

Parse diff into structured data

Parameters:

  • diff (String)

    The diff content

Returns:

  • (Hash)

    Parsed diff data



77
78
79
80
81
82
83
84
85
# File 'lib/ace/git/atoms/diff_parser.rb', line 77

def parse(diff)
  {
    content: diff,
    stats: count_changes(diff),
    files: extract_files(diff),
    line_count: count_lines(diff),
    empty: diff.nil? || diff.strip.empty?
  }
end