Class: Git::DiffStats

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

Overview

Lazy diff statistics for a comparison between two trees

Supports comparing (1) two commits, (2) a commit against the working tree, or (3) the index against the working tree.

Examples:

Get the insertion and deletion counts

stats = repo.diff_stats
stats.insertions #=> 3
stats.deletions  #=> 1

Instance Method Summary collapse

Constructor Details

#initialize(base, from, to, path_limiter = nil) ⇒ DiffStats

Returns a new instance of DiffStats.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/git/diff_stats.rb', line 17

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
  @fetch_stats = nil
end

Instance Method Details

#deletionsInteger

Returns the total number of lines deleted

Examples:

Get the deletion count

stats = repo.diff_stats
stats.deletions #=> 5

Returns:

  • (Integer)

    the total deletion count



37
38
39
# File 'lib/git/diff_stats.rb', line 37

def deletions
  fetch_stats[:total][:deletions]
end

#filesHash{String=>Hash{Symbol=>Integer}}

Returns a hash of statistics for each file in the diff

Examples:

Get per-file statistics

stats = repo.diff_stats
stats.files #=> { "lib/foo.rb" => { insertions: 3, deletions: 1 } }

Returns:

  • (Hash{String=>Hash{Symbol=>Integer}})

    per-file statistics keyed by file path



71
72
73
# File 'lib/git/diff_stats.rb', line 71

def files
  fetch_stats[:files]
end

#insertionsInteger

Returns the total number of lines inserted

Examples:

Get the insertion count

stats = repo.diff_stats
stats.insertions #=> 3

Returns:

  • (Integer)

    the total insertion count



48
49
50
# File 'lib/git/diff_stats.rb', line 48

def insertions
  fetch_stats[:total][:insertions]
end

#linesInteger

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

Examples:

Get the total changed-line count

stats = repo.diff_stats
stats.lines #=> 8

Returns:

  • (Integer)

    the total changed-line count



59
60
61
# File 'lib/git/diff_stats.rb', line 59

def lines
  fetch_stats[:total][:lines]
end

#totalHash{Symbol=>Integer}

Returns a hash of the total statistics for the diff

Examples:

Get total statistics

stats = repo.diff_stats
stats.total #=> { insertions: 3, deletions: 1, lines: 4, files: 1 }

Returns:

  • (Hash{Symbol=>Integer})

    aggregate statistics for the entire diff



83
84
85
# File 'lib/git/diff_stats.rb', line 83

def total
  fetch_stats[:total]
end