Module: Commiti::DiffParser

Defined in:
lib/services/git/diff_parser.rb

Constant Summary collapse

DOC_EXTENSIONS =
%w[.md .markdown .rst .adoc .txt].freeze

Class Method Summary collapse

Class Method Details

.docs_only_files?(files) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
# File 'lib/services/git/diff_parser.rb', line 46

def self.docs_only_files?(files)
  return false if files.empty?

  files.all? do |path|
    normalized = path.to_s.downcase
    DOC_EXTENSIONS.any? { |ext| normalized.end_with?(ext) } ||
      normalized.start_with?('docs/') ||
      normalized.include?('/docs/')
  end
end

.metadata(diff) ⇒ Object



42
43
44
# File 'lib/services/git/diff_parser.rb', line 42

def self.(diff)
  (split_by_file_lines(diff))
end

.metadata_from_line_chunks(chunks) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/services/git/diff_parser.rb', line 33

def self.(chunks)
  files = chunks.map { |chunk| chunk[:path].to_s }.reject(&:empty?).uniq
  {
    files: files,
    total_files: files.length,
    docs_only: docs_only_files?(files)
  }
end

.split_by_file(diff) ⇒ Object



27
28
29
30
31
# File 'lib/services/git/diff_parser.rb', line 27

def self.split_by_file(diff)
  split_by_file_lines(diff).map do |chunk|
    { path: chunk[:path], diff: chunk[:lines].join }
  end
end

.split_by_file_lines(diff) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/services/git/diff_parser.rb', line 7

def self.split_by_file_lines(diff)
  chunks = []
  current_path = nil
  current_lines = []

  diff.to_s.each_line do |line|
    if line.start_with?('diff --git ')
      chunks << { path: current_path, lines: current_lines } if current_path

      current_path = extract_path(line)
      current_lines = [line]
    else
      current_lines << line
    end
  end

  chunks << { path: current_path, lines: current_lines } if current_path
  chunks
end