自己动手:bash脚本工具统计git历史, 发掘谷歌在Android项目奥秘

和几个项目经理朋友聊到项目工作量评估,公司内部对代码量,工程师的工作量通常有集成工具记录和衡量,但是如何比对其它业界的项目效率就成立讨论焦点,比如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."

实现上有这么几个key points:

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


2. step2中,把这些单独的sql语句组合成事务,目的是避免过多的插入事务对DB产生不必要的负担。


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选项,得到每行的输入参数,方便查找问题。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Git指令的Shell脚本,能够快速便捷地管理Git库,包括添加修改、提交修改、显示库状态、推送到远程库、从远程库更新到本地、版本恢复等操作。 使用方法: 1. 在Linux系统中,将本文件放在Git库目录下,利用Shell运行本文件; 2.在windows系统中,需下载安装与操作系统相对应的Git软件,并将本文件放在Git库目录下,双击即可运行。 运行示例: Please choose the first letter of options. [Add|Commit|Diff|Fetch|Exit|Help|Log|Push|User|Reset|Status]? h A: Add all changes to repository. C: Commit all changes to repository. D: Show differences between current version with HEAD->. E: Exit shell script. F: Fetch origin/master and merge. L: Show latest two-weeks logs of repository. P: Push commissions to origin/master. U: User command mode(Press ‘Enter’ to exit). R: Reset current version according version_id. S: Show status of repository. Please choose the first letter of options. [Add|Commit|Diff|Fetch|Exit|Help|Log|Push|User|Reset|Status]? s On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: Git.sh modified: PyNote/PyNote_2.md no changes added to commit (use "git add" and/or "git commit -a") Please choose the first letter of options. [Add|Commit|Diff|Fetch|Exit|Help|Log|Push|User|Reset|Status]? a On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD ..." to unstage) modified: Git.sh modified: PyNote/PyNote_2.md

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值