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)

Initializes diff-stats state for a tree comparison.

Parameters:

  • base (Git::Repository)

    the git object used to fetch diff stats

  • from (String)

    the first commit or object to compare

  • to (String, nil)

    the second commit or object to compare

  • path_limiter (String, Pathname, Array<String, Pathname>, nil) (defaults to: nil)

    path(s) to limit the diff

Raises:

  • (ArgumentError)

    when from or to starts with "-"



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/git/diff_stats.rb', line 32

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



52
53
54
# File 'lib/git/diff_stats.rb', line 52

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



86
87
88
# File 'lib/git/diff_stats.rb', line 86

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



63
64
65
# File 'lib/git/diff_stats.rb', line 63

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



74
75
76
# File 'lib/git/diff_stats.rb', line 74

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



98
99
100
# File 'lib/git/diff_stats.rb', line 98

def total
  fetch_stats[:total]
end