Last active
April 17, 2018 18:14
-
-
Save JustGoscha/e1057b1d384d842415a64bb8da502e83 to your computer and use it in GitHub Desktop.
Counting commits in all subfolders per User
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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