Skip to content

Commit

Permalink
feat: friendly git error message
Browse files Browse the repository at this point in the history
  • Loading branch information
lawvs committed Mar 27, 2022
1 parent 9eba335 commit cedd181
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/plugin-commands-publishing/src/gitChecks.ts
Expand Up @@ -12,8 +12,13 @@ export async function isGitRepo () {
}

export async function getCurrentBranch () {
const { stdout } = await execa('git', ['symbolic-ref', '--short', 'HEAD'])
return stdout
try {
const { stdout } = await execa('git', ['symbolic-ref', '--short', 'HEAD'])
return stdout
} catch (_: any) { // eslint-disable-line
// Command will fail with code 1 if the HEAD is detached.
return null
}
}

export async function isWorkingTreeClean () {
Expand Down
9 changes: 9 additions & 0 deletions packages/plugin-commands-publishing/src/publish.ts
Expand Up @@ -116,6 +116,15 @@ export async function handler (
}
const branches = opts.publishBranch ? [opts.publishBranch] : ['master', 'main']
const currentBranch = await getCurrentBranch()
if (currentBranch === null) {
throw new PnpmError(
'GIT_UNKNOWN_BRANCH',
`The Git HEAD may not attached to any branch, but your "publish-branch" is set to "${branches.join('|')}".`,
{
hint: GIT_CHECKS_HINT,
}
)
}
if (!branches.includes(currentBranch)) {
const { confirm } = await prompt({
message: `You're on branch "${currentBranch}" but your "publish-branch" is set to "${branches.join('|')}". \
Expand Down

0 comments on commit cedd181

Please sign in to comment.