github-pages-deploy-action/src/worktree.ts
Lucas dos Santos Abreu 460c7b8081
fix issue #866 - upgrading to typescript 4.4.3 (#896)
* Bump typescript from 4.3.5 to 4.4.3

Bumps [typescript](https://github.com/Microsoft/TypeScript) from 4.3.5 to 4.4.3.
- [Release notes](https://github.com/Microsoft/TypeScript/releases)
- [Commits](https://github.com/Microsoft/TypeScript/compare/v4.3.5...v4.4.3)

---
updated-dependencies:
- dependency-name: typescript
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* (fix): extract error message

* (fix): error is unknown on __tests__

* (fix): add cover

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-10-06 23:20:55 +01:00

86 lines
2.3 KiB
TypeScript

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) {
// Create a new history if we don't have the branch, or if we want to reset 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
)}`
)
}
}