@@ -3,13 +3,37 @@ import {ActionInterface} from './constants'
3
3
import { execute } from './execute'
4
4
import { extractErrorMessage , suppressSensitiveInformation } from './util'
5
5
6
+ /**
7
+ * Git checkout command.
8
+ */
6
9
export class GitCheckout {
10
+ /**
11
+ * @param orphan - Bool indicating if the branch is an orphan.
12
+ */
7
13
orphan = false
14
+
15
+ /**
16
+ * @param commitish - The commitish to check out.
17
+ */
8
18
commitish ?: string | null = null
19
+
20
+ /**
21
+ * @param branch - The branch name.
22
+ */
9
23
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 ) {
11
30
this . branch = branch
31
+ this . commitish = commitish || null
12
32
}
33
+
34
+ /**
35
+ * Returns the string representation of the git checkout command.
36
+ */
13
37
toString ( ) : string {
14
38
return [
15
39
'git' ,
@@ -22,12 +46,15 @@ export class GitCheckout {
22
46
}
23
47
24
48
/**
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.
26
53
*/
27
54
export async function generateWorktree (
28
55
action : ActionInterface ,
29
56
worktreedir : string ,
30
- branchExists : unknown
57
+ branchExists : boolean | number
31
58
) : Promise < void > {
32
59
try {
33
60
info ( 'Creating worktree…' )
@@ -46,7 +73,8 @@ export async function generateWorktree(
46
73
action . silent
47
74
)
48
75
49
- const checkout = new GitCheckout ( action . branch )
76
+ let branchName = action . branch
77
+ let checkout = new GitCheckout ( branchName )
50
78
51
79
if ( branchExists ) {
52
80
// There's existing data on the branch to check out
@@ -62,14 +90,28 @@ export async function generateWorktree(
62
90
checkout . orphan = true
63
91
}
64
92
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
+ }
70
112
71
113
if ( ! branchExists ) {
72
- info ( `Created the ${ action . branch } branch… 🔧` )
114
+ info ( `Created the ${ branchName } branch… 🔧` )
73
115
74
116
// Our index is in HEAD state, reset
75
117
await execute (
@@ -81,7 +123,7 @@ export async function generateWorktree(
81
123
if ( ! action . singleCommit ) {
82
124
// New history isn't singleCommit, create empty initial commit
83
125
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"` ,
85
127
`${ action . workspace } /${ worktreedir } ` ,
86
128
action . silent
87
129
)
0 commit comments