2021-02-08 12:58:32 +08:00
|
|
|
"use strict";
|
|
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
exports.generateWorktree = exports.GitCheckout = void 0;
|
|
|
|
const core_1 = require("@actions/core");
|
|
|
|
const execute_1 = require("./execute");
|
|
|
|
const util_1 = require("./util");
|
|
|
|
class GitCheckout {
|
|
|
|
constructor(branch) {
|
|
|
|
this.orphan = false;
|
|
|
|
this.commitish = null;
|
|
|
|
this.branch = branch;
|
|
|
|
}
|
|
|
|
toString() {
|
|
|
|
return [
|
|
|
|
'git',
|
|
|
|
'checkout',
|
|
|
|
this.orphan ? '--orphan' : '-B',
|
|
|
|
this.branch,
|
|
|
|
this.commitish || ''
|
|
|
|
].join(' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exports.GitCheckout = GitCheckout;
|
|
|
|
/* Generate the worktree and set initial content if it exists */
|
|
|
|
function generateWorktree(action, worktreedir, branchExists) {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
try {
|
2021-11-18 20:58:07 +08:00
|
|
|
(0, core_1.info)('Creating worktree…');
|
2021-02-08 12:58:32 +08:00
|
|
|
if (branchExists) {
|
2021-11-18 20:58:07 +08:00
|
|
|
yield (0, execute_1.execute)(`git fetch --no-recurse-submodules --depth=1 origin ${action.branch}`, action.workspace, action.silent);
|
2021-02-08 12:58:32 +08:00
|
|
|
}
|
2021-11-18 20:58:07 +08:00
|
|
|
yield (0, execute_1.execute)(`git worktree add --no-checkout --detach ${worktreedir}`, action.workspace, action.silent);
|
2021-02-08 12:58:32 +08:00
|
|
|
const checkout = new GitCheckout(action.branch);
|
|
|
|
if (branchExists) {
|
|
|
|
// There's existing data on the branch to check out
|
|
|
|
checkout.commitish = `origin/${action.branch}`;
|
|
|
|
}
|
|
|
|
if (!branchExists || action.singleCommit) {
|
|
|
|
// Create a new history if we don't have the branch, or if we want to reset it
|
|
|
|
checkout.orphan = true;
|
|
|
|
}
|
2021-11-18 20:58:07 +08:00
|
|
|
yield (0, execute_1.execute)(checkout.toString(), `${action.workspace}/${worktreedir}`, action.silent);
|
2021-02-08 12:58:32 +08:00
|
|
|
if (!branchExists) {
|
2021-11-18 20:58:07 +08:00
|
|
|
(0, core_1.info)(`Created the ${action.branch} branch… 🔧`);
|
2021-02-08 12:58:32 +08:00
|
|
|
// Our index is in HEAD state, reset
|
2021-11-18 20:58:07 +08:00
|
|
|
yield (0, execute_1.execute)('git reset --hard', `${action.workspace}/${worktreedir}`, action.silent);
|
2021-02-08 12:58:32 +08:00
|
|
|
if (!action.singleCommit) {
|
|
|
|
// New history isn't singleCommit, create empty initial commit
|
2021-11-18 20:58:07 +08:00
|
|
|
yield (0, execute_1.execute)(`git commit --no-verify --allow-empty -m "Initial ${action.branch} commit"`, `${action.workspace}/${worktreedir}`, action.silent);
|
2021-02-08 12:58:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (error) {
|
2021-11-18 20:58:07 +08:00
|
|
|
throw new Error(`There was an error creating the worktree: ${(0, util_1.suppressSensitiveInformation)((0, util_1.extractErrorMessage)(error), action)} ❌`);
|
2021-02-08 12:58:32 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
exports.generateWorktree = generateWorktree;
|