Class: Git::DiffInfo

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

Overview

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

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_patchesObject (readonly)

Returns the value of attribute file_patches

Returns:

  • (Object)

    the current value of file_patches



99
100
101
102
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
162
163
164
165
166
167
168
# File 'lib/git/diff_info.rb', line 99

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

  # @!method deletions
  #   @return [Integer] total number of lines deleted

  # @!method lines
  #   @return [Integer] total number of lines changed (insertions + deletions)

  # @!method file_count
  #   @return [Integer] number of files changed

  # 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

#statsObject (readonly)

Returns the value of attribute stats

Returns:

  • (Object)

    the current value of stats



99
100
101
102
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
162
163
164
165
166
167
168
# File 'lib/git/diff_info.rb', line 99

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

  # @!method deletions
  #   @return [Integer] total number of lines deleted

  # @!method lines
  #   @return [Integer] total number of lines changed (insertions + deletions)

  # @!method file_count
  #   @return [Integer] number of files changed

  # 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

Total number of lines deleted

Returns:

  • (Integer)


# File 'lib/git/diff_info.rb', line 103

#file_countInteger

Number of files changed

Returns:

  • (Integer)


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

#file_statsHash<String, Hash>

Per-file statistics hash

Returns:

  • (Hash<String, Hash>)

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



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

def file_stats
  stats[:files]
end

#insertionsInteger

Total number of lines inserted

Returns:

  • (Integer)


# File 'lib/git/diff_info.rb', line 100

#linesInteger

Total number of lines changed (insertions + deletions)

Returns:

  • (Integer)


# File 'lib/git/diff_info.rb', line 106

#patch_for(path) ⇒ FileDiffInfo?

Get patch info for a specific file

Parameters:

  • path (String)

    the file path

Returns:



165
166
167
# File 'lib/git/diff_info.rb', line 165

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

#patches?Boolean

Check if patch information is available

Returns:

  • (Boolean)

    true if file_patches were loaded



156
157
158
# File 'lib/git/diff_info.rb', line 156

def patches?
  !file_patches.empty?
end