diff --git a/.changeset/mean-zoos-move.md b/.changeset/mean-zoos-move.md new file mode 100644 index 00000000000..4687c0492bc --- /dev/null +++ b/.changeset/mean-zoos-move.md @@ -0,0 +1,5 @@ +--- +"@pnpm/plugin-commands-publishing": patch +--- + +Show friendly error message when get current git branch name error. diff --git a/packages/plugin-commands-publishing/src/gitChecks.ts b/packages/plugin-commands-publishing/src/gitChecks.ts index dbabdb6814b..017b5d8029d 100644 --- a/packages/plugin-commands-publishing/src/gitChecks.ts +++ b/packages/plugin-commands-publishing/src/gitChecks.ts @@ -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 () { diff --git a/packages/plugin-commands-publishing/src/publish.ts b/packages/plugin-commands-publishing/src/publish.ts index aae9c42cf52..282a438472d 100644 --- a/packages/plugin-commands-publishing/src/publish.ts +++ b/packages/plugin-commands-publishing/src/publish.ts @@ -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('|')}". \ diff --git a/packages/plugin-commands-publishing/test/gitChecks.ts b/packages/plugin-commands-publishing/test/gitChecks.ts index 781a348223b..3ae5be0be46 100644 --- a/packages/plugin-commands-publishing/test/gitChecks.ts +++ b/packages/plugin-commands-publishing/test/gitChecks.ts @@ -135,3 +135,28 @@ test('publish: fails git check if branch is not up-to-date', async () => { new PnpmError('GIT_NOT_LATEST', 'Remote history differs. Please pull changes.') ) }) + +test('publish: fails git check if HEAD is detached', async () => { + prepare({ + name: 'test-publish-package.json', + version: '0.0.0', + }) + + await execa('git', ['init']) + await execa('git', ['config', 'user.email', 'x@y.z']) + await execa('git', ['config', 'user.name', 'xyz']) + await execa('git', ['add', '*']) + await execa('git', ['commit', '-m', 'init', '--no-gpg-sign']) + await execa('git', ['commit', '--allow-empty', '--allow-empty-message', '-m', '', '--no-gpg-sign']) + await execa('git', ['checkout', 'HEAD~1']) + + await expect( + publish.handler({ + ...DEFAULT_OPTS, + argv: { original: ['publish', ...CREDENTIALS] }, + dir: process.cwd(), + }, []) + ).rejects.toThrow( + new PnpmError('GIT_UNKNOWN_BRANCH', 'The Git HEAD may not attached to any branch, but your "publish-branch" is set to "master|main".') + ) +})