Module: Git::Parsers::Diff::Numstat Private

Defined in:
lib/git/parsers/diff.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Parser for --numstat output

Class Method Summary collapse

Class Method Details

.build_stats(insertions_s, deletions_s, include_binary) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Builds stats data from a numstat entry



227
228
229
230
231
232
# File 'lib/git/parsers/diff.rb', line 227

def build_stats(insertions_s, deletions_s, include_binary)
  stats = { insertions: Diff.parse_stat_value(insertions_s),
            deletions: Diff.parse_stat_value(deletions_s) }
  stats[:binary] = (insertions_s == '-' && deletions_s == '-') if include_binary
  stats
end

.parse(output, include_dirstat: false) ⇒ Git::DiffResult

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse numstat output into DiffResult



170
171
172
173
174
175
176
177
178
179
# File 'lib/git/parsers/diff.rb', line 170

def parse(output, include_dirstat: false)
  lines = output.split("\n").reject(&:empty?)
  numstat_lines, shortstat_line, dirstat_lines = split_sections(lines, include_dirstat)

  Diff.build_result(
    files: parse_file_stats(numstat_lines),
    shortstat: Diff.parse_shortstat(shortstat_line),
    dirstat: include_dirstat ? Diff.parse_dirstat(dirstat_lines) : nil
  )
end

.parse_as_map(lines, include_binary: false) ⇒ Hash<String, Hash>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse numstat lines into a path -> stats hash (for combining with other formats)



208
209
210
211
212
213
214
215
# File 'lib/git/parsers/diff.rb', line 208

def parse_as_map(lines, include_binary: false)
  lines.to_h do |line|
    insertions_s, deletions_s, filename = line.split("\t", 3)
    # Normalize rename paths so the key matches the dst_path used by raw/patch parsers
    dst_path, _src_path = parse_rename_path(filename)
    [Diff.unescape_path(dst_path), build_stats(insertions_s, deletions_s, include_binary)]
  end
end

.parse_file_stats(lines) ⇒ Array<Git::DiffFileNumstatInfo>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse numstat lines into DiffFileNumstatInfo array



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/git/parsers/diff.rb', line 187

def parse_file_stats(lines)
  lines.map do |line|
    insertions_s, deletions_s, filename = line.split("\t", 3)
    path, src_path = parse_rename_path(filename)
    Git::DiffFileNumstatInfo.new(
      path: Diff.unescape_path(path),
      src_path: src_path ? Diff.unescape_path(src_path) : nil,
      insertions: Diff.parse_stat_value(insertions_s),
      deletions: Diff.parse_stat_value(deletions_s)
    )
  end
end

.parse_rename_path(filename) ⇒ Array(String, (String, nil))

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse potential rename path into [dst_path, src_path]



256
257
258
259
260
261
262
263
264
265
# File 'lib/git/parsers/diff.rb', line 256

def parse_rename_path(filename)
  if (match = filename.match(BRACE_RENAME_PATTERN))
    prefix, old_part, new_part, suffix = match.captures
    ["#{prefix}#{new_part}#{suffix}", "#{prefix}#{old_part}#{suffix}"]
  elsif (match = filename.match(RENAME_PATTERN))
    [match[2], match[1]]
  else
    [filename, nil]
  end
end

.split_sections(lines, include_dirstat) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Split output into numstat, shortstat, and dirstat sections



242
243
244
245
246
247
248
# File 'lib/git/parsers/diff.rb', line 242

def split_sections(lines, include_dirstat)
  shortstat_index = lines.index { |l| l.match?(/^\s*\d+\s+files?\s+changed/) }
  return [lines, nil, []] unless shortstat_index

  [lines[0...shortstat_index], lines[shortstat_index],
   include_dirstat ? lines[(shortstat_index + 1)..] : []]
end