Skip to content

Commit

Permalink
Update worktree.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesIves committed May 18, 2024
1 parent 1e704e6 commit f7ff0eb
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions src/worktree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import {ActionInterface} from './constants'
import {execute} from './execute'
import {extractErrorMessage, suppressSensitiveInformation} from './util'

/**
* Git checkout command.
*/
export class GitCheckout {
orphan = false
commitish?: string | null = null
Expand All @@ -22,10 +25,16 @@ export class GitCheckout {
}
}

/**
* Generates a git worktree.
* @param action - The action interface.
* @param worktreedir - The worktree directory.
* @param branchExists - Bool indicating if the branch exists.
*/
export async function generateWorktree(
action: ActionInterface,
worktreedir: string,
branchExists: unknown
branchExists: boolean | number
): Promise<void> {
try {
info('Creating worktree…')
Expand All @@ -44,13 +53,24 @@ export async function generateWorktree(
action.silent
)

// Create a unique branch name
const uniqueBranchName = `temp-${Date.now()}`
let checkout: GitCheckout
let branchName = action.branch

const checkout = new GitCheckout(
uniqueBranchName,
`origin/${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 {
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 ||
Expand All @@ -68,7 +88,7 @@ export async function generateWorktree(
)

if (!branchExists) {
info(`Created the ${uniqueBranchName} branch… 🔧`)
info(`Created the ${branchName} branch… 🔧`)

// Our index is in HEAD state, reset
await execute(
Expand All @@ -80,7 +100,7 @@ export async function generateWorktree(
if (!action.singleCommit) {
// New history isn't singleCommit, create empty initial commit
await execute(
`git commit --no-verify --allow-empty -m "Initial ${uniqueBranchName} commit"`,
`git commit --no-verify --allow-empty -m "Initial ${branchName} commit"`,
`${action.workspace}/${worktreedir}`,
action.silent
)
Expand Down

0 comments on commit f7ff0eb

Please sign in to comment.