forked from JamesIves/github-pages-deploy-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworktree.ts
More file actions
96 lines (85 loc) · 2.44 KB
/
worktree.ts
File metadata and controls
96 lines (85 loc) · 2.44 KB
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
import {info} from '@actions/core'
import {ActionInterface} from './constants'
import {execute} from './execute'
import {extractErrorMessage, suppressSensitiveInformation} from './util'
export class GitCheckout {
orphan = false
commitish?: string | null = null
branch: string
constructor(branch: string) {
this.branch = branch
}
toString(): string {
return [
'git',
'checkout',
this.orphan ? '--orphan' : '-B',
this.branch,
this.commitish || ''
].join(' ')
}
}
/* Generate the worktree and set initial content if it exists */
export async function generateWorktree(
action: ActionInterface,
worktreedir: string,
branchExists: unknown
): 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
)
const checkout = new GitCheckout(action.branch)
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
}
await execute(
checkout.toString(),
`${action.workspace}/${worktreedir}`,
action.silent
)
if (!branchExists) {
info(`Created the ${action.branch} 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 ${action.branch} commit"`,
`${action.workspace}/${worktreedir}`,
action.silent
)
}
}
} catch (error) {
throw new Error(
`There was an error creating the worktree: ${suppressSensitiveInformation(
extractErrorMessage(error),
action
)} ❌`
)
}
}