Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 🐛 Use commish to make branch unique #1591

Merged
merged 6 commits into from
May 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 53 additions & 11 deletions src/worktree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,37 @@
import {execute} from './execute'
import {extractErrorMessage, suppressSensitiveInformation} from './util'

/**
* Git checkout command.
*/
export class GitCheckout {
/**
* @param orphan - Bool indicating if the branch is an orphan.
*/
orphan = false

/**
* @param commitish - The commitish to check out.
*/
commitish?: string | null = null

/**
* @param branch - The branch name.
*/
branch: string
constructor(branch: string) {

/**
* @param branch - The branch name.
* @param commitish - The commitish to check out.
*/
constructor(branch: string, commitish?: string) {
this.branch = branch
this.commitish = commitish || null
}

/**
* Returns the string representation of the git checkout command.
*/
toString(): string {
return [
'git',
Expand All @@ -22,12 +46,15 @@
}

/**
* Generate the worktree and set initial content if it exists
* 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 @@ -46,7 +73,8 @@
action.silent
)

const checkout = new GitCheckout(action.branch)
let branchName = action.branch
let checkout = new GitCheckout(branchName)

if (branchExists) {
// There's existing data on the branch to check out
Expand All @@ -62,14 +90,28 @@
checkout.orphan = true
}

await execute(
checkout.toString(),
`${action.workspace}/${worktreedir}`,
action.silent
)
try {
await execute(
checkout.toString(),
`${action.workspace}/${worktreedir}`,
action.silent
)
} catch (error) {
info(

Check warning on line 100 in src/worktree.ts

View check run for this annotation

Codecov / codecov/patch

src/worktree.ts#L100

Added line #L100 was not covered by tests
'Error encountered while checking out branch. Attempting to continue with a new branch name.'
)
branchName = `temp-${Date.now()}`
checkout = new GitCheckout(branchName, `origin/${action.branch}`)

Check warning on line 104 in src/worktree.ts

View check run for this annotation

Codecov / codecov/patch

src/worktree.ts#L103-L104

Added lines #L103 - L104 were not covered by tests

await execute(

Check warning on line 106 in src/worktree.ts

View check run for this annotation

Codecov / codecov/patch

src/worktree.ts#L106

Added line #L106 was not covered by tests
checkout.toString(),
`${action.workspace}/${worktreedir}`,
action.silent
)
}

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

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