Skip to content

Instantly share code, notes, and snippets.

@ehaynes99
Last active November 24, 2021 16:37
Show Gist options
  • Save ehaynes99/6124ac258b63b0aa513b2606cb7770dc to your computer and use it in GitHub Desktop.
Save ehaynes99/6124ac258b63b0aa513b2606cb7770dc to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const { exec } = require('child_process');
const { resolve } = require('path');
const cwd = process.argv[2] ? resolve(process.argv[2]) : process.cwd();
const maxBuffer = 10 * 1024 * 1024;
const execShell = (command) => {
return new Promise((resolve) => {
exec(command, { cwd, maxBuffer }, (error, stdout, stderr) => {
const errorMessage = (error && error.message) || stderr;
if (errorMessage) {
console.error(errorMessage);
process.exit(1);
}
resolve(stdout);
});
});
};
const run = async () => {
try {
const repoUrl = await getRepo();
const getCommitUrl = (commitHash) => `${repoUrl}/commit/${commitHash}`;
const commits = await getCommits();
commits.forEach(({ commit, files }, index) => {
if (index) {
console.log('-----------------');
}
console.log('\x1b[36m%s\x1b[0m', getCommitUrl(commit));
console.log();
files.forEach((file) => console.log(file));
});
} catch (error) {
console.error(error);
}
};
const getRepo = async () => {
const origin = await execShell('git remote -v | grep fetch');
const repoUrl = origin.split(/\s+/)[1];
return repoUrl
.replace('git@', 'http://')
.replace('com:', 'com/')
.replace(/.git$/, '');
};
const getCommits = async () => {
const output = await execShell(`git log --diff-filter=D --summary -C ${cwd}`);
const result = [];
let current = null;
output.split('\n').forEach((line) => {
const commitMatch = line.match(/^commit ([a-f0-9]+)/);
if (commitMatch) {
current && result.push(current);
const commit = commitMatch[1];
current = { commit, files: [] };
} else {
const fileMatch = line.match(/^\s*delete mode \d+ (.*)/);
if (fileMatch) {
current.files.push(fileMatch[1]);
}
}
});
return result;
};
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment