Skip to content

Commit

Permalink
Merge branch 'releases/v4-flow' of https://github.com/JamesIves/githu…
Browse files Browse the repository at this point in the history
…b-pages-deploy-action into releases/v4-flow
  • Loading branch information
JamesIves committed May 18, 2024
2 parents c61fe4c + 2d5a05e commit 66364f2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
11 changes: 10 additions & 1 deletion lib/worktree.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { ActionInterface } from './constants';
/**
* Git checkout command.
*/
export declare class GitCheckout {
orphan: boolean;
commitish?: string | null;
branch: string;
constructor(branch: string, commitish?: string);
toString(): string;
}
export declare function generateWorktree(action: ActionInterface, worktreedir: string, branchExists: unknown): Promise<void>;
/**
* 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<void>;
33 changes: 28 additions & 5 deletions lib/worktree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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.
Expand All @@ -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);
}
}
}
Expand Down

0 comments on commit 66364f2

Please sign in to comment.