module GitHelpers::GitStats

Public Instance Methods

output_stats_diff(logopts=nil) click to toggle source
# File lib/git_helpers/stats.rb, line 35
def output_stats_diff(logopts=nil)
        lines=stats_diff(logopts)
        lines.sort_by { |a, c| -c[:all] }.each do |a, c|
                puts "#{a}: #{c[:all]} lines of diff (+#{c[:added]}/-#{c[:deleted]})"
        end
end
stats_diff(logopts=nil) click to toggle source

Note: stats-authors give the same result, should be faster, and handle mailcap inspired by git-mainline//git-rank-contributors

# File lib/git_helpers/stats.rb, line 6
def stats_diff(logopts=nil)
        lines = {}

        with_dir do
                author = nil
                state = :pre_author
                DR::Encoding.fix_utf8(`git log #{DefaultLogOptions} -p #{logopts}`).each_line do |l|
                        case
                        when (state == :pre_author || state == :post_author) && m=l[/Author: (.*)$/,1]
                                #TODO: using directly author=l[]... seems to only affect a block scoped author variable
                                author=m
                                state = :post_author
                                lines[author] ||= {added: 0, deleted: 0, all: 0}
                        when state == :post_author && l =~ /^\+\+\+\s/
                                state = :in_diff
                        when state == :in_diff && l =~ /^[\+\-]/
                                unless l=~ /^(\+\+\+|\-\-\-)\s/
                                        lines[author][:all] += 1 
                                        lines[author][:added] += 1       if l[0]=="+"
                                        lines[author][:deleted] += 1 if l[0]=="-"
                                end
                        when state == :in_diff && l =~ /^commit /
                                state = :pre_author
                        end
                end
        end
        lines
end
stats_lines(file) click to toggle source

inspired by visionmedia//git-line-summary

# File lib/git_helpers/stats.rb, line 43
def stats_lines(file)
        out=""
        with_dir do
                out,_suc=SH.run_simple("git", "blame", "--line-porcelain", file, quiet: true)
        end
        r={}
        begin
        out.each_line do |l|
                        l.match(/^author (.*)/) do |m|
                                r[m[1]]||=0
                                r[m[1]]+=1
                        end
                end
        rescue => e
                warn "Warning: #{e} on #{file}"
        end
        r
end
stats_lines_all() click to toggle source
# File lib/git_helpers/stats.rb, line 62
def stats_lines_all
        r={}
        all_files.select {|f| SH::Pathname.new(f).text? rescue false}.each do |f|
                stats_lines(f).each do |k,v|
                        r[k]||=0
                        r[k]+=v
                end