Skip to content

Instantly share code, notes, and snippets.

@yuanliwei
Created January 23, 2019 17:35
Show Gist options
  • Save yuanliwei/eaba7e3aabc0fcb9c24715705f3898ac to your computer and use it in GitHub Desktop.
Save yuanliwei/eaba7e3aabc0fcb9c24715705f3898ac to your computer and use it in GitHub Desktop.
统计Git代码行数,js
let fs = require('fs')
let path = require('path')
// git log --stat > stat.txt
let content = fs.readFileSync(path.join(__dirname,'stat.txt'), 'utf-8')
let lines = content.split('\n')
let commits = []
let item = {}
lines.forEach((l)=>{
if(l.startsWith('commit ')){
item = {name:l, count: 0}
commits.push(item)
console.log(l);
} else if(l.match(/\| *(\d+) */)){
let num = l.match(/\| *(\d+) */)[1]
item.count += parseInt(num)
}
})
console.log(commits.map((o)=>`${o.name} : ${o.count}`).join('\n'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment