Module: GitHelpers::GitStats

Included in:
GitDir
Defined in:
lib/git_helpers/stats.rb

Instance Method Summary collapse

Instance Method Details

#infosObject

inspired by visionmedia//git-infos



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/git_helpers/stats.rb', line 130

def infos
	with_dir do
		puts "## Remote URLs:"
		puts
		system("git --no-pager remote -v")
		puts
		
		puts "## Remote Branches:"
		puts
		system("git --no-pager  branch -r")
		puts
		
		puts "## Local Branches:"
		puts
		system("git --no-pager  branch")
		puts
		
		puts "## Most Recent Commit:"
		puts
		system("git --no-pager log --max-count=1 --pretty=short")
		puts
	end
end

#output_stats_authors(logopts = nil) ⇒ Object



122
123
124
125
126
127
# File 'lib/git_helpers/stats.rb', line 122

def output_stats_authors(logopts=nil)
	authors=stats_authors(logopts, more: true)
	authors.each do |a,v|
		puts "- #{a}: #{v[:commits]} commits (+#{v[:added]}/-#{v[:deleted]}), #{v[:files]} files modified, #{v[:renames]} renames, #{v[:merges]} merges"
	end
end

#output_stats_diff(logopts = nil) ⇒ Object



35
36
37
38
39
40
# 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

#output_stats_linesObject



73
74
75
76
77
78
79
80
# File 'lib/git_helpers/stats.rb', line 73

def output_stats_lines
	stats=stats_lines_all
	total=stats.values.sum
	stats.sort_by{|k,v| -v}.each do |k,v|
		puts "- #{k}: #{v} (#{"%2.1f%%" % (100*v/total.to_f)})"
	end
	puts "Total lines: #{total}"
end

#stats_authors(logopts = nil, more: false) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/git_helpers/stats.rb', line 83

def stats_authors(logopts=nil, more: false)
	require 'set'
	#Exemple: --after=..., --before=...,
	#  -w #word diff
	#  -C --find-copies-harder; -M
	authors={}
	with_dir do
		%x/git shortlog -sn #{logopts}/.each_line do |l|
			commits, author=l.chomp.split(' ', 2)
			authors[author]={commits: commits.to_i}
		end

		if more
			authors.each_key do |a|
				tot_a=0; tot_r=0; tot_rename=0; files=Set.new
				%x/git log #{DefaultLogOptions} #{logopts} --numstat --format="%n" --author='#{a}'/.each_line do |l|
					added, deleted, file=l.chomp.split(' ',3)
					#puts "#{l} => #{added}, #{deleted}, #{rest}"
					tot_a+=added.to_i; tot_r+=deleted.to_i
					next if file.nil?
					if file.include?(' => ')
						tot_rename+=1
					else
						files.add(file) unless file.empty?
					end
				end
				#rev-list should be faster, but I would need to use 
				# `git rev-parse --revs-only --default HEAD #{logopts.shelljoin}`
				# to be sure we default to HEAD, and 
				# `git rev-parse --flags #{logopts.shelljoin}` to get the log flags...
				#tot_merges=%x/git rev-list #{logopts} --merges --author='#{a}'/.each_line.count
				tot_merges=%x/git log --pretty=oneline #{logopts} --merges --author='#{a}'/.each_line.count
				authors[a].merge!({added: tot_a, deleted: tot_r, files: files.size, renames: tot_rename, merges: tot_merges})
			end
		end
	end
	authors
end

#stats_diff(logopts = nil) ⇒ Object

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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) ⇒ Object

inspired by visionmedia//git-line-summary



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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_allObject



62
63
64
65
66
67
68
69
70
71
# 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
	end
	r
end

#summary(logopts = nil) ⇒ Object

inspired by visionmedia//git-summary



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/git_helpers/stats.rb', line 155

def summary(logopts=nil)
	with_dir do
		project=Pathname.new(%x/git rev-parse --show-toplevel/).basename
		authors=stats_authors(logopts)
		commits=authors.map {|a,v| v[:commits]}.sum
		file_count=%x/git ls-files/.each_line.count
		active_days=%x/git log --date=short --pretty='format: %ad' #{logopts}/.each_line.uniq.count
		#This only give the rep age of the current branch; and is not
		#efficient since we generate the first log
		#A better way would be to get all the roots commits via
		#    git rev-list --max-parents=0 HEAD
		#and then look at their ages
		repository_age=%x/git log --reverse --pretty=oneline --format="%ar" #{logopts}/.each_line.first.sub!('ago','')
		#total= %x/git rev-list #{logopts}/.each_line.count
		total=%x/git rev-list --count #{logopts.empty? ? "HEAD" : logopts.shelljoin}/.to_i

		puts " project  : #{project}"
		puts " repo age : #{repository_age}"
		puts " active   : #{active_days} days"
		puts " commits  : #{commits}"
		puts " files    : #{file_count}"
		puts " authors  : #{authors.keys.join(", ")} (Total: #{total})"
		authors.each do |a,v|
			puts " - #{a}: #{v[:commits]} (#{"%2.1f" % (100*v[:commits]/commits.to_f)}%)"
		end
	end
end