With gitBranchLockfile, an install named its lockfile after the branch HEAD pointed at. A detached HEAD, which is what a CI checkout of a commit SHA produces, names none, so the install read only the shared pnpm-lock.yaml and failed with ERR_PNPM_NO_LOCKFILE even though the lockfile of a branch containing the commit sat next to it. When HEAD is detached, the wanted-lockfile read now falls back to the lockfiles of the local and remote-tracking branches containing the commit, tried before the shared one. A CI checkout of a SHA usually has only the remote-tracking ref it fetched, so those count too. The write target is unchanged: a branch containing HEAD need not have HEAD at its tip, so a detached install still writes the shared lockfile, as under mergeGitBranchLockfiles. The fix covers both the TypeScript v11 CLI and the Rust v12 CLI. Close pnpm/pnpm#7672 --------- Co-authored-by: gaishijie2025 <gaishijie@isrc.iscas.ac.cn> Co-authored-by: Zoltan Kochan <z@kochan.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
213 lines
7.9 KiB
TypeScript
213 lines
7.9 KiB
TypeScript
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
|
|
import { expect, test } from '@jest/globals'
|
|
import { getBranchesContainingHead, getCurrentBranch, isGitRepo, isHeadDetached, isWorkingTreeClean, nonInteractiveGitEnv, nonInteractiveGitSubmoduleEnv, safeGitEnv } from '@pnpm/network.git-utils'
|
|
import { safeExeca as execa } from 'execa'
|
|
import { temporaryDirectory } from 'tempy'
|
|
|
|
test('isGitRepo', async () => {
|
|
const tempDir = temporaryDirectory()
|
|
process.chdir(tempDir)
|
|
|
|
await expect(isGitRepo()).resolves.toBe(false)
|
|
|
|
await execa('git', ['init'])
|
|
|
|
await expect(isGitRepo()).resolves.toBe(true)
|
|
})
|
|
|
|
test('getCurrentBranch', async () => {
|
|
const tempDir = temporaryDirectory()
|
|
process.chdir(tempDir)
|
|
|
|
await execa('git', ['init'])
|
|
await execa('git', ['checkout', '-b', 'foo'])
|
|
|
|
await expect(getCurrentBranch()).resolves.toBe('foo')
|
|
await expect(isHeadDetached()).resolves.toBe(false)
|
|
})
|
|
|
|
test('getCurrentBranch reads branch from .git/HEAD without spawning git', async () => {
|
|
const tempDir = temporaryDirectory()
|
|
|
|
await execa('git', ['init'], { cwd: tempDir })
|
|
await execa('git', ['checkout', '-b', 'bar'], { cwd: tempDir })
|
|
|
|
await expect(getCurrentBranch({ cwd: tempDir })).resolves.toBe('bar')
|
|
})
|
|
|
|
test('getCurrentBranch returns null for detached HEAD', async () => {
|
|
const tempDir = temporaryDirectory()
|
|
|
|
await execa('git', ['init'], { cwd: tempDir })
|
|
await execa('git', ['checkout', '-b', 'main'], { cwd: tempDir })
|
|
await execa('git', ['config', 'user.email', 'test@test.com'], { cwd: tempDir })
|
|
await execa('git', ['config', 'user.name', 'test'], { cwd: tempDir })
|
|
await execa('git', ['config', 'commit.gpgsign', 'false'], { cwd: tempDir })
|
|
await execa('git', ['commit', '--allow-empty', '-m', 'init'], { cwd: tempDir })
|
|
await expect(isHeadDetached({ cwd: tempDir })).resolves.toBe(false)
|
|
await execa('git', ['checkout', '--detach', 'HEAD'], { cwd: tempDir })
|
|
|
|
await expect(getCurrentBranch({ cwd: tempDir })).resolves.toBeNull()
|
|
await expect(isHeadDetached({ cwd: tempDir })).resolves.toBe(true)
|
|
const subdir = path.join(tempDir, 'subdir')
|
|
fs.mkdirSync(subdir)
|
|
await expect(isHeadDetached({ cwd: subdir })).resolves.toBe(true)
|
|
})
|
|
|
|
test('getCurrentBranch returns null outside a git repo', async () => {
|
|
const tempDir = temporaryDirectory()
|
|
|
|
await expect(getCurrentBranch({ cwd: tempDir })).resolves.toBeNull()
|
|
await expect(isHeadDetached({ cwd: tempDir })).resolves.toBe(false)
|
|
})
|
|
|
|
test('getBranchesContainingHead lists local and remote-tracking branches by branch name', async () => {
|
|
const tempDir = temporaryDirectory()
|
|
const git = (...args: string[]) => execa('git', args, { cwd: tempDir })
|
|
|
|
await git('init', '-b', 'main')
|
|
await git('config', 'user.email', 'test@test.com')
|
|
await git('config', 'user.name', 'test')
|
|
await git('config', 'commit.gpgsign', 'false')
|
|
await git('commit', '--allow-empty', '-m', 'init')
|
|
await git('checkout', '-b', 'feature')
|
|
await git('commit', '--allow-empty', '-m', 'feature')
|
|
await git('update-ref', 'refs/remotes/origin/feature', 'HEAD')
|
|
await git('update-ref', 'refs/remotes/upstream/fix/login', 'HEAD')
|
|
await git('symbolic-ref', 'refs/remotes/origin/HEAD', 'refs/remotes/origin/feature')
|
|
await git('checkout', '--detach')
|
|
|
|
await expect(getBranchesContainingHead({ cwd: tempDir })).resolves.toStrictEqual(['feature', 'fix/login'])
|
|
|
|
await git('branch', '-D', 'feature')
|
|
|
|
await expect(getBranchesContainingHead({ cwd: tempDir })).resolves.toStrictEqual(['feature', 'fix/login'])
|
|
})
|
|
|
|
test('getBranchesContainingHead returns an empty array outside a git repo', async () => {
|
|
await expect(getBranchesContainingHead({ cwd: temporaryDirectory() })).resolves.toStrictEqual([])
|
|
})
|
|
|
|
test('isWorkingTreeClean', async () => {
|
|
const tempDir = temporaryDirectory()
|
|
process.chdir(tempDir)
|
|
|
|
await execa('git', ['init'])
|
|
|
|
await expect(isWorkingTreeClean()).resolves.toBe(true)
|
|
|
|
fs.writeFileSync(path.join(tempDir, 'foo'), 'foo')
|
|
|
|
await expect(isWorkingTreeClean()).resolves.toBe(false)
|
|
})
|
|
|
|
test('nonInteractiveGitEnv disables git and ssh prompts', async () => {
|
|
await withIsolatedGitConfig(async () => {
|
|
await expect(nonInteractiveGitEnv()).resolves.toMatchObject({
|
|
GIT_TERMINAL_PROMPT: '0',
|
|
GIT_SSH_COMMAND: 'ssh -o BatchMode=yes',
|
|
})
|
|
})
|
|
})
|
|
|
|
test('nonInteractiveGitEnv keeps an ssh command selected through the environment', async () => {
|
|
await withIsolatedGitConfig(async () => {
|
|
process.env.GIT_SSH_COMMAND = 'ssh -i ~/.ssh/deploy_key'
|
|
await expect(nonInteractiveGitEnv()).resolves.toMatchObject({
|
|
GIT_SSH_COMMAND: 'ssh -i ~/.ssh/deploy_key',
|
|
GIT_TERMINAL_PROMPT: '0',
|
|
})
|
|
|
|
delete process.env.GIT_SSH_COMMAND
|
|
process.env.GIT_SSH = 'plink'
|
|
const gitEnv = await nonInteractiveGitEnv()
|
|
expect(gitEnv).toMatchObject({ GIT_SSH: 'plink', GIT_TERMINAL_PROMPT: '0' })
|
|
expect(gitEnv).not.toHaveProperty('GIT_SSH_COMMAND')
|
|
})
|
|
})
|
|
|
|
test('nonInteractiveGitEnv keeps an ssh command selected through git configuration', async () => {
|
|
await withIsolatedGitConfig(async () => {
|
|
await execa('git', ['init'])
|
|
await execa('git', ['config', 'core.sshCommand', 'ssh -i ~/.ssh/deploy_key'])
|
|
|
|
const gitEnv = await nonInteractiveGitEnv()
|
|
expect(gitEnv).toMatchObject({ GIT_TERMINAL_PROMPT: '0' })
|
|
expect(gitEnv).not.toHaveProperty('GIT_SSH_COMMAND')
|
|
})
|
|
})
|
|
|
|
test('nonInteractiveGitEnv reads the git configuration in effect in the given directory', async () => {
|
|
await withIsolatedGitConfig(async () => {
|
|
const repoWithSshCommand = temporaryDirectory()
|
|
await execa('git', ['init'], { cwd: repoWithSshCommand })
|
|
await execa('git', ['config', 'core.sshCommand', 'ssh -i ~/.ssh/deploy_key'], { cwd: repoWithSshCommand })
|
|
|
|
await expect(nonInteractiveGitEnv()).resolves.toMatchObject({ GIT_SSH_COMMAND: 'ssh -o BatchMode=yes' })
|
|
await expect(nonInteractiveGitEnv({ cwd: repoWithSshCommand })).resolves.not.toHaveProperty('GIT_SSH_COMMAND')
|
|
})
|
|
})
|
|
|
|
test('nonInteractiveGitSubmoduleEnv disables the file transport by default', async () => {
|
|
await withIsolatedGitConfig(async () => {
|
|
await expect(nonInteractiveGitSubmoduleEnv()).resolves.toMatchObject({
|
|
GIT_ALLOW_PROTOCOL: 'git:http:https:ssh',
|
|
})
|
|
})
|
|
})
|
|
|
|
test('nonInteractiveGitSubmoduleEnv honors configured protocol permissions', async () => {
|
|
await withIsolatedGitConfig(async () => {
|
|
await execa('git', ['init'])
|
|
await execa('git', ['config', 'protocol.file.allow', 'always'])
|
|
await execa('git', ['config', 'protocol.https.allow', 'never'])
|
|
|
|
await expect(nonInteractiveGitSubmoduleEnv()).resolves.toMatchObject({
|
|
GIT_ALLOW_PROTOCOL: 'file:git:http:ssh',
|
|
})
|
|
})
|
|
})
|
|
|
|
test('safeGitEnv removes repository location and index overrides', () => {
|
|
const env = safeGitEnv({
|
|
GIT_DIR: '/tmp/git-dir',
|
|
GIT_WORK_TREE: '/tmp/work-tree',
|
|
GIT_COMMON_DIR: '/tmp/common-dir',
|
|
GIT_INDEX_FILE: '/tmp/index',
|
|
GIT_OBJECT_DIRECTORY: '/tmp/objects',
|
|
GIT_ALTERNATE_OBJECT_DIRECTORIES: '/tmp/alt-objects',
|
|
KEEP_ME: 'true',
|
|
})
|
|
expect(env).toEqual({ KEEP_ME: 'true' })
|
|
})
|
|
|
|
/**
|
|
* Runs `fn` in a fresh directory where only the git configuration written by
|
|
* the test is in effect, with the ssh selection variables unset.
|
|
*/
|
|
async function withIsolatedGitConfig (fn: () => Promise<void>): Promise<void> {
|
|
const tempDir = temporaryDirectory()
|
|
process.chdir(tempDir)
|
|
const names = ['GIT_ALLOW_PROTOCOL', 'GIT_SSH', 'GIT_SSH_COMMAND', 'GIT_CONFIG_GLOBAL', 'GIT_CONFIG_NOSYSTEM']
|
|
const original = Object.fromEntries(names.map((name) => [name, process.env[name]]))
|
|
delete process.env.GIT_SSH
|
|
delete process.env.GIT_SSH_COMMAND
|
|
delete process.env.GIT_ALLOW_PROTOCOL
|
|
process.env.GIT_CONFIG_GLOBAL = path.join(tempDir, 'empty-gitconfig')
|
|
process.env.GIT_CONFIG_NOSYSTEM = '1'
|
|
fs.writeFileSync(process.env.GIT_CONFIG_GLOBAL, '')
|
|
try {
|
|
await fn()
|
|
} finally {
|
|
for (const [name, value] of Object.entries(original)) {
|
|
if (value === undefined) {
|
|
delete process.env[name]
|
|
} else {
|
|
process.env[name] = value
|
|
}
|
|
}
|
|
}
|
|
}
|