Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save z-hao-wang/8979a1288f3bae4e1f910495d1bff11d to your computer and use it in GitHub Desktop.
Save z-hao-wang/8979a1288f3bae4e1f910495d1bff11d to your computer and use it in GitHub Desktop.
const git = require('nodegit-kit');
const { exec } = require('child_process');
async function getCurrentBranchName() {
return new Promise(resolve => {
exec('git branch | grep \\*', function(err, stdout, stderr) {
resolve(stdout.replace('* ', ''));
});
});
}
async function getChangeFiles() {
const repo = await git.open(__dirname + '/../../../');
const masterHistory = await git.log(repo, { branch: 'master', 'max-count': 1 });
const currentBranchName = await getCurrentBranchName();
const currentBranchHistory = await git.log(repo, { branch: currentBranchName.trim(), 'max-count': 1 });
const diff = await git.diff(repo, masterHistory[0], currentBranchHistory[0]);
const changedFiles = diff.map(file => file.path);
console.info('ChangedFiles:');
console.info(changedFiles.join('\n'));
return changedFiles;
}
module.exports = getChangeFiles;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment