module Git::Parsers::Diff::Numstat
Parser for βnumstat output
Public Instance Methods
Source
# 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
Builds stats data from a numstat entry
@param insertions_s [String] insertions field from numstat output
@param deletions_s [String] deletions field from numstat output
@param include_binary [Boolean] whether to include the β:binary` key
@return [Hash] stats hash with insertion and deletion totals
Source
# 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 numstat output into DiffResult
@param output [String] raw numstat + shortstat output
@param include_dirstat [Boolean] whether dirstat output is expected
@return [Git::DiffResult]
Source
# 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 numstat lines into a path -> stats hash (for combining with other formats)
@param lines [Array<String>] numstat lines
@param include_binary [Boolean] whether to include binary flag
@return [Hash<String, Hash>] path to stats mapping (keyed by destination path)
Source
# 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 numstat lines into DiffFileNumstatInfo array
@param lines [Array<String>] numstat lines
@return [Array<Git::DiffFileNumstatInfo>]
Source
# 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
Parse potential rename path into [dst_path, src_path]
@param filename [String] the path string from numstat output
@return [Array(String, (String, nil))] [destination_path, source_path_or_nil]
Source
# 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
Split output into numstat, shortstat, and dirstat sections
@param lines [Array<String>] all output lines
@param include_dirstat [Boolean] whether to expect dirstat section
@return [Array] [numstat_lines, shortstat_line, dirstat_lines]