Skip to content

Instantly share code, notes, and snippets.

@JustGoscha
Last active April 17, 2018 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JustGoscha/e1057b1d384d842415a64bb8da502e83 to your computer and use it in GitHub Desktop.
Save JustGoscha/e1057b1d384d842415a64bb8da502e83 to your computer and use it in GitHub Desktop.
Counting commits in all subfolders per User
const util = require('util')
const path = require('path')
const fs = require('fs')
const fsStat = util.promisify(fs.stat)
const exec = util.promisify(require('child_process').exec);
fs.readdir('.', async (err, files) => {
const commitsPerFolders = (await Promise.all(files.map(countCommitsByAuthorInFolder))).filter(c => c)
console.log(commitsPerFolders)
console.log('----------------------')
console.log()
const allSummed = sumAllCommits(commitsPerFolders)
const highscore = Object.entries(allSummed)
.sort((a,b) => a[1] < b[1] ? 1 : -1)
.forEach(([author, commits]) => console.log(`${author}: ${commits}`))
})
async function isFolder(file) {
const stat = await fsStat(file)
return !stat.isFile()
}
async function countCommitsByAuthorInFolder(file) {
if (await isFolder(file)) {
const {stdout, stderr} = await exec(`cd ${file} && git shortlog -s -n --all --no-merges`)
const lines = splitLinesAndTrim(stdout).filter(l => l)
return lines.map(line => line.split('\t')).reduce((acc, [commits, author]) => {
if(author) acc[author] = parseInt(commits)
return acc
}, {})
}
}
function splitLinesAndTrim(str) {
return str.split('\n').map(s => s.trim())
}
function sumAllCommits(commitsPerFolders) {
return commitsPerFolders.reduce((acc, folderCommits) => {
Object.entries(folderCommits).forEach(([author, commits]) => {
if (acc[author])
acc[author] = acc[author] + commits
else
acc[author] = commits
})
return acc
}, {})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment