-
-
Notifications
You must be signed in to change notification settings - Fork 360
/
worktree.ts
165 lines (146 loc) · 4.07 KB
/
worktree.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import {info} from '@actions/core'
import {ActionInterface} from './constants'
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
/**
* @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',
'checkout',
this.orphan ? '--orphan' : '-B',
this.branch,
this.commitish || ''
].join(' ')
}
}
/**
* 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: boolean | number
): Promise<void> {
try {
info('Creating worktree…')
if (branchExists) {
await execute(
`git fetch --no-recurse-submodules --depth=1 origin ${action.branch}`,
action.workspace,
action.silent
)
}
await execute(
`git worktree add --no-checkout --detach ${worktreedir}`,
action.workspace,
action.silent
)
let branchName = action.branch
let checkout = new GitCheckout(branchName)
if (branchExists) {
// There's existing data on the branch to check out
checkout.commitish = `origin/${action.branch}`
}
if (
!branchExists ||
(action.singleCommit && action.branch !== process.env.GITHUB_REF_NAME)
) {
/* Create a new history if we don't have the branch, or if we want to reset it.
If the ref name is the same as the branch name, do not attempt to create an orphan of it. */
checkout.orphan = true
}
try {
await execute(
checkout.toString(),
`${action.workspace}/${worktreedir}`,
action.silent
)
} catch (error) {
info(
'Error encountered while checking out branch. Attempting to continue with a new branch name.'
)
branchName = `temp-${Date.now()}`
try {
checkout = new GitCheckout(branchName, `origin/${action.branch}`)
await execute(
checkout.toString(),
`${action.workspace}/${worktreedir}`,
action.silent
)
} catch (error) {
info('Unable to track the origin branch…')
checkout = new GitCheckout(branchName)
await execute(
checkout.toString(),
`${action.workspace}/${worktreedir}`,
action.silent
)
}
}
if (!branchExists) {
info(`Created the ${branchName} branch… 🔧`)
// Our index is in HEAD state, reset
await execute(
'git reset --hard',
`${action.workspace}/${worktreedir}`,
action.silent
)
if (!action.singleCommit) {
// New history isn't singleCommit, create empty initial commit
await execute(
`git commit --no-verify --allow-empty -m "Initial ${branchName} commit"`,
`${action.workspace}/${worktreedir}`,
action.silent
)
}
}
/**
* Ensure that the workspace is a safe directory.
*/
try {
await execute(
`git config --global --add safe.directory "${action.workspace}/${worktreedir}"`,
action.workspace,
action.silent
)
} catch {
info('Unable to set worktree temp directory as a safe directory…')
}
} catch (error) {
throw new Error(
`There was an error creating the worktree: ${suppressSensitiveInformation(
extractErrorMessage(error),
action
)} ❌`
)
}
}