module Git::Parsers::Diff::Raw
Parser for โraw output (combined with numstat)
Public Instance Methods
Source
# File lib/git/parsers/diff.rb, line 393 def build_file_ref(mode, sha, path) return nil if mode == NULL_MODE || path.nil? Git::FileRef.new(mode: mode, sha: sha, path: path) end
Build a FileRef, returning nil if the file doesnโt exist on this side
@param mode [String] file mode
@param sha [String] file SHA
@param path [String, nil] file path
@return [Git::FileRef, nil]
Source
# File lib/git/parsers/diff.rb, line 348 def build_raw_info(parsed, stats) Git::DiffFileRawInfo.new( src: build_file_ref(parsed[:modes][0], parsed[:shas][0], parsed[:src_path]), dst: build_file_ref(parsed[:modes][1], parsed[:shas][1], parsed[:dst_path]), status: parsed[:status], similarity: parsed[:similarity], **stats ) end
Builds a raw diff file info object from parsed metadata and stats
@param parsed [Hash] parsed metadata from {#parse_raw_line_parts}
@param stats [Hash] insertion, deletion, and binary information
@return [Git::DiffFileRawInfo]
Source
# File lib/git/parsers/diff.rb, line 374 def extract_paths(paths) if paths.length == 2 [Diff.unescape_path(paths[0]), Diff.unescape_path(paths[1])] else path = Diff.unescape_path(paths[0]) [path, path] end end
Extract source and destination paths from raw output paths
@param paths [Array<String>] paths array
@return [Array((String, nil), (String, nil))] [src_path, dst_path]
Source
# File lib/git/parsers/diff.rb, line 280 def parse(output, include_dirstat: false) raw_lines, numstat_lines, shortstat_line, dirstat_lines = split_sections(output, include_dirstat) numstat_map = Numstat.parse_as_map(numstat_lines, include_binary: true) Diff.build_result( files: raw_lines.map { |line| parse_raw_line(line, numstat_map) }, shortstat: Diff.parse_shortstat(shortstat_line), dirstat: include_dirstat ? Diff.parse_dirstat(dirstat_lines) : nil ) end
Parse combined raw + numstat + shortstat output into DiffResult
@param output [String] combined output
@param include_dirstat [Boolean] whether dirstat output is expected
@return [Git::DiffResult]
Source
# File lib/git/parsers/diff.rb, line 318 def parse_raw_line(line, numstat_map) parsed = parse_raw_line_parts(line) stats = numstat_map.fetch(parsed[:dst_path] || parsed[:src_path], { insertions: 0, deletions: 0, binary: false }) build_raw_info(parsed, stats) end
Parse a single โraw output line
@param line [String] a single raw output line
@param numstat_map [Hash<String, Hash>] path to stats mapping
@return [Git::DiffFileRawInfo]
Source
# File lib/git/parsers/diff.rb, line 331 def parse_raw_line_parts(line) parts = line[1..].split(/\s+/, 5) status_char, *paths = parts[4].split("\t") status, similarity = parse_status(status_char) src_path, dst_path = extract_paths(paths) { modes: parts[0..1], shas: parts[2..3], status: status, similarity: similarity, src_path: src_path, dst_path: dst_path } end
Parses the fields from a raw diff line
@param line [String] a single raw output line
@return [Hash] parsed metadata for one raw diff entry
Source
# File lib/git/parsers/diff.rb, line 362 def parse_status(status_char) letter = status_char[0] similarity = status_char.length > 1 ? status_char[1..].to_i : nil [STATUS_MAP.fetch(letter, :unknown), similarity] end
Parse status character and optional similarity percentage
@param status_char [String] e.g., โMโ, โAโ, โR075โ
@return [Array(Symbol, (Integer, nil))] [status, similarity]
Source
# File lib/git/parsers/diff.rb, line 299 def split_sections(output, include_dirstat) lines = output.split("\n").reject(&:empty?) raw_lines, non_raw_lines = lines.partition { |l| l.start_with?(':') } shortstat_index = non_raw_lines.index { |l| l.match?(/^\s*\d+\s+files?\s+changed/) } return [raw_lines, non_raw_lines, nil, []] unless shortstat_index [raw_lines, non_raw_lines[0...shortstat_index], non_raw_lines[shortstat_index], include_dirstat ? non_raw_lines[(shortstat_index + 1)..] : []] end
Split output into raw, numstat, shortstat, and dirstat sections
@param output [String] combined output
@param include_dirstat [Boolean] whether to expect dirstat section
@return [Array(Array<String>, Array<String>, (String, nil), Array<String>)]