/
git-delete-squashed.js
executable file
·49 lines (42 loc) · 1.84 KB
/
git-delete-squashed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env node
'use strict';
const childProcess = require('child_process');
const Promise = require('bluebird');
const DEFAULT_BRANCH_NAME = 'master';
const RUN_WITH_NODE = process.argv[0].includes('node');
const selectedBranchName = process.argv[RUN_WITH_NODE ? 2 : 1] || DEFAULT_BRANCH_NAME;
/**
* Calls `git` with the given arguments from the CWD
* @param {string[]} args A list of arguments
* @returns {Promise<string>} The output from `git`
*/
function git (args) {
return new Promise((resolve, reject) => {
const child = childProcess.spawn('git', args);
let stdout = '';
let stderr = '';
child.stdout.on('data', data => stdout += data);
child.stderr.on('data', data => stderr += data);
child.on('close', exitCode => exitCode ? reject(stderr) : resolve(stdout));
}).then(stdout => stdout.replace(/\n$/, ''));
}
git(['for-each-ref', 'refs/heads/', '--format=%(refname:short)'])
.then(branchListOutput => branchListOutput.split('\n'))
.tap(branchNames => {
if (branchNames.indexOf(selectedBranchName) === -1) {
throw `fatal: no branch named '${selectedBranchName}' found in this repo`;
}
}).filter(branchName =>
// Get the common ancestor with the branch and master
Promise.join(
git(['merge-base', selectedBranchName, branchName]),
git(['rev-parse', `${branchName}^{tree}`]),
(ancestorHash, treeId) => git(['commit-tree', treeId, '-p', ancestorHash, '-m', `Temp commit for ${branchName}`])
)
.then(danglingCommitId => git(['cherry', selectedBranchName, danglingCommitId]))
.then(output => output.startsWith('-'))
)
.tap(branchNamesToDelete => branchNamesToDelete.length && git(['checkout', selectedBranchName]))
.mapSeries(branchName => git(['branch', '-D', branchName]))
.mapSeries(stdout => console.log(stdout))
.catch(err => console.error(err.cause || err));