From 3efd6d9a934aed99342915484ddf15a861478fb6 Mon Sep 17 00:00:00 2001 From: EscapeB Date: Thu, 13 Jun 2024 12:01:07 +0800 Subject: [PATCH 1/2] fix: fix pattern match for dot path --- .../main_2024-06-13-03-59.json | 10 +++++ .../src/executor/modules/check.ts | 37 +++++++------------ 2 files changed, 24 insertions(+), 23 deletions(-) create mode 100644 common/changes/rush-git-lfs-plugin/main_2024-06-13-03-59.json diff --git a/common/changes/rush-git-lfs-plugin/main_2024-06-13-03-59.json b/common/changes/rush-git-lfs-plugin/main_2024-06-13-03-59.json new file mode 100644 index 0000000..6430050 --- /dev/null +++ b/common/changes/rush-git-lfs-plugin/main_2024-06-13-03-59.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "rush-git-lfs-plugin", + "comment": "fix pattern match for paths that contains dot.", + "type": "patch" + } + ], + "packageName": "rush-git-lfs-plugin" +} \ No newline at end of file diff --git a/rush-plugins/rush-git-lfs-plugin/src/executor/modules/check.ts b/rush-plugins/rush-git-lfs-plugin/src/executor/modules/check.ts index d5169ef..509a13f 100644 --- a/rush-plugins/rush-git-lfs-plugin/src/executor/modules/check.ts +++ b/rush-plugins/rush-git-lfs-plugin/src/executor/modules/check.ts @@ -13,7 +13,7 @@ import { toRelativePath, toAbsolutePath, RushRootFolder, - findFileBelongProject, + findFileBelongProject } from '../../helpers/file-path-analyser'; export interface IGitLFSCheckModuleContext extends IGitLFSModuleContext { @@ -32,7 +32,7 @@ export interface IGitLFSCheckModuleFileError { export const enum GitLFSCheckModuleErrorType { FileNeedToBeTrackedByLFS, FixFileLFSStatusFail, - GitAddFail, + GitAddFail } export class GitLFSCheckModule extends GitLFSBaseModule { @@ -40,7 +40,7 @@ export class GitLFSCheckModule extends GitLFSBaseModule { const entries: [string, number][] = Object.entries(pattern); for (const [ptn, size] of entries) { - if (minimatch(toRelativePath(p), ptn)) { + if (minimatch(toRelativePath(p), ptn, { dot: true })) { const stat: fse.Stats = fse.statSync(toAbsolutePath(p)); return stat.size > size; } @@ -51,12 +51,9 @@ export class GitLFSCheckModule extends GitLFSBaseModule { public isTrackedByLFS = (p: string): boolean => { /* use git check-attr to test if a file was managed by git-lfs */ try { - const { exitCode, stdout } = execa.commandSync( - `git check-attr --all -- ${toRelativePath(p)}`, - { - cwd: RushRootFolder, - } - ); + const { exitCode, stdout } = execa.commandSync(`git check-attr --all -- ${toRelativePath(p)}`, { + cwd: RushRootFolder + }); return exitCode === 0 && stdout.includes('filter: lfs'); } catch (e) { return false; @@ -70,7 +67,7 @@ export class GitLFSCheckModule extends GitLFSBaseModule { const runCWD: string = typeof project === 'undefined' ? RushRootFolder : project.projectFolder; const relativePath: string = path.relative(runCWD, toRelativePath(p)); const { exitCode } = execa.commandSync(`git lfs track ${relativePath}`, { - cwd: toAbsolutePath(runCWD), + cwd: toAbsolutePath(runCWD) }); if (exitCode !== 0) { throw GitLFSCheckModuleErrorType.FixFileLFSStatusFail; @@ -84,7 +81,7 @@ export class GitLFSCheckModule extends GitLFSBaseModule { public addFileToGit = (p: string): void => { terminal.writeVerboseLine(withPrefix(`Trying git add on ${toRelativePath(p)}`)); const { exitCode } = execa.commandSync(`git add ${toRelativePath(p)}`, { - cwd: toAbsolutePath(RushRootFolder), + cwd: toAbsolutePath(RushRootFolder) }); if (exitCode !== 0) { throw GitLFSCheckModuleErrorType.GitAddFail; @@ -97,7 +94,7 @@ export class GitLFSCheckModule extends GitLFSBaseModule { result, fix, option: { checkPattern }, - spinner, + spinner } = ctx; spinner.start('Start Git LFS check...'); @@ -116,21 +113,17 @@ export class GitLFSCheckModule extends GitLFSBaseModule { isFixed = true; terminal.writeVerboseLine(withPrefix(`Fixed ${toRelativePath(f)}`)); } catch (e) { - terminal.writeVerboseLine( - withPrefix(`Error occurs while fixing ${toRelativePath(f)} ${e}`) - ); + terminal.writeVerboseLine(withPrefix(`Error occurs while fixing ${toRelativePath(f)} ${e}`)); } } if (isFixed) { spinner.warn( - `Git LFS check failed for ${chalk.red( - toRelativePath(f) - )}, but was automatically fixed` + `Git LFS check failed for ${chalk.red(toRelativePath(f))}, but was automatically fixed` ); } else { result.push({ file: toRelativePath(f), - errorType: GitLFSCheckModuleErrorType.FileNeedToBeTrackedByLFS, + errorType: GitLFSCheckModuleErrorType.FileNeedToBeTrackedByLFS }); spinner.fail( `Git LFS check failed for ${chalk.red(toRelativePath(f))}${ @@ -140,14 +133,12 @@ export class GitLFSCheckModule extends GitLFSBaseModule { } } else { result.push({ - file: toRelativePath(f), + file: toRelativePath(f) }); } } - const errors: IGitLFSCheckModuleFileError[] = result.filter( - e => typeof e.errorType !== 'undefined' - ); + const errors: IGitLFSCheckModuleFileError[] = result.filter((e) => typeof e.errorType !== 'undefined'); if (errors.length > 0) { spinner.fail('Git LFS check failed'); } else { From ec5e985f3da7697e80f160812b9f3776b21f5eb6 Mon Sep 17 00:00:00 2001 From: EscapeB Date: Thu, 13 Jun 2024 12:06:41 +0800 Subject: [PATCH 2/2] test: add unit test --- .../rush-git-lfs-plugin/src/tests/executor/core.test.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/rush-plugins/rush-git-lfs-plugin/src/tests/executor/core.test.ts b/rush-plugins/rush-git-lfs-plugin/src/tests/executor/core.test.ts index ac2fa19..54e7644 100644 --- a/rush-plugins/rush-git-lfs-plugin/src/tests/executor/core.test.ts +++ b/rush-plugins/rush-git-lfs-plugin/src/tests/executor/core.test.ts @@ -8,9 +8,7 @@ describe('lfs file detect', () => { const tFile = factory.createTextFile('test-t'); const checker = new GitLFSCheckModule(); expect(checker.isFileNeedToTrack(bFile, { '**/*.png': 0, '**/*': 5 * 1024 * 1024 })).toBe(true); - expect(checker.isFileNeedToTrack(tFile, { '**/*.png': 0, '**/*': 5 * 1024 * 1024 })).toBe( - false - ); + expect(checker.isFileNeedToTrack(tFile, { '**/*.png': 0, '**/*': 5 * 1024 * 1024 })).toBe(false); }); it('should detect large file correctly', () => { @@ -29,10 +27,14 @@ describe('lfs file detect', () => { const tsFile = factory.createSizedFile('tsFile.ts', 6 * 1024 * 1024); const jsFile = factory.createSizedFile('jsFile.js', 2 * 1024 * 1024); + // + const dotFile = factory.createSizedFile('.temp/a.js', 2 * 1024 * 1024); + const checker = new GitLFSCheckModule(); expect(checker.isFileNeedToTrack(tsFile, { '**/*.ts': 7 * 1024 * 1024 })).toBe(false); expect(checker.isFileNeedToTrack(jsFile, { '**/*.js': 1 * 1024 * 1024 })).toBe(true); + expect(checker.isFileNeedToTrack(dotFile, { '**/*.js': 1 * 1024 * 1024 })).toBe(true); }); it('should detect LFS status correctly', () => {