fix(git-resolver): install Git-hosted dependency that has non-master default branch (#3506)

close #3199, close #3430, close #3503
This commit is contained in:
Zoltan Kochan
2021-06-05 22:46:23 +03:00
committed by GitHub
parent 5f1e6a7c38
commit 69ffc40996
3 changed files with 26 additions and 8 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/git-resolver": minor
---
It should be possible to install a Git-hosted dependency that names the default branch not "master".

View File

@@ -16,7 +16,7 @@ export default function (
if (parsedSpec == null) return null
const pref = parsedSpec.gitCommittish == null || parsedSpec.gitCommittish === ''
? 'master'
? 'HEAD'
: parsedSpec.gitCommittish
const commit = await resolveRef(parsedSpec.fetchSpec, pref, parsedSpec.gitRange)
let resolution
@@ -58,16 +58,17 @@ function resolveVTags (vTags: string[], range: string) {
}
async function getRepoRefs (repo: string, ref: string | null) {
const gitArgs = ['ls-remote', '--refs', repo]
const gitArgs = [repo]
if (ref !== 'HEAD') {
gitArgs.unshift('--refs')
}
if (ref) {
gitArgs.push(ref)
}
// graceful-git by default retries 10 times, reduce to single retry
const result = await git(gitArgs, { retries: 1 })
const result = await git(['ls-remote', ...gitArgs], { retries: 1 })
const refs = result.stdout.split('\n').reduce((obj: object, line: string) => {
const commitAndRef = line.split('\t')
const commit = commitAndRef[0]
const refName = commitAndRef[1]
const [commit, refName] = line.split('\t')
obj[refName] = commit
return obj
}, {})

View File

@@ -36,6 +36,18 @@ test('resolveFromGit() with no commit', async () => {
}
})
test('resolveFromGit() with no commit, when main branch is not master', async () => {
const resolveResult = await resolveFromGit({ pref: 'zoli-forks/cmd-shim' })
expect(resolveResult).toStrictEqual({
id: 'github.com/zoli-forks/cmd-shim/a00a83a1593edb6e395d3ce41f2ef70edf7e2cf5',
normalizedPref: 'github:zoli-forks/cmd-shim',
resolution: {
tarball: 'https://codeload.github.com/zoli-forks/cmd-shim/tar.gz/a00a83a1593edb6e395d3ce41f2ef70edf7e2cf5',
},
resolvedVia: 'git-repository',
})
})
test('resolveFromGit() with branch', async () => {
const resolveResult = await resolveFromGit({ pref: 'zkochan/is-negative#canary' })
expect(resolveResult).toStrictEqual({
@@ -353,10 +365,10 @@ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\trefs/heads/master\
})
const resolveResult = await resolveFromGit({ pref: 'git+https://0000000000000000000000000000000000000000:x-oauth-basic@github.com/foo/bar.git' })
expect(resolveResult).toStrictEqual({
id: '0000000000000000000000000000000000000000+x-oauth-basic@github.com/foo/bar/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
id: '0000000000000000000000000000000000000000+x-oauth-basic@github.com/foo/bar/0000000000000000000000000000000000000000',
normalizedPref: 'git+https://0000000000000000000000000000000000000000:x-oauth-basic@github.com/foo/bar.git',
resolution: {
commit: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
commit: '0000000000000000000000000000000000000000',
repo: 'https://0000000000000000000000000000000000000000:x-oauth-basic@github.com/foo/bar.git',
type: 'git',
},