内容输出和到文件,添加了配置文件。。。
配置文件按照ruby的语法写,在ruby里面直接load加载就行:srcconfig.conf
#!/usr/bin/ruby
#coding=utf-8
#后缀添加
$suffixes = [
".c",
".cpp",
".js",
".h"
]
$start_rev = "0978ff77" #或者分支名
#分支名
$end_rev = [
"origin/xx/xx/xx",
"xx/xx/xx/xx"
]
$name_list = {
"name1" => "姓名1",
"name2" => "姓名2",
"name3" => "姓名3"
}
======================================================================================
src.rb
#!/usr/bin/ruby
#coding=utf-8
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
#加载配置文件srcconfig.conf
load "srcconfig.conf"
#通过git diff 获取指定分支下改变的文件名(绝对路径),
#并过滤掉删除的文件(删除的文件也会被git diff找出来),放入数组chang_filenames
def get_diff_filename(versions)
filenames = `git diff --name-status #{$start_rev} #{versions}`
chang_filenames = Array.new
filenames.each_line{|l| chang_filenames.push(l)}
chang_filenames.delete_if {|item| item.split[0] == 'D' }
#通过字符串拼接获得完整路径
path = `echo %cd%`.chomp!
chang_filenames.map! {|item| item = item.split[1]}
chang_filenames.map! {|item| item = "#{path}" + "\\" + item }
chang_filenames.delete_if {|item|!((item.include?$suffixes[0]) || (item.include?$suffixes[1]) || \
(item.include?$suffixes[2]) || (item.include?$suffixes[3]))}
#替换路径分隔符
for j in 0..chang_filenames.length-1
chang_filenames[j].gsub!('/','\\')
end
return chang_filenames
end
#通过git blame加文件名获得每个文件每个变化行的author
def get_authers(versions,chang_filenames)
blames = Array.new
j = -1;
`git checkout #{versions}`
#利用git blame得到修改过的文件的长字符串,放到数组
for i in 0..chang_filenames.length-1
blames[i] = `git blame #{chang_filenames[i]} #{$start_rev}..#{versions}`
blames[i] = blames[i].encode("UTF-16BE", "UTF-8", :invalid => :replace, \
:undef => :replace, :replace => '?').encode("UTF-8")
blames[i].each_line{ |l| l.chomp!;
if (l!="\u0000" && l[0]!=' ' && l[0]!='^' && l[0]!="\n" && l[0]!="\t" && l[0]!="\r" && \
(l.split(')')[1])[0..-1]!="\u0000" && (l.split(')')[1])[0..-1]!=' ' && (l.split(')')[1])[0..-1]!="\r" \
&& (l.split(')')[1])[0..-1]!="\t") then $authers[j+=1] = l.split("(")[1].split[0]
end}
end
$authers.delete(nil)
basic = $authers.uniq
return basic
end
#得到每个author的代码量,放入数组vars
def get_count(basic)
#必须先初始化,双层循环里自加的时候需要
vars = Array.new(basic.length, 0)
for i in 0..basic.length-1
for j in 0..$authers.length-1
if $authers[j]==basic[i] then
vars[i] += 1
end
end
end
return vars
end
#将从配置文件读取的每个分支的统计情况以追加的方式写入文件output
def put_result_foreach(basic, vars, versions)
k = -1;
name_count = Array.new
basic.each{|item|name_count[k+=1] = " #{$name_list[item]}\t\t\t#{vars[k]}"}
#输出结果
aFile = File.new("output.txt", "a+")
if aFile
aFile.syswrite(" 基版\t\t\t比较版本\t\n")
aFile.syswrite("=====================================================\n")
aFile.syswrite(" #{$start_rev}\t\t#{versions}\n\n")
aFile.syswrite(" 姓名\t\t\t代码量(line)\n")
name_count.each{|name| aFile.syswrite(name); aFile.syswrite("\n")}
aFile.syswrite("=====================================================\n\n")
else
puts "Unable to open file!"
end
end
#处理两个二维数组得到几个分支代码变化的总数
def get_total
basic = Array.new
vars = Array.new
num = Array.new
k = -1
for i in 0..$basic_each.length-1
basic += $basic_each[i]
end
for i in 0..$basic_each.length-1
vars += $vars_each[i]
end
for i in 0..basic.length-2
for j in i+1..basic.length-1
if basic[i] == basic[j] then
vars[i] += vars[j]
num[k+=1] = j
end
end
end
for i in 0..num.length-1
basic.delete_at(num[i].to_i)
vars.delete_at(num[i].to_i)
end
put_result_total(basic, vars)
end
#将总结的代码变化数组写入文件output
def put_result_total(basic, vars)
k = -1;
name_count = Array.new
basic.each{|item|name_count[k+=1] = " #{$name_list[item]}\t\t\t#{vars[k]}"}
aFile = File.new("output.txt", "a+")
if aFile
aFile.syswrite("\t\t代码总和\t\t\n")
aFile.syswrite("=====================================================\n")
aFile.syswrite(" 姓名\t\t\t代码量(line)\n")
name_count.each{|name| aFile.syswrite(name); aFile.syswrite("\n")}
else
puts "Unable to open file!"
end
end
def main
for i in 0..$end_rev.length-1
chang_filenames = get_diff_filename($end_rev[i])
$basic_each[i] = get_authers($end_rev[i], chang_filenames)
$vars_each[i] = get_count($basic_each[i])
put_result_foreach($basic_each[i], $vars_each[i], $end_rev[i])
end
get_total
end
$authers = Array.new
$basic_each = Array.new
$vars_each = Array.new
main
======================================================================================
最终输出结果:
基版 比较版本
=====================================================
版本号 版本号
姓名 代码量(line)
name1 396
=====================================================
基版 比较版本
=====================================================
版本号 版本号
姓名 代码量(line)
name1 35
name2 66
name3 59
name4 48
name5 47
name6 24
name7 190
name8 58
name9 37
name10 29
name11 442
name12 1
=====================================================
代码总和
=====================================================
姓名 代码量(line)
name1 396
name2 35
name3 66
name4 59
name5 48
name6 47
name7 24
name8 190
name9 58
name10 37
name11 29
name12 442