Class: Grit::CommitStats

Inherits:
Object
  • Object
show all
Defined in:
lib/grit/commit_stats.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, id, files) ⇒ CommitStats

Instantiate a new CommitStats

+id+ is the id of the commit
+files+ is an array of :
  [ [filename, adds, deletes, total],
    [filename, adds, deletes, total],
    [filename, adds, deletes, total] ]

Returns Grit::CommitStats (baked)



15
16
17
18
19
20
21
22
# File 'lib/grit/commit_stats.rb', line 15

def initialize(repo, id, files)
  @repo = repo
  @id = id
  @files = files
  @additions  = files.inject(0) { |total, a| total += a[1] }
  @deletions  = files.inject(0) { |total, a| total += a[2] }
  @total  = files.inject(0) { |total, a| total += a[3] }
end

Instance Attribute Details

#additionsObject (readonly)

Returns the value of attribute additions.



5
6
7
# File 'lib/grit/commit_stats.rb', line 5

def additions
  @additions
end

#deletionsObject (readonly)

Returns the value of attribute deletions.



5
6
7
# File 'lib/grit/commit_stats.rb', line 5

def deletions
  @deletions
end

#filesObject (readonly)

Returns the value of attribute files.



5
6
7
# File 'lib/grit/commit_stats.rb', line 5

def files
  @files
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/grit/commit_stats.rb', line 5

def id
  @id
end

#totalObject (readonly)

Returns the value of attribute total.



5
6
7
# File 'lib/grit/commit_stats.rb', line 5

def total
  @total
end

Class Method Details

.find_all(repo, ref, options = {}) ⇒ Object

Find all commit stats matching the given criteria.

+repo+ is the Repo
+ref+ is the ref from which to begin (SHA1 or name) or nil for --all
+options+ is a Hash of optional arguments to git
  :max_count is the maximum number of commits to fetch
  :skip is the number of commits to skip

Returns assoc array [sha, Grit::Commit[] (baked)]



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/grit/commit_stats.rb', line 32

def self.find_all(repo, ref, options = {})
  allowed_options = [:max_count, :skip, :since]

  default_options = {:numstat => true}
  actual_options = default_options.merge(options)

  if ref
    output = repo.git.log(actual_options, ref)
  else
    output = repo.git.log(actual_options.merge(:all => true))
  end

  self.list_from_string(repo, output)
end

.list_from_string(repo, text) ⇒ Object

Parse out commit information into an array of baked Commit objects

+repo+ is the Repo
+text+ is the text output from the git command (raw format)

Returns assoc array [sha, Grit::Commit[] (baked)]



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/grit/commit_stats.rb', line 52

def self.list_from_string(repo, text)
  lines = text.split("\n")

  commits = []

  while !lines.empty?
    id = lines.shift.split.last

    lines.shift
    lines.shift
    lines.shift

    message_lines = []
    message_lines << lines.shift[4..-1] while lines.first =~ /^ {4}/ || lines.first == ''

    lines.shift while lines.first && lines.first.empty?

    files = []
    while lines.first =~ /^([-\d]+)\s+([-\d]+)\s+(.+)/
      (additions, deletions, filename) = lines.shift.split(nil, 3)
      additions = additions.to_i
      deletions = deletions.to_i
      total = additions + deletions
      files << [filename, additions, deletions, total]
    end

    lines.shift while lines.first && lines.first.empty?

    commits << [id, CommitStats.new(repo, id, files)]
  end

  commits
end

Instance Method Details

#inspectObject

Pretty object inspection



87
88
89
# File 'lib/grit/commit_stats.rb', line 87

def inspect
  %Q{#<Grit::CommitStats "#{@id}">}
end

#to_diffstatObject

Convert to an easy-to-traverse structure



92
93
94
95
96
# File 'lib/grit/commit_stats.rb', line 92

def to_diffstat
  files.map do ||
    DiffStat.new(*)
  end
end

#to_hashObject

private



100
101
102
103
104
105
106
107
108
# File 'lib/grit/commit_stats.rb', line 100

def to_hash
  {
    'id'        => id,
    'files'     => files,
    'additions' => additions,
    'deletions' => deletions,
    'total'     => total
  }
end