Class Grit::CommitStats
In: lib/grit/commit_stats.rb
Parent: Object

Methods

Attributes

additions  [R] 
deletions  [R] 
files  [R] 
id  [R] 
total  [R] 

Public Class methods

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)]

[Source]

# 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

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)]

[Source]

# 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
          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

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)

[Source]

# 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

Public Instance methods

Pretty object inspection

[Source]

# File lib/grit/commit_stats.rb, line 87
    def inspect
      %Q{#<Grit::CommitStats "#{@id}">}
    end

Convert to an easy-to-traverse structure

[Source]

# File lib/grit/commit_stats.rb, line 92
    def to_diffstat
      files.map do |metadata|
        DiffStat.new(*metadata)
      end
    end

private

[Source]

# File lib/grit/commit_stats.rb, line 100
    def to_hash
      {
        'id'        => id,
        'files'     => files,
        'additions' => additions,
        'deletions' => deletions,
        'total'     => total
      }
    end

[Validate]