Created
August 2, 2016 20:26
git-js example
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
// Imports _____________________________________________________________________ | |
const git = require('git-js'); | |
// Setup _______________________________________________________________________ | |
git.print_output = false; | |
// Functions ___________________________________________________________________ | |
function commitToAlterMap(commit) { | |
const alters = { | |
insertions: 0, | |
deletions: 0, | |
changes: 0, | |
}; | |
commit.split(', ').forEach(alter => { | |
alter = alter.trim(); | |
if (alter === '') { | |
return; | |
} | |
let key = 'changes'; | |
if (alter.indexOf('(+)') !== -1) { | |
key = 'insertions'; | |
} | |
else if (alter.indexOf('(-)') !== -1) { | |
key = 'deletions'; | |
} | |
const count = parseInt(alter.match(/[0-9]+/)[0], 10); | |
alters[key] = alters[key] + count; | |
}); | |
return alters; | |
} | |
function reduceAlters(previous, current) { | |
for (let key in current) { | |
previous[key] = previous[key] + current[key]; | |
} | |
return previous; | |
} | |
function getAlters(author, since) { | |
const params = `--stat --pretty="" --no-merges --author="${author}" --since="${since}"`; | |
const alters = git.log(params) | |
.split('\n') | |
.filter(line => line.indexOf('|') === -1) | |
.map(commitToAlterMap) | |
.reduce(reduceAlters); | |
return alters; | |
} | |
function printAlters(author, since) { | |
const alters = getAlters(author, since); | |
console.log('Insertions:\t', alters.insertions); | |
console.log('Deletions:\t', alters.deletions); | |
console.log('Changes:\t', alters.changes); | |
} | |
function getCommits(author, since) { | |
const params = `--no-merges --pretty="oneline" --author="${author}" --since="${since}"`; | |
const commits = git.log(params) | |
.split('\n') | |
.map(line => line.split(' ')[0]) | |
.filter(line => line.trim() !== ''); | |
return commits; | |
} | |
function getFilesForCommit(commit_hash) { | |
return git.diffTree(`--no-commit-id --name-only -r ${commit_hash}`).split('\n'); | |
} | |
function getFilesTouched(author, since, ignored_extensions = ['svg', 'png', 'css', 'json', 'html', 'wav', 'ogg', 'mp3', 'yaml']) { | |
const files_touched = []; | |
const commits = getCommits(author, since); | |
commits.forEach((commit_hash) => { | |
const files_changed_in_commit = getFilesForCommit(commit_hash); | |
files_changed_in_commit.forEach((file_name) => { | |
// Skip duplicates | |
if (files_touched.includes(file_name)) { | |
return; | |
} | |
// Skip ignored extensions | |
const parts = file_name.split('.'); | |
const extension = parts[parts.length - 1]; | |
if (ignored_extensions.includes(extension)) { | |
return; | |
} | |
files_touched.push(file_name); | |
}); | |
}); | |
return files_touched.sort(); | |
} | |
function printFilesTouched(author, since) { | |
const files = getFilesTouched(author, since); | |
console.log('Files Touched:'); | |
console.log(files.join('\n')); | |
} | |
function printReport(author, since) { | |
console.log('Git Report'); | |
console.log(''); | |
printAlters(author, since); | |
console.log(''); | |
printFilesTouched(author, since) | |
} | |
// Initialization ______________________________________________________________ | |
printReport('user@domain.com', '2016-01-01'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: