Class: Git::DiffInfo Private

Inherits:
Data
  • Object
show all
Defined in:
lib/git/diff_info.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Immutable value object representing diff statistics and optional file patches

DiffInfo encapsulates the parsed output from various git commands that produce diff statistics (like git stash show --stat). When patches are requested, it also includes the full patch information for each file.

The stats hash structure:

  • :total - Hash with :insertions, :deletions, :lines, :files keys
  • :files - Hash mapping file paths to { insertions:, deletions: } hashes

Work in progress; this class is internal for now and may be made public in a future release.

Examples:

Create a DiffInfo from parsed stats output

info = Git::DiffInfo.new(
  stats: {
    total: { insertions: 10, deletions: 5, lines: 15, files: 2 },
    files: {
      'lib/foo.rb' => { insertions: 8, deletions: 3 },
      'lib/bar.rb' => { insertions: 2, deletions: 2 }
    }
  },
  file_patches: []
)

Access statistics

info.insertions  # => 10
info.deletions   # => 5
info.lines       # => 15
info.file_count  # => 2

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#file_patchesArray<FileDiffInfo> (readonly)

Returns array of file diff info objects (empty if patch not requested).

Returns:

  • (Array<FileDiffInfo>)

    array of file diff info objects (empty if patch not requested)



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/git/diff_info.rb', line 103

DiffInfo = Data.define(:stats, :file_patches) do
  # Total number of lines inserted
  #
  # @return [Integer]
  #
  def insertions
    stats[:total][:insertions]
  end

  # Total number of lines deleted
  #
  # @return [Integer]
  #
  def deletions
    stats[:total][:deletions]
  end

  # Total number of lines changed (insertions + deletions)
  #
  # @return [Integer]
  #
  def lines
    stats[:total][:lines]
  end

  # Number of files changed
  #
  # @return [Integer]
  #
  def file_count
    stats[:total][:files]
  end

  # Per-file statistics hash
  #
  # @return [Hash<String, Hash>] mapping file paths to `{ insertions:, deletions: }` hashes
  #
  def file_stats
    stats[:files]
  end

  # Check if patch information is available
  #
  # @return [Boolean] true if file_patches were loaded
  #
  def patches?
    !file_patches.empty?
  end

  # Get patch info for a specific file
  #
  # @param path [String] the file path
  #
  # @return [FileDiffInfo, nil] the diff info or nil if not found
  #
  def patch_for(path)
    file_patches.find { |p| p.path == path }
  end
end

#statsHash (readonly)

Returns the statistics hash with :total and :files keys.

Returns:

  • (Hash)

    the statistics hash with :total and :files keys



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/git/diff_info.rb', line 103

DiffInfo = Data.define(:stats, :file_patches) do
  # Total number of lines inserted
  #
  # @return [Integer]
  #
  def insertions
    stats[:total][:insertions]
  end

  # Total number of lines deleted
  #
  # @return [Integer]
  #
  def deletions
    stats[:total][:deletions]
  end

  # Total number of lines changed (insertions + deletions)
  #
  # @return [Integer]
  #
  def lines
    stats[:total][:lines]
  end

  # Number of files changed
  #
  # @return [Integer]
  #
  def file_count
    stats[:total][:files]
  end

  # Per-file statistics hash
  #
  # @return [Hash<String, Hash>] mapping file paths to `{ insertions:, deletions: }` hashes
  #
  def file_stats
    stats[:files]
  end

  # Check if patch information is available
  #
  # @return [Boolean] true if file_patches were loaded
  #
  def patches?
    !file_patches.empty?
  end

  # Get patch info for a specific file
  #
  # @param path [String] the file path
  #
  # @return [FileDiffInfo, nil] the diff info or nil if not found
  #
  def patch_for(path)
    file_patches.find { |p| p.path == path }
  end
end

Instance Method Details

#deletionsInteger

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.

Total number of lines deleted

Returns:

  • (Integer)


116
117
118
# File 'lib/git/diff_info.rb', line 116

def deletions
  stats[:total][:deletions]
end

#file_countInteger

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.

Number of files changed

Returns:

  • (Integer)


132
133
134
# File 'lib/git/diff_info.rb', line 132

def file_count
  stats[:total][:files]
end

#file_statsHash<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.

Per-file statistics hash

Returns:

  • (Hash<String, Hash>)

    mapping file paths to { insertions:, deletions: } hashes



140
141
142
# File 'lib/git/diff_info.rb', line 140

def file_stats
  stats[:files]
end

#insertionsInteger

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.

Total number of lines inserted

Returns:

  • (Integer)


108
109
110
# File 'lib/git/diff_info.rb', line 108

def insertions
  stats[:total][:insertions]
end

#linesInteger

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.

Total number of lines changed (insertions + deletions)

Returns:

  • (Integer)


124
125
126
# File 'lib/git/diff_info.rb', line 124

def lines
  stats[:total][:lines]
end

#patch_for(path) ⇒ FileDiffInfo?

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.

Get patch info for a specific file

Parameters:

  • path (String)

    the file path

Returns:



158
159
160
# File 'lib/git/diff_info.rb', line 158

def patch_for(path)
  file_patches.find { |p| p.path == path }
end

#patches?Boolean

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.

Check if patch information is available

Returns:

  • (Boolean)

    true if file_patches were loaded



148
149
150
# File 'lib/git/diff_info.rb', line 148

def patches?
  !file_patches.empty?
end