Files
pnpm/network/git-utils/src/index.ts
Zoltan Kochan 53668a42b8 fix: support explicit versions and --no-commit-hooks / --no-git-tag-version in pnpm version (#11299)
* fix: support explicit versions and --no-commit-hooks / --no-git-tag-version in `pnpm version`

Registers options under their canonical names so nopt correctly parses the
`--no-*` variants, accepts an explicit semver argument alongside bump types,
and creates a git commit + annotated tag for the bump (honoring
`--no-git-tag-version`, `--no-commit-hooks`, `--sign-git-tag`, `--message`,
and `--tag-version-prefix`). Also fixes `--no-git-checks` which was parsed
incorrectly. Closes #11271.

* chore: add gpgsign and newversion to cspell; set gpg sign config in version test

* fix: address Copilot review feedback on pnpm version

* fix: skip git commit and tag in recursive version runs

* fix: address Copilot review feedback on pnpm version
2026-04-20 00:26:32 +02:00

53 lines
1.4 KiB
TypeScript

import { safeExeca as execa } from 'execa'
// git checks logic is from https://github.com/sindresorhus/np/blob/master/source/git-tasks.js
export interface GitCwdOptions {
cwd?: string
}
export async function isGitRepo (opts: GitCwdOptions = {}): Promise<boolean> {
try {
await execa('git', ['rev-parse', '--git-dir'], { cwd: opts.cwd })
} catch {
return false
}
return true
}
export async function getCurrentBranch (opts: GitCwdOptions = {}): Promise<string | null> {
try {
const { stdout } = await execa('git', ['symbolic-ref', '--short', 'HEAD'], { cwd: opts.cwd })
return stdout as string
} catch {
// Command will fail with code 1 if the HEAD is detached.
return null
}
}
export async function isWorkingTreeClean (opts: GitCwdOptions = {}): Promise<boolean> {
try {
const { stdout: status } = await execa('git', ['status', '--porcelain'], { cwd: opts.cwd })
if (status !== '') {
return false
}
return true
} catch {
return false
}
}
export async function isRemoteHistoryClean (opts: GitCwdOptions = {}): Promise<boolean> {
let history
try { // Gracefully handle no remote set up.
const { stdout } = await execa('git', ['rev-list', '--count', '--left-only', '@{u}...HEAD'], { cwd: opts.cwd })
history = stdout
} catch {
history = null
}
if (history && history !== '0') {
return false
}
return true
}