Skip to content

Commit 4d4f166

Browse files
committed
feat: add safety checks for reset, to avoid unintentional resets
1 parent b68e13a commit 4d4f166

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

commands/reset.js

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,52 @@
1+
const { spawnSync } = require('child_process');
2+
const readline = require('readline');
3+
4+
async function confirm(question) {
5+
const line = readline.createInterface({
6+
input: process.stdin,
7+
output: process.stdout,
8+
})
9+
10+
return new Promise((resolve) => {
11+
line.question(question, (response) => {
12+
line.close()
13+
resolve(response.toLowerCase() === 'y')
14+
})
15+
})
16+
}
17+
118
exports.command = 'reset'
219
exports.describe = 'Caution: Reverts unstaged changes and decaffeinate commits.'
320
exports.builder = {}
421

5-
const { spawnSync } = require('child_process');
22+
exports.handler = async function (/* argv */) {
623

7-
exports.handler = function (/* argv */) {
8-
console.info(
9-
`\x1b[94me2cct: Resetting your git state... \x1b[0m`
10-
);
11-
12-
const result = spawnSync('sh', ['-c', 'git reset HEAD~ && git reset HEAD~ && git reset --hard && git clean -f']);
13-
14-
if (result.status !== 0) {
15-
console.error(`e2cct: Reset failed with code ${result.status}: ${result.stderr}`);
16-
} else {
24+
const result = spawnSync('git', ['log', '--oneline'], { stdio: 'pipe', encoding: 'utf-8' });
25+
26+
if (result.stdout) {
27+
const output = result.stdout.trim().split('\n').slice(0, 3).join('\n');
28+
console.log('Last 3 commits in the git tree:');
29+
console.log(output);
30+
if (output.indexOf('decaffeinate:') < 1) {
31+
console.log(`\x1b[31mIt looks like you don't have recent decaffeinations. Continuing with the reset will undo the last 2 listed commits!\x1b[0m`);
32+
}
33+
}
34+
35+
const confirmed = await confirm(`\x1b[38;2;255;165;0mAre you sure you want to reset? [Y/n] \x1b[0m`)
36+
37+
if (confirmed) {
1738
console.info(
18-
`\x1b[94me2cct: Reset done \x1b[0m`
39+
`\x1b[94me2cct: Resetting your git state... \x1b[0m`
1940
);
41+
42+
const result = spawnSync('sh', ['-c', 'git reset HEAD~ && git reset HEAD~ && git reset --hard && git clean -f']);
43+
44+
if (result.status !== 0) {
45+
console.error(`e2cct: Reset failed with code ${result.status}: ${result.stderr}`);
46+
} else {
47+
console.info(
48+
`\x1b[94me2cct: Reset done \x1b[0m`
49+
);
50+
}
2051
}
21-
}
52+
}

0 commit comments

Comments
 (0)