class Git::DiffStats

Provides access to the statistics of a diff between two commits, including insertions, deletions, and file-level details.

Public Class Methods

new(base, from, to, path_limiter = nil) click to toggle source

@private

# File lib/git/diff_stats.rb, line 8
def initialize(base, from, to, path_limiter = nil)
  # Eagerly check for invalid arguments
  [from, to].compact.each do |arg|
    raise ArgumentError, "Invalid argument: '#{arg}'" if arg.start_with?('-')
  end

  @base = base
  @from = from
  @to = to
  @path_limiter = path_limiter
  @stats = nil
end

Public Instance Methods

deletions() click to toggle source

Returns the total number of lines deleted.

# File lib/git/diff_stats.rb, line 22
def deletions
  fetch_stats[:total][:deletions]
end
files() click to toggle source

Returns a hash of statistics for each file in the diff.

@return [Hash<String, {insertions: Integer, deletions: Integer}>]

# File lib/git/diff_stats.rb, line 39
def files
  fetch_stats[:files]
end
insertions() click to toggle source

Returns the total number of lines inserted.

# File lib/git/diff_stats.rb, line 27
def insertions
  fetch_stats[:total][:insertions]
end
lines() click to toggle source

Returns the total number of lines changed (insertions + deletions).

# File lib/git/diff_stats.rb, line 32
def lines
  fetch_stats[:total][:lines]
end
total() click to toggle source

Returns a hash of the total statistics for the diff.

@return [{insertions: Integer, deletions: Integer, lines: Integer, files: Integer}]

# File lib/git/diff_stats.rb, line 46
def total
  fetch_stats[:total]
end

Private Instance Methods

fetch_stats() click to toggle source

Lazily fetches and caches the stats from the git lib.

# File lib/git/diff_stats.rb, line 53
def fetch_stats
  @fetch_stats ||= @base.lib.diff_stats(
    @from, @to, { path_limiter: @path_limiter }
  )
end