Skip to content

Commit

Permalink
feat: add sub folder query support for git url
Browse files Browse the repository at this point in the history
Close pnpm#7483
  • Loading branch information
RexSkz committed Jan 4, 2024
1 parent c0182b4 commit 64ccf50
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
4 changes: 3 additions & 1 deletion fetching/git-fetcher/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ export function createGitFetcher (createOpts: CreateGitFetcherOptions) {
// the linking of files to the store is in progress.
return addFilesFromDir({
cafsDir: cafs.cafsDir,
dir: tempLocation,
dir: resolution.path
? path.join(tempLocation, resolution.path)
: tempLocation,
filesIndexFile: opts.filesIndexFile,
readManifest: opts.readManifest,
pkg: opts.pkg,
Expand Down
1 change: 1 addition & 0 deletions resolving/git-resolver/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function createGitResolver (
resolution = {
commit,
repo: parsedSpec.fetchSpec,
path: parsedSpec.path,
type: 'git',
} as ({ type: string } & object)
}
Expand Down
23 changes: 22 additions & 1 deletion resolving/git-resolver/src/parsePref.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// cspell:ignore sshurl
import urlLib, { URL } from 'url'
import urlLib, { URL, type URLSearchParams } from 'url'
import { fetch } from '@pnpm/fetch'

import git from 'graceful-git'
Expand All @@ -17,6 +17,7 @@ export interface HostedPackageSpec {
normalizedPref: string
gitCommittish: string | null
gitRange?: string
path?: string
}

const gitProtocols = new Set([
Expand Down Expand Up @@ -48,13 +49,15 @@ export async function parsePref (pref: string): Promise<HostedPackageSpec | null
fetchSpec: urlToFetchSpec(url),
normalizedPref: pref,
...setGitCommittish(committish),
...setSubFolder(pref),
}
}
return null
}

function urlToFetchSpec (url: URL) {
url.hash = ''
url.search = ''
const fetchSpec = urlLib.format(url)
if (fetchSpec.startsWith('git+')) {
return fetchSpec.slice(4)
Expand Down Expand Up @@ -88,6 +91,7 @@ async function fromHostedGit (hosted: any): Promise<HostedPackageSpec> { // esli
},
normalizedPref: `git+${httpsUrl}`,
...setGitCommittish(hosted.committish),
...setSubFolder(httpsUrl),
}
} else {
try {
Expand Down Expand Up @@ -122,6 +126,7 @@ async function fromHostedGit (hosted: any): Promise<HostedPackageSpec> { // esli
},
normalizedPref: hosted.shortcut(),
...setGitCommittish(hosted.committish),
...setSubFolder(fetchSpec!),
}
}

Expand Down Expand Up @@ -153,6 +158,22 @@ function setGitCommittish (committish: string | null) {
return { gitCommittish: committish }
}

function setSubFolder (url: string) {
let query: URLSearchParams
try {
query = new urlLib.URL(url).searchParams
} catch (err) {
// maybe it's not a valid url, just ignore it
return {}
}

const path = query.get('path') ?? null
if (Array.isArray(path)) {
throw new Error(`Only one sub folder is supported, but got ${path.length}: ${path.join(', ')}`)
}
return path ? { path } : {}
}

// handle SCP-like URLs
// see https://github.com/yarnpkg/yarn/blob/5682d55/src/util/git.js#L103
function correctUrl (gitUrl: string) {
Expand Down
1 change: 1 addition & 0 deletions resolving/resolver-base/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface DirectoryResolution {
export interface GitResolution {
commit: string
repo: string
path?: string
type: 'git'
}

Expand Down

0 comments on commit 64ccf50

Please sign in to comment.