日子走呀走,就没了踪迹,也忘了是多少周前,团队从 SVN 切换到 Git,之前写的 SVN 周报工具也算是安心退役了。前天终于下定决心写个基于 Git 的周报工具。javascript
我对工具的构思以下:java
上一个周报工具是用 Nodejs + svn 命令实现的,此次就不想用 git 命令配合了。因而上网搜了一些资料后,发现 NodeGit 这个库很适合。那么主旋律肯定了,就能够开始动工了,如下是流程图。node
根据流程图得出如下的总体流程代码:git
async function init() {
const folders = fs.readdirSync(config.dir)
// 获取到不存在的git仓库(约定文件夹都是git仓库)(其实也能够根据是否有.git 或者 nodeGit的exist)
const emptyProjects = config.projects.filter(
v => folders.indexOf(v.folder) === -1
)
if (emptyProjects.length) {
// 建立本地不存在git仓库
await createRespository(emptyProjects)
}
// 获取commit信息
const logs = await getRepositoryLog()
// 生成周报
renderReport(logs)
}
复制代码
读取本地 Git 仓库目录,这里取(tou)巧(lan)了,约定存在文件夹即认为存在 git 仓库,其实也能够根据是否有.git 目录或者经过 nodeGit 的 exit 来判断。github
不存在与本地的 Git 仓库,考虑到有不少项目是不必 clone 到本地的,因此我并不想把整个 Git 仓库都拉到本地,只是想建立个连接,而后拉取一下 Log 信息。因此实现的功能如同如下命令:shell
git init
git fetch origin
git log --all
复制代码
获取 Git 提交记录,经过 nodeGit 的 Revwalk 实现 log 全部分支的 commit 信息。其中内部约定重复提交的信息以 update 字符标识,程序上会忽略这个提交信息。markdown
const repo = await nodeGit.Repository.open(`${temporaryFolder}/.git`)
const walker = nodeGit.Revwalk.create(repo)
// 经过pushGlob来获取全部分支的提交记录
walker.pushGlob('*')
// 获取符合日期的commits
const commits = await walker.getCommitsUntil(c => {
const now = c.date()
return now > beginDate && now < endDate
})
const selfCommits = []
Promise.all(
commits
.filter(x => {
// 过滤不须要记录的commit信息
const regexp = new RegExp(`${projectFolder}|update|merge`, 'gi')
return !regexp.test(x.message())
})
.map(async x => {
// 是否须要统计行数
const total = needCount ? await countLines(x) : 0
// 构建周报信息集
selfCommits.push({
msg: x
.message()
.split(/\n|;/g)
.filter(v => v.length),
total,
project: projectName,
committer: x.committer().name()
})
})
).then(() => {
resolve(selfCommits)
})
复制代码
生成周报,最后经过 markvis、markdown-it、d3-node 生成周报图片,具体的项目路径、名字、帐号、密码、是否统计行数在 config/index.js 中配置。async
// ./config/index.js
module.exports = {
username: 'username', // Git username
password: 'password', // Git password
reponame: 'origin', // Repository name
dir: 'Git directory path', // /Users/viccici/github
reportDir: 'Report directory path', // /Users/viccici/report
commiter: {
'Git name': 'Real name' // Git committer name matching the real name
},
projects: [
{
name: 'Project name', // We often use chinese project name
folder: 'Project folder', // Git folder name that based on git path. [ PS: weekly-report-git ]
path: 'Git path',
count: true // Whether to count
}
]
}
复制代码
最终的结果以下图。svn
该周报工具更多的依赖于团队的约定,不然周报信息可读性就不好,后续还须要跟队员们商量更优的方案。NodeGit 还有不少须要深挖的知识点,后续会花点时间认真研究,从而能优化此周报工具。若有兴趣 or 更好想法,可到这里留言观看。工具