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)

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.



203
204
205
206
207
208
# File 'lib/git/parsers/diff.rb', line 203

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:



159
160
161
162
163
164
165
166
167
168
# File 'lib/git/parsers/diff.rb', line 159

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)



194
195
196
197
198
199
200
201
# File 'lib/git/parsers/diff.rb', line 194

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:



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/git/parsers/diff.rb', line 175

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]



229
230
231
232
233
234
235
236
237
238
# File 'lib/git/parsers/diff.rb', line 229

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]



216
217
218
219
220
221
222
# File 'lib/git/parsers/diff.rb', line 216

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