diff --git a/lib/execute.d.ts b/lib/execute.d.ts index c60948013d..adf2c1f385 100644 --- a/lib/execute.d.ts +++ b/lib/execute.d.ts @@ -2,6 +2,7 @@ type ExecuteOutput = { stdout: string; stderr: string; + exitCode: number; }; /** Wrapper around the GitHub toolkit exec command which returns the output. * Also allows you to easily toggle the current working directory. diff --git a/lib/execute.js b/lib/execute.js index 2e0ff4c529..3fb82cbfe1 100644 --- a/lib/execute.js +++ b/lib/execute.js @@ -15,7 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.stderr = exports.stdout = exports.execute = void 0; const exec_1 = require("@actions/exec"); const buffer_1 = __importDefault(require("buffer")); -const output = { stdout: '', stderr: '' }; +const output = { stdout: '', stderr: '', exitCode: 0 }; /** Wrapper around the GitHub toolkit exec command which returns the output. * Also allows you to easily toggle the current working directory. * @@ -29,11 +29,18 @@ function execute(cmd_1, cwd_1, silent_1) { return __awaiter(this, arguments, void 0, function* (cmd, cwd, silent, ignoreReturnCode = false) { output.stdout = ''; output.stderr = ''; - yield (0, exec_1.exec)(cmd, [], { + output.exitCode = yield (0, exec_1.exec)(cmd, [], { // Silences the input unless the INPUT_DEBUG flag is set. silent, cwd, - listeners: { stdout, stderr }, + listeners: { + stdout: (data) => { + stdout(data); + }, + stderr: (data) => { + stderr(data); + } + }, ignoreReturnCode }); return Promise.resolve(output); diff --git a/lib/worktree.js b/lib/worktree.js index f315eca410..dffa425ed1 100644 --- a/lib/worktree.js +++ b/lib/worktree.js @@ -40,6 +40,12 @@ function generateWorktree(action, worktreedir, branchExists) { if (branchExists) { yield (0, execute_1.execute)(`git fetch --no-recurse-submodules --depth=1 origin ${action.branch}`, action.workspace, action.silent); } + // Check if the worktree already exists + const { exitCode } = yield (0, execute_1.execute)(`git worktree list | grep ${worktreedir}`, action.workspace, action.silent); + // If the worktree exists, remove it + if (exitCode === 0) { + yield (0, execute_1.execute)(`git worktree remove ${worktreedir}`, action.workspace, action.silent); + } yield (0, execute_1.execute)(`git worktree add --no-checkout --detach ${worktreedir}`, action.workspace, action.silent); const checkout = new GitCheckout(action.branch); if (branchExists) {