和几个项目经理朋友聊到项目工作量评估,公司内部对代码量,工程师的工作量通常有集成工具记录和衡量,但是如何比对其它业界的项目效率就成立讨论焦点,比如Google的Android工程师的代码出产效率,每个项目都花费了多少人力等等。受人之托,从Android的Git历史中研究以下这些问题,于是有了这个Bash脚本工具。
最新版本的github.com下载链接 https://github.com/neokidd/ghist2db
使用这个工具的前提是你有包含完整Git历史信息的Android的代码库,一般来说,是用"repo sync"来完成的下载的代码库,可以是全部project,也可以是部分的,比如说是用"repo sync framewors/base"这样单独sync的某些project。
repo for-all -c ghist2db.sh
输出结果是在代码库顶级目录下的一个ginfo.db文件,然后就可以用sqlite3来取得许多有趣的统计信息。比如在Android 4.0.4上,谷歌工程师commit的次数一共是155416,最多的工程师是三位大神是jessewilson@google.com, hackbod@google.com (Diane Hackbod, 大名鼎鼎的framework工程师), omakoto@google.com。另外,如何假设每个谷歌工程师每周3次commit,那么Android 4.0.4共花费了51805 staff*week的工作量。源代码如下:
#!/bin/bash
usage()
{
echo "ghist2db -- convert Android git history to sqlite db."
echo "Usage:"
echo "repo forall -c ghist2db.sh"
echo "or, run it separately for each repo project"
}
find_repo_top()
{
cwd=$1
while [ "$cwd" != "" -a "$cwd" != "~" ]; do
if [ -f $cwd/.repo/repo/main.py ]; then
repo_top=$cwd
return
fi
cwd=${cwd%\/*}
done
}
if [ "$repo_top"=="" ]; then
#echo "repo_top is empty, need to call find_repo_top"
find_repo_top `pwd`
fi
if [ "$repo_top" == "" ]; then
echo "seems you're not in a Android repo, please cd to Android repo!"
usage; exit 1
fi
repo_name=${repo_top##*\/}
#echo "repo_top: " $repo_top
#echo "repo_name: " $repo_name
proj=`pwd | awk -F"$repo_name"/ '{print $2}'`
echo "populating git info in $proj..."
# step 1: print git history logs in with "--pretty" format option, so that it's ready as sql sentences.
git log --pretty=format:"insert into googler_info (h, ae, ce, at, proj) values ('%h', '%ae', '%ce', '%at', '$proj');" --author=@google.com > googler_info.txt
# step 2: group all insert operations into one commit.
echo -e "begin;\n$(cat googler_info.txt)\ncommit;" > final_googler_info.sql
# step 3: add lines for creating table.
echo -e "create table if not exists googler_info(
_id integer primary key autoincrement,
h text not null,
ae text not null,
ce text not null,
at text not null,
proj text not null);
\n$(cat final_googler_info.sql)" > final_googler_info.sql
# step 4: create the db file
echo "inserting to db..."
sqlite3 "$repo_top"/ginfo.db < final_googler_info.sql
echo "done."
1. step1中,定制git log的输出历史格式,重点是--pretty参数,让输出直接是sql语句。另外--author可以指定只输出@google.com的作者,就是只看googler的代码贡献。这里取以下这些字段,更多的可以看man page (man git-log)
%h: abbreviated commit hash
%ae: author email
%ce: committer email
%at: author date, UNIX timestamp
3. step3中,在开头加入创建db的语句,注意需要用到if not exists。
4. step4中,用sqlite3来执行这些sql语句。
5. Android原生的repo for-all负责遍历每个project,并运行ghist2db.sh这个脚本。所有信息被插入同一个db。
几个值得注意的细节:
1. Bash的字符串操作有许多方法。一类是用sed或者awk,比如line 36行,另一类用sh或者bash的内建方法,比如find_repo_top()中用到的子串的查找,${cwd%\/*},${repo_top##*\/}。参考资料:
Bash string manipulating:
http://tldp.org/LDP/abs/html/string-manipulation.html
SED:
http://www.doc88.com/p-0079912374852.html
2. Bash的if比较语句,[]里的语句和“[”之间需要有空格,开始是总是不习惯这样写。另外,字符串比较, 变量需要quote,比如"$cwd" != "~"。
3. 在指定行插入text,方法有多种,比如还是sed的方法,如果仅在开头或者结尾插入,可以echo源文件,在前后添加,如果step2中。sed的好用方法还可以参见这里(比如sed -i):
http://superuser.com/questions/246837/how-do-i-add-text-to-the-beginning-of-a-file-in-bash
http://www.doc88.com/p-0079912374852.html
4. Bash调试:用bash -x选项,得到每行的输入参数,方便查找问题。