Skip to content

Commit 91fd6ee

Browse files
authored
fix: 🐛 Use commish to make branch unique (#1591)
* fix: 🐛 Use commish to make branch unique * Update worktree.ts * Update worktree.ts * Update worktree.ts * Update worktree.ts * Update worktree.ts
1 parent 5bb052c commit 91fd6ee

File tree

1 file changed

+53
-11
lines changed

1 file changed

+53
-11
lines changed

src/worktree.ts

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,37 @@ import {ActionInterface} from './constants'
33
import {execute} from './execute'
44
import {extractErrorMessage, suppressSensitiveInformation} from './util'
55

6+
/**
7+
* Git checkout command.
8+
*/
69
export class GitCheckout {
10+
/**
11+
* @param orphan - Bool indicating if the branch is an orphan.
12+
*/
713
orphan = false
14+
15+
/**
16+
* @param commitish - The commitish to check out.
17+
*/
818
commitish?: string | null = null
19+
20+
/**
21+
* @param branch - The branch name.
22+
*/
923
branch: string
10-
constructor(branch: string) {
24+
25+
/**
26+
* @param branch - The branch name.
27+
* @param commitish - The commitish to check out.
28+
*/
29+
constructor(branch: string, commitish?: string) {
1130
this.branch = branch
31+
this.commitish = commitish || null
1232
}
33+
34+
/**
35+
* Returns the string representation of the git checkout command.
36+
*/
1337
toString(): string {
1438
return [
1539
'git',
@@ -22,12 +46,15 @@ export class GitCheckout {
2246
}
2347

2448
/**
25-
* Generate the worktree and set initial content if it exists
49+
* Generates a git worktree.
50+
* @param action - The action interface.
51+
* @param worktreedir - The worktree directory.
52+
* @param branchExists - Bool indicating if the branch exists.
2653
*/
2754
export async function generateWorktree(
2855
action: ActionInterface,
2956
worktreedir: string,
30-
branchExists: unknown
57+
branchExists: boolean | number
3158
): Promise<void> {
3259
try {
3360
info('Creating worktree…')
@@ -46,7 +73,8 @@ export async function generateWorktree(
4673
action.silent
4774
)
4875

49-
const checkout = new GitCheckout(action.branch)
76+
let branchName = action.branch
77+
let checkout = new GitCheckout(branchName)
5078

5179
if (branchExists) {
5280
// There's existing data on the branch to check out
@@ -62,14 +90,28 @@ export async function generateWorktree(
6290
checkout.orphan = true
6391
}
6492

65-
await execute(
66-
checkout.toString(),
67-
`${action.workspace}/${worktreedir}`,
68-
action.silent
69-
)
93+
try {
94+
await execute(
95+
checkout.toString(),
96+
`${action.workspace}/${worktreedir}`,
97+
action.silent
98+
)
99+
} catch (error) {
100+
info(
101+
'Error encountered while checking out branch. Attempting to continue with a new branch name.'
102+
)
103+
branchName = `temp-${Date.now()}`
104+
checkout = new GitCheckout(branchName, `origin/${action.branch}`)
105+
106+
await execute(
107+
checkout.toString(),
108+
`${action.workspace}/${worktreedir}`,
109+
action.silent
110+
)
111+
}
70112

71113
if (!branchExists) {
72-
info(`Created the ${action.branch} branch… 🔧`)
114+
info(`Created the ${branchName} branch… 🔧`)
73115

74116
// Our index is in HEAD state, reset
75117
await execute(
@@ -81,7 +123,7 @@ export async function generateWorktree(
81123
if (!action.singleCommit) {
82124
// New history isn't singleCommit, create empty initial commit
83125
await execute(
84-
`git commit --no-verify --allow-empty -m "Initial ${action.branch} commit"`,
126+
`git commit --no-verify --allow-empty -m "Initial ${branchName} commit"`,
85127
`${action.workspace}/${worktreedir}`,
86128
action.silent
87129
)

0 commit comments

Comments
 (0)