Skip to content

Commit 4f3e5ba

Browse files
committed
cleanup stacked-rebase dir if editor exits with error
Signed-off-by: Kipras Melnikovas <kipras@kipras.org>
1 parent 292f1a4 commit 4f3e5ba

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

apply.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from "path";
44
import Git from "nodegit";
55

66
import { createQuestion } from "./util/createQuestion";
7+
import { isDirEmptySync } from "./util/fs";
78

89
import { filenames } from "./filenames";
910
import { configKeys } from "./configKeys";
@@ -178,7 +179,7 @@ export const markThatApplied = (pathToStackedRebaseDirInsideDotGit: string): voi
178179
// // })(),
179180
fs.existsSync(rewrittenListPath) && fs.unlinkSync(rewrittenListPath),
180181
fs.existsSync(gitRebaseTodoPath) && fs.unlinkSync(gitRebaseTodoPath),
181-
fs.readdirSync(pathToStackedRebaseDirInsideDotGit).length === 0 &&
182+
isDirEmptySync(pathToStackedRebaseDirInsideDotGit) &&
182183
fs.rmdirSync(pathToStackedRebaseDirInsideDotGit, { recursive: true }),
183184
void 0
184185
)

git-stacked-rebase.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { parseTodoOfStackedRebase } from "./parse-todo-of-stacked-rebase/parseTo
2222
import { Termination } from "./util/error";
2323
import { assertNever } from "./util/assertNever";
2424
import { Single, Tuple } from "./util/tuple";
25+
import { isDirEmptySync } from "./util/fs";
2526
import {
2627
GoodCommand,
2728
GoodCommandRegular,
@@ -463,6 +464,14 @@ export const gitStackedRebase = async (
463464
execSyncInRepo(`${options.editor} ${pathToStackedRebaseTodoFile}`);
464465
}
465466
} catch (_e) {
467+
/**
468+
* cleanup
469+
*/
470+
fs.unlinkSync(pathToStackedRebaseTodoFile);
471+
if (isDirEmptySync(pathToStackedRebaseDirInsideDotGit)) {
472+
fs.rmdirSync(pathToStackedRebaseDirInsideDotGit, { recursive: true });
473+
}
474+
466475
throw new Termination(`error: There was a problem with the editor '${options.editor}'.\n`);
467476
}
468477
}

util/fs.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import fs from "fs";
2+
3+
export const isDirEmptySync = (dirPath: fs.PathLike): boolean => fs.readdirSync(dirPath).length === 0;

0 commit comments

Comments
 (0)