67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/git-branch_stats.rb', line 67
def self.analyze(branch = `git rev-parse --abbrev-ref HEAD`.strip)
warnings = []
branches = `git branch`.split(/\n/)
independent_commits_and_emails = if branches.one?
independent_commits_and_emails = `git log --format="%H,%ae" --no-merges #{branch}`.split(/\n/)
else
independent_commits_and_emails = `git log --format="%H,%ae" --no-merges #{branch} --not $(git for-each-ref --format="%(refname)" refs/heads | grep -Fv refs/heads/#{branch})`.split(/\n/)
end
independent_commits = independent_commits_and_emails.collect {|c| c.split(',').first}
emails = independent_commits_and_emails.collect {|c| c.split(',').last}
emails = emails.uniq
if independent_commits.any?
diff_origin = independent_commits.last + "~"
commit_count = independent_commits.length
`git rev-parse --verify --quiet #{diff_origin}`
if $?.to_i > 0
diff_origin = independent_commits.last
warnings.push "WARN: Branch has independent commits since the first commit of the repo, stats will be skewed (can't run stats *before* the first commit)"
commit_count = commit_count - 1
end
numstats = `git diff --numstat #{diff_origin}`.split(/\n/)
stats_by_file = numstats.collect do |numstat|
additions, deletions, filename = numstat.split(/\t/)
{:additions => additions.to_i, :deletions => deletions.to_i, :filename => filename}
end
stats_by_file.select {|numstat| numstat[:additions] > 0}.each do |numstat|
diff = `git diff #{diff_origin} -- #{numstat[:filename]}`
new_content = diff.lines.select {|line| line =~ /^\+[^\+]/}.collect {|line| line[1..-1]}.join("\n")
numstat[:content] = new_content
end
language_stats = (stats_by_file)
change_count = stats_by_file.length
total_additions = stats_by_file.inject(0) {|total, numstat| total += numstat[:additions]}
total_deletions = stats_by_file.inject(0) {|total, numstat| total += numstat[:deletions]}
else
commit_count = 0
total_additions = 0
total_deletions = 0
change_count = 0
language_stats = []
emails = []
warnings.push "No independent commits yet, commit something!"
end
{
:commits => commit_count,
:additions => total_additions,
:deletions => total_deletions,
:files_changed => change_count,
:language_stats => language_stats,
:emails => emails,
:warnings => warnings
}
end
|