From 2d5a05efc1cbfb34db11b06af43920c49ac774c8 Mon Sep 17 00:00:00 2001 From: James Ives Date: Sat, 18 May 2024 02:55:41 +0000 Subject: [PATCH] =?UTF-8?q?Deploy=20Production=20Code=20for=20Commit=20f7f?= =?UTF-8?q?f0ebd3a01ccc01538360a87ad98c66fe096a0=20=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/worktree.d.ts | 11 ++++++++++- lib/worktree.js | 33 ++++++++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/lib/worktree.d.ts b/lib/worktree.d.ts index e20864f03..37da47e99 100644 --- a/lib/worktree.d.ts +++ b/lib/worktree.d.ts @@ -1,4 +1,7 @@ import { ActionInterface } from './constants'; +/** + * Git checkout command. + */ export declare class GitCheckout { orphan: boolean; commitish?: string | null; @@ -6,4 +9,10 @@ export declare class GitCheckout { constructor(branch: string, commitish?: string); toString(): string; } -export declare function generateWorktree(action: ActionInterface, worktreedir: string, branchExists: unknown): Promise; +/** + * Generates a git worktree. + * @param action - The action interface. + * @param worktreedir - The worktree directory. + * @param branchExists - Bool indicating if the branch exists. + */ +export declare function generateWorktree(action: ActionInterface, worktreedir: string, branchExists: boolean | number): Promise; diff --git a/lib/worktree.js b/lib/worktree.js index 69c82699d..bedfa6db9 100644 --- a/lib/worktree.js +++ b/lib/worktree.js @@ -13,6 +13,9 @@ exports.generateWorktree = exports.GitCheckout = void 0; const core_1 = require("@actions/core"); const execute_1 = require("./execute"); const util_1 = require("./util"); +/** + * Git checkout command. + */ class GitCheckout { constructor(branch, commitish) { this.orphan = false; @@ -31,6 +34,12 @@ class GitCheckout { } } exports.GitCheckout = GitCheckout; +/** + * Generates a git worktree. + * @param action - The action interface. + * @param worktreedir - The worktree directory. + * @param branchExists - Bool indicating if the branch exists. + */ function generateWorktree(action, worktreedir, branchExists) { return __awaiter(this, void 0, void 0, function* () { try { @@ -39,9 +48,23 @@ function generateWorktree(action, worktreedir, branchExists) { yield (0, execute_1.execute)(`git fetch --no-recurse-submodules --depth=1 origin ${action.branch}`, action.workspace, action.silent); } yield (0, execute_1.execute)(`git worktree add --no-checkout --detach ${worktreedir}`, action.workspace, action.silent); - // Create a unique branch name - const uniqueBranchName = `temp-${Date.now()}`; - const checkout = new GitCheckout(uniqueBranchName, `origin/${action.branch}`); + let checkout; + let branchName = action.branch; + /** + * If the branch doesn't exist, we need to create a new branch using a unique name. + */ + try { + checkout = new GitCheckout(branchName); + } + catch (_a) { + console.log('encountered'); + branchName = `temp-${Date.now()}`; + checkout = new GitCheckout(branchName, `origin/${action.branch}`); + } + if (branchExists) { + // There's existing data on the branch to check out + checkout.commitish = `origin/${action.branch}`; + } if (!branchExists || (action.singleCommit && action.branch !== process.env.GITHUB_REF_NAME)) { /* Create a new history if we don't have the branch, or if we want to reset it. @@ -50,12 +73,12 @@ function generateWorktree(action, worktreedir, branchExists) { } yield (0, execute_1.execute)(checkout.toString(), `${action.workspace}/${worktreedir}`, action.silent); if (!branchExists) { - (0, core_1.info)(`Created the ${uniqueBranchName} branch… đŸ”§`); + (0, core_1.info)(`Created the ${branchName} branch… đŸ”§`); // Our index is in HEAD state, reset yield (0, execute_1.execute)('git reset --hard', `${action.workspace}/${worktreedir}`, action.silent); if (!action.singleCommit) { // New history isn't singleCommit, create empty initial commit - yield (0, execute_1.execute)(`git commit --no-verify --allow-empty -m "Initial ${uniqueBranchName} commit"`, `${action.workspace}/${worktreedir}`, action.silent); + yield (0, execute_1.execute)(`git commit --no-verify --allow-empty -m "Initial ${branchName} commit"`, `${action.workspace}/${worktreedir}`, action.silent); } } }