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

Parameters:

  • insertions_s (String)

    insertions field from numstat output

  • deletions_s (String)

    deletions field from numstat output

  • include_binary (Boolean)

    whether to include the :binary key

Returns:

  • (Hash)

    stats hash with insertion and deletion totals



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

Parameters:

  • output (String)

    raw numstat + shortstat output

  • include_dirstat (Boolean) (defaults to: false)

    whether dirstat output is expected

Returns:



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)

Parameters:

  • lines (Array<String>)

    numstat lines

  • include_binary (Boolean) (defaults to: false)

    whether to include binary flag

Returns:

  • (Hash<String, Hash>)

    path to stats mapping (keyed by destination path)



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

Parameters:

  • lines (Array<String>)

    numstat lines

Returns:



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]

Parameters:

  • filename (String)

    the path string from numstat output

Returns:

  • (Array(String, (String, nil)))

    [destination_path, source_path_or_nil]



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

Parameters:

  • lines (Array<String>)

    all output lines

  • include_dirstat (Boolean)

    whether to expect dirstat section

Returns:

  • (Array)

    [numstat_lines, shortstat_line, dirstat_lines]



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