Module: Git::Parsers::Diff 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.
Combined/merge diffs (e.g., from git diff --cc or git show <merge>) are not
supported. These have a different format with multiple +/- columns per parent.
Parser for git diff output in various formats
Handles parsing of --numstat, --shortstat, --dirstat, --raw, and --patch output. This parser is used by stash show, diff, show, and log commands.
Design Note: Namespace Organization
This parser creates and returns DiffResult and DiffFile*Info
objects, which live at the top-level Git:: namespace rather than within
Git::Parsers::. This is intentional:
- Parsers are infrastructure - marked
@api private, users shouldn't interact with them directly - Info/Result classes are public API - returned by commands and used throughout the codebase
- Info classes are domain entities - represent git diff data
- Result classes are operation outcomes - represent command results, not parsing details
Keeping Info/Result classes at Git:: improves discoverability and correctly
reflects their role as public types rather than parser internals.
Defined Under Namespace
Constant Summary collapse
- STATUS_MAP =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Status letter to symbol mapping for --raw output
{ 'A' => :added, 'M' => :modified, 'D' => :deleted, 'R' => :renamed, 'C' => :copied, 'T' => :type_changed }.freeze
- NULL_SHA =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Null SHA for non-existent files
'0' * 7
- NULL_MODE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Null mode for non-existent files
'000000'- RENAME_PATTERN =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Rename format patterns from git numstat -M output: old_name.rb => new_name.rb {old_dir => new_dir}/file.rb dir/{old_name.rb => new_name.rb}
/\A(.+) => (.+)\z/- BRACE_RENAME_PATTERN =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Brace-expanded rename format from git numstat -M output
/\A(.*)\{(.+) => (.+)\}(.*)\z/
Class Method Summary collapse
-
.build_result(files:, shortstat:, dirstat:) ⇒ Git::DiffResult
private
Build a DiffResult from parsed components.
-
.empty_result ⇒ Git::DiffResult
private
Build an empty DiffResult.
-
.parse_dirstat(lines) ⇒ Git::DirstatInfo
private
Parse dirstat lines into DirstatInfo.
-
.parse_shortstat(line) ⇒ Hash
private
Parse shortstat line into components.
-
.parse_stat_value(value) ⇒ Integer
private
Parse a stat value (handles '-' for binary files).
-
.unescape_path(path) ⇒ String
private
Unescape quoted path from git output.
Class Method Details
.build_result(files:, shortstat:, dirstat:) ⇒ 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.
Build a DiffResult from parsed components
76 77 78 79 80 81 82 83 84 |
# File 'lib/git/parsers/diff.rb', line 76 def build_result(files:, shortstat:, dirstat:) Git::DiffResult.new( files_changed: shortstat[:files_changed], total_insertions: shortstat[:insertions], total_deletions: shortstat[:deletions], files: files, dirstat: dirstat ) end |
.empty_result ⇒ 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.
Build an empty DiffResult
90 91 92 93 94 95 |
# File 'lib/git/parsers/diff.rb', line 90 def empty_result Git::DiffResult.new( files_changed: 0, total_insertions: 0, total_deletions: 0, files: [], dirstat: nil ) end |
.parse_dirstat(lines) ⇒ Git::DirstatInfo
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 dirstat lines into DirstatInfo
127 128 129 130 131 132 133 134 |
# File 'lib/git/parsers/diff.rb', line 127 def parse_dirstat(lines) entries = lines.filter_map do |line| next unless (match = line.match(/^\s*([\d.]+)%\s+(.+)$/)) Git::DirstatEntry.new(percentage: match[1].to_f, directory: match[2]) end Git::DirstatInfo.new(entries: entries) end |
.parse_shortstat(line) ⇒ 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 shortstat line into components
107 108 109 110 111 112 113 114 115 |
# File 'lib/git/parsers/diff.rb', line 107 def parse_shortstat(line) return { files_changed: 0, insertions: 0, deletions: 0 } if line.nil? { files_changed: line.match(/(\d+)\s+files?\s+changed/)&.[](1).to_i, insertions: line.match(/(\d+)\s+insertions?\(\+\)/)&.[](1).to_i, deletions: line.match(/(\d+)\s+deletions?\(-\)/)&.[](1).to_i } end |
.parse_stat_value(value) ⇒ Integer
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 a stat value (handles '-' for binary files)
142 143 144 |
# File 'lib/git/parsers/diff.rb', line 142 def parse_stat_value(value) value == '-' ? 0 : value.to_i end |
.unescape_path(path) ⇒ String
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.
Unescape quoted path from git output
152 153 154 155 156 |
# File 'lib/git/parsers/diff.rb', line 152 def unescape_path(path) return path unless path&.start_with?('"') && path.end_with?('"') Git::EscapedPath.new(path[1..-2]).unescape end |