【Git 学习笔记】第六章 从仓库中提取有用信息

第六章 从仓库中提取有用信息

相关主题

  • 提取头号贡献者
  • 找到代码树形结构中的瓶颈
  • grep 处理 commit 消息
  • 发布内容简介
  • 找出仓库在过去一段时间取得的成效

简介

无论在大型组织还是小型组织中工作,保护和维护数据始终很重要。只需要做些数据提取工作,就能跟踪大量信息。 当提交注释填写了某些关键信息时,开发人员会将其纳入业务系统中。例如 bug 跟踪系统中被修复 bug 详情。

这些数据不仅有助于管理,也能给公司级 C 类文件(即修复了几乎绝大部分 bug 的文件)带来更多时间价值。

6.1 提取头号贡献者

Git 内置了一些同步统计小工具。例如 git log 命令中的 --number 参数,可以获取到每个文件新增或删除的代码总行数。如果只统计头号贡献人员,使用 git shortlog 命令即可。

示例如下:

$ git clone https://github.com/eclipse-jgit/jgit chapter6
# showing the last five commits with shortlog
$ git shortlog -5
# To sort and find the top committer, use -n or --numbered option
$ git shortlog -5 --numbered
# only show the commit count for each developer
$ git shortlog -5 --numbered --summary
# show email for all branches, use -e or --email, and --all
$ git shortlog  --numbered --summary --email --all
# list the top committers for the last six months
$ git shortlog  --numbered --summary --email --all --since="6 months ago"
# list the top committer for a specific month using the --until option
$ git shortlog  --numbered --summary --email --all --since="30 september 2013" --until="1 november 2013"

控制时间段,可以使用 --since--util 参数,可用的时间格式有:

  • “n weeks ago”
  • "n days ago"
  • “n months ago”
  • " n hours ago"

发散:也可以根据指定的文件或文件夹,利用 git shortlog 命令找到最佳的求助对象。将文件名或文件夹放到末尾:

# Add the file to the end
$ git shortlog  --numbered --summary --email ./pom.xml
# Or append a file path
$ git shortlog --numbered --summary --email .\org.eclipse.jgit.http.test

6.2 找到代码树形结构中的瓶颈

研发团队通常知道代码结构的瓶颈问题所在,但要让管理人员相信你确实需要一些资源重写代码就有点难了。利用 Git 可以轻松提取相关信息予以佐证:

# Checkout branch stable-3.1 for use
$ git checkout stable-3.1
# use dirstat parameter for a single commit log
$ git log -1 --dirstat

使用 --dirstatoption 参数可以查看变动的文件夹及变动的总比例。默认情况下统计的是提交时新增或删除的行数。因此调整代码行的位置不会影响输出结果。要将行变换的情况考虑进去,使用参数 --dirstat=lines 即可:

$ git log -1 --dirstat=lines
# only show dirs that have 10%+ changes
$ git log -1 --dirstat=lines,10 
# To cumulate the given dir as well as in its subdir, use 'cumulative' like this:
$ git log -1 --dirstat=lines,10,cumulative 
# 'dirstat' is also valid for 'git diff' command
# Show changes between two releases
$ git diff  origin/stable-3.1..origin/stable-3.2 --dirstat
# use --relative to narrow the path
$ git diff --pretty  origin/stable-3.1..origin/stable-3.2 --numstat  --relative="org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/" org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/
4       2       storage/file/FileRepositoryBuilderTest.java
8       1       storage/file/FileSnapshotTest.java
0       741     storage/file/GCTest.java
162     0       storage/file/GcBasicPackingTest.java
119     0       storage/file/GcBranchPrunedTest.java

最后的结果,第一列表示新增的行数,第二列表示删除的行数,统计范围是origin/stable-3.1 分支与 origin/stable-3.2 分支之间。

发散:查找提交版本最多的文件:(需要借助 bash 字符串工具)

# list all the files changed in each commit between two releases
$ git log origin/stable-3.1..origin/stable-3.2 --format=format: --name-only
# use bash tool based on the output to sort and extract the top 10 files with maximum commits
$ git log origin/stable-3.1..origin/stable-3.2 --format=format: --name-only | sed '/^$/d'  | sort | uniq -c | sort -r | head -10 
# Then use the top 1 file name (RebaseCommand.java) to check the commit details
$ git log origin/stable-3.1..origin/stable-3.2 org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java

6.3 用 grep 处理 commit 消息

知道了怎么列出并排序改动频繁的文件,有时候还需要了解修复的 bug、实现的新特性,或者是谁给代码签名背书。这些信息通常可以从提交消息入手。一些公司会明确要求在提交注释中标注某个 bug、某个特性或其他特定引用,以便后期得到一个不错的发布说明。

本例继续沿用 chapter6 本地仓库:

# Show how many commits in the repository are referring to a bug
$ git log --all --grep="^[bB][uU][gG]: [0-9]" 
# Count the number
$ git log --all --oneline --grep="^[bB][uU][gG]: [0-9]" | wc -l
# Using more advanced regex pattern
$ git log --all --oneline  --extended-regexp --grep="^[bB][uU][gG]: [0-9]{6}"
# ignore case
$ git log --all --oneline  --regexp-ignore-case --extended-regexp --grep="^bug: [0-9]{6}"

注意:即使开启了通用正则(--extended-regexp),具体的匹配表达式仍然要写到 --grep 参数上。

6.4 发布内容简介

从 Git 中提取信息后,自然要做的事情之一就是生成发行说明。要生成发行说明,您需要某版本和上一版的存储库之间的所有有效信息。以 v2.3.1.201302201838-rv3.0.0.201305080800-m7 之间的提交历史为例:

# Show the whole of the commit infos between two releases
$ git log --oneline v2.3.1.201302201838-r..v3.0.0.201305080800-m7
# Count the number
$ git log --oneline v2.3.1.201302201838-r..v3.0.0.201305080800-m7 | wc -l
# Show the most modified files between the releases
$ git log  v2.3.1.201302201838-r..v3.0.0.201305080800-m7  --format=format: --name-only | sed '/^$/d'  | sort | uniq -c | sort -r | head -10
# find the commit that refers to bugs 
$ git log --format=format:%h --regexp-ignore-case  --extended-regexp --grep="bug: [0-9]{6}"  v2.3.1.201302201838-r..v3.0.0.201305080800-m7 | xargs -n1 git log -1 | grep --ignore-case -E "commit [0-9a-f]{40}|bug:"

这里的 xargs -n1 git log -1 会对前面 git log 命令得到的每一个提交记录执行一次 git log -1。而 grep --ignore-case -E "commit [0-9a-f]{40}|bug:" 则会忽略大小写进行正则匹配,这里的 -E 表示开启通用正则表达式模式,{40} 表示 40 个字符。

提取的所有这些信息为一份详实可靠的发行说明奠定了基础。紧跟着下一步,便是查看 bug 跟踪系统,列出提交历史中修复的每个 bug 的标题。

6.5 找出仓库在过去一段时间取得的成效

# 1. Show commits in the last 30 days
$ git log --all --since="30 days ago"
# 2. Show commits by David Pursehouse
$ git log --all --since="30 days ago" --oneline --author="Matthias Sohn"
# Show the commits by current user
$ git log --all --since="30 days ago" --oneline --author="$(git config user.name)"
# Get rid of those merged commits
$ git log --all --since="30 days ago" --oneline --author="Matthias Sohn" --no-merges
  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值