Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add git-branch-lockfile config to generate lockfile in each branch #4475

Merged
merged 30 commits into from Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
771978d
feat: add git-branch-lockfile config to generate lockfile in each branch
chengcyber Mar 24, 2022
781addd
feat: add --clean-git-branch-lockfiles arg to pnpm install
chengcyber Mar 24, 2022
3713348
chore: test lockfile name
chengcyber Mar 24, 2022
5ea34d3
chore: add more tests for git branch lockfile
chengcyber Mar 25, 2022
bb014fa
chore: syntax tweak accoding to code review
chengcyber Mar 26, 2022
bfb8a67
refactor: extract git checks to a package @pnpm/git-utils
chengcyber Mar 26, 2022
01bc5f9
test: git-utils
chengcyber Mar 26, 2022
ad8091d
test: @pnpm/core test
chengcyber Mar 26, 2022
7363a2d
refactor: clean-git-branch-lockfiles -> merge-git-branch-lockfiles
chengcyber Mar 26, 2022
e19b2e9
fix: cleanGitBranchLockfiles when install --merge-git-branch-lockfiles
chengcyber Mar 26, 2022
5f39748
refactor: reuse readwantedlockfile logic
chengcyber Mar 27, 2022
3708de4
feat: merge git branch lockfiles unsupported in headless
chengcyber Mar 27, 2022
545ed3b
chore: turn on sourcemap accidently
chengcyber Mar 27, 2022
b2b6151
chore: test file
chengcyber Apr 1, 2022
36878d5
feat: merge-git-branch-lockfiles-branch-pattern
chengcyber Apr 1, 2022
4d6c772
feat: reuse merge lockfile changes logic
chengcyber Apr 5, 2022
124156e
Merge branch 'main' into feat-lockfile-assembly
zkochan May 1, 2022
2a6aca5
Merge remote-tracking branch 'origin/main' into feat-lockfile-assembly
zkochan May 13, 2022
f132ebf
Update packages/lockfile-file/src/gitBranchLockfile.ts
chengcyber Jun 3, 2022
1942722
Merge remote-tracking branch 'origin/main' into feat-lockfile-assembly
zkochan Jun 10, 2022
acccbb2
fix: add missing dependency to @pnpm/config
zkochan Jun 10, 2022
e598218
test: fix
zkochan Jun 10, 2022
eeec313
Merge remote-tracking branch 'origin/main' into feat-lockfile-assembly
zkochan Jun 10, 2022
f53e407
test: fix
zkochan Jun 10, 2022
19b457c
Merge remote-tracking branch 'origin/main' into feat-lockfile-assembly
zkochan Jun 12, 2022
847db0b
fix: engines in git-utils
zkochan Jun 12, 2022
ebb052b
test: fix
zkochan Jun 12, 2022
4df4553
test: fix
zkochan Jun 12, 2022
04361ce
test: fix
zkochan Jun 12, 2022
958df6a
docs: add changeset
zkochan Jun 14, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/config/package.json
Expand Up @@ -34,6 +34,8 @@
"dependencies": {
"@pnpm/constants": "workspace:6.1.0",
"@pnpm/error": "workspace:3.0.1",
"@pnpm/git-utils": "workspace:0.0.0",
"@pnpm/matcher": "workspace:3.0.0",
"@pnpm/pnpmfile": "workspace:2.0.2",
"@pnpm/read-project-manifest": "workspace:3.0.2",
"@pnpm/types": "workspace:8.0.1",
Expand Down
3 changes: 3 additions & 0 deletions packages/config/src/Config.ts
Expand Up @@ -130,6 +130,9 @@ export interface Config {
modulesDir?: string
sharedWorkspaceLockfile?: boolean
useLockfile: boolean
useGitBranchLockfile: boolean
mergeGitBranchLockfiles?: boolean
mergeGitBranchLockfilesBranchPattern?: string[]
globalPnpmfile?: string
npmPath?: string
gitChecks?: boolean
Expand Down
21 changes: 21 additions & 0 deletions packages/config/src/index.ts
Expand Up @@ -4,6 +4,8 @@ import { LAYOUT_VERSION } from '@pnpm/constants'
import PnpmError from '@pnpm/error'
import { requireHooks } from '@pnpm/pnpmfile'
import { safeReadProjectManifestOnly } from '@pnpm/read-project-manifest'
import { getCurrentBranch } from '@pnpm/git-utils'
import matcher from '@pnpm/matcher'
import camelcase from 'camelcase'
import loadNpmConf from '@zkochan/npm-conf'
import npmTypes from '@zkochan/npm-conf/lib/types'
Expand Down Expand Up @@ -36,6 +38,8 @@ export const types = Object.assign({
bail: Boolean,
'cache-dir': String,
'child-concurrency': Number,
'merge-git-branch-lockfiles': Boolean,
'merge-git-branch-lockfiles-branch-pattern': Array,
color: ['always', 'auto', 'never'],
'config-dir': String,
dev: [null, true],
Expand All @@ -53,6 +57,7 @@ export const types = Object.assign({
'global-dir': String,
'global-path': String,
'global-pnpmfile': String,
'git-branch-lockfile': Boolean,
hoist: Boolean,
'hoist-pattern': Array,
'ignore-pnpmfile': Boolean,
Expand Down Expand Up @@ -185,6 +190,7 @@ export default async (
'bitbucket.org',
],
globalconfig: npmDefaults.globalconfig,
'git-branch-lockfile': false,
hoist: true,
'hoist-pattern': ['*'],
'ignore-workspace-root-check': false,
Expand Down Expand Up @@ -261,6 +267,21 @@ export default async (
if (typeof pnpmConfig['packageLock'] === 'boolean') return pnpmConfig['packageLock']
return false
})()
pnpmConfig.useGitBranchLockfile = (() => {
if (typeof pnpmConfig['gitBranchLockfile'] === 'boolean') return pnpmConfig['gitBranchLockfile']
return false
})()
Comment on lines +270 to +273
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed? The default value of use-git-branch-lockfile is set to false at line 184

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to keep the same code style with useLockfile

image

pnpmConfig.mergeGitBranchLockfiles = await (async () => {
if (typeof pnpmConfig['mergeGitBranchLockfiles'] === 'boolean') return pnpmConfig['mergeGitBranchLockfiles']
if (pnpmConfig['mergeGitBranchLockfilesBranchPattern'] != null && pnpmConfig['mergeGitBranchLockfilesBranchPattern'].length > 0) {
const branch = await getCurrentBranch()
if (branch) {
const branchMatcher = matcher(pnpmConfig['mergeGitBranchLockfilesBranchPattern'])
return branchMatcher(branch)
}
}
return undefined
})()
pnpmConfig.pnpmHomeDir = getDataDir(process)

if (cliOptions['global']) {
Expand Down
90 changes: 90 additions & 0 deletions packages/config/test/index.ts
Expand Up @@ -2,6 +2,7 @@
import { promises as fs } from 'fs'
import path from 'path'
import PATH from 'path-name'
import { getCurrentBranch } from '@pnpm/git-utils/test/utils/mock'
import getConfig from '@pnpm/config'
import PnpmError from '@pnpm/error'
import prepare, { prepareEmpty } from '@pnpm/prepare'
Expand Down Expand Up @@ -786,3 +787,92 @@ test('getConfig() sets sideEffectsCacheRead and sideEffectsCacheWrite when side-
expect(config.sideEffectsCacheRead).toBeTruthy()
expect(config.sideEffectsCacheWrite).toBeTruthy()
})

test('respect merge-git-branch-lockfiles-branch-pattern', async () => {
{
const { config } = await getConfig({
cliOptions: {},
packageManager: {
name: 'pnpm',
version: '1.0.0',
},
})

expect(config.mergeGitBranchLockfilesBranchPattern).toBeUndefined()
expect(config.mergeGitBranchLockfiles).toBeUndefined()
}
{
prepareEmpty()

const npmrc = [
'merge-git-branch-lockfiles-branch-pattern[]=main',
'merge-git-branch-lockfiles-branch-pattern[]=release/**',
].join('\n')

await fs.writeFile('.npmrc', npmrc, 'utf8')

const { config } = await getConfig({
cliOptions: {
global: false,
},
packageManager: {
name: 'pnpm',
version: '1.0.0',
},
})

expect(config.mergeGitBranchLockfilesBranchPattern).toEqual(['main', 'release/**'])
}
})

test('getConfig() sets merge-git-branch-lockfiles when branch matches merge-git-branch-lockfiles-branch-pattern', async () => {
prepareEmpty()
{
const npmrc = [
'merge-git-branch-lockfiles-branch-pattern[]=main',
'merge-git-branch-lockfiles-branch-pattern[]=release/**',
].join('\n')

await fs.writeFile('.npmrc', npmrc, 'utf8')

getCurrentBranch.mockReturnValue('develop')
const { config } = await getConfig({
cliOptions: {
global: false,
},
packageManager: {
name: 'pnpm',
version: '1.0.0',
},
})

expect(config.mergeGitBranchLockfilesBranchPattern).toEqual(['main', 'release/**'])
expect(config.mergeGitBranchLockfiles).toBe(false)
}
{
getCurrentBranch.mockReturnValue('main')
const { config } = await getConfig({
cliOptions: {
global: false,
},
packageManager: {
name: 'pnpm',
version: '1.0.0',
},
})
expect(config.mergeGitBranchLockfiles).toBe(true)
}
{
getCurrentBranch.mockReturnValue('release/1.0.0')
const { config } = await getConfig({
cliOptions: {
global: false,
},
packageManager: {
name: 'pnpm',
version: '1.0.0',
},
})
expect(config.mergeGitBranchLockfiles).toBe(true)
}
})
6 changes: 6 additions & 0 deletions packages/config/tsconfig.json
Expand Up @@ -18,6 +18,12 @@
{
"path": "../error"
},
{
"path": "../git-utils"
},
{
"path": "../matcher"
},
{
"path": "../pnpmfile"
},
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/getPeerDependencyIssues.ts
Expand Up @@ -16,6 +16,7 @@ export type ListMissingPeersOptions = Partial<GetContextOptions>
| 'preferWorkspacePackages'
| 'saveWorkspaceProtocol'
| 'storeController'
| 'useGitBranchLockfile'
| 'workspacePackages'
>
& Pick<GetContextOptions, 'storeDir'>
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/install/extendInstallOptions.ts
Expand Up @@ -23,6 +23,8 @@ export interface StrictInstallOptions {
extraBinPaths: string[]
hoistingLimits?: HoistingLimits
useLockfile: boolean
useGitBranchLockfile: boolean
mergeGitBranchLockfiles: boolean
linkWorkspacePackagesDepth: number
lockfileOnly: boolean
fixLockfile: boolean
Expand Down Expand Up @@ -162,6 +164,8 @@ const defaults = async (opts: InstallOptions) => {
process.getuid() !== 0,
update: false,
useLockfile: true,
useGitBranchLockfile: false,
mergeGitBranchLockfiles: false,
userAgent: `${packageManager.name}/${packageManager.version} npm/? node/${process.version} ${process.platform} ${process.arch}`,
verifyStoreIntegrity: true,
workspacePackages: {},
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/install/index.ts
Expand Up @@ -25,6 +25,7 @@ import {
writeCurrentLockfile,
writeLockfiles,
writeWantedLockfile,
cleanGitBranchLockfiles,
} from '@pnpm/lockfile-file'
import { writePnpFile } from '@pnpm/lockfile-to-pnp'
import { extendProjectsWithTargetDirs } from '@pnpm/lockfile-utils'
Expand Down Expand Up @@ -202,6 +203,10 @@ export async function mutateModules (
streamParser.removeListener('data', reporter)
}

if (opts.mergeGitBranchLockfiles) {
await cleanGitBranchLockfiles(ctx.lockfileDir)
}

return result

async function _install (): Promise<UpdatedProject[]> {
Expand Down Expand Up @@ -779,7 +784,7 @@ const _installInContext: InstallFunction = async (projects, ctx, opts) => {
}

const depsStateCache: DepsStateCache = {}
const lockfileOpts = { forceSharedFormat: opts.forceSharedLockfile }
const lockfileOpts = { forceSharedFormat: opts.forceSharedLockfile, useGitBranchLockfile: opts.useGitBranchLockfile, mergeGitBranchLockfiles: opts.mergeGitBranchLockfiles }
if (!opts.lockfileOnly && opts.enableModulesDir) {
const result = await linkPackages(
projects,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/link/index.ts
Expand Up @@ -129,7 +129,7 @@ export default async function link (
} else {
newPkg = opts.manifest
}
const lockfileOpts = { forceSharedFormat: opts.forceSharedLockfile }
const lockfileOpts = { forceSharedFormat: opts.forceSharedLockfile, useGitBranchLockfile: opts.useGitBranchLockfile, mergeGitBranchLockfiles: opts.mergeGitBranchLockfiles }
if (opts.useLockfile) {
await writeLockfiles({
currentLockfile: updatedCurrentLockfile,
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/link/options.ts
Expand Up @@ -29,6 +29,9 @@ interface StrictLinkOptions {

publicHoistPattern: string[] | undefined
forcePublicHoistPattern: boolean

useGitBranchLockfile: boolean
mergeGitBranchLockfiles: boolean
}

export type LinkOptions =
Expand Down