Skip to content

Commit

Permalink
Merge pull request #16 from EscapeB/fix/fix_pattern_match_for_dot_path
Browse files Browse the repository at this point in the history
fix: fix pattern match for path that contains dot
  • Loading branch information
chengcyber authored Jun 13, 2024
2 parents 07668fd + ec5e985 commit 874f167
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
10 changes: 10 additions & 0 deletions common/changes/rush-git-lfs-plugin/main_2024-06-13-03-59.json
Original file line number Diff line number Diff line change
@@ -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"
}
37 changes: 14 additions & 23 deletions rush-plugins/rush-git-lfs-plugin/src/executor/modules/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
toRelativePath,
toAbsolutePath,
RushRootFolder,
findFileBelongProject,
findFileBelongProject
} from '../../helpers/file-path-analyser';

export interface IGitLFSCheckModuleContext extends IGitLFSModuleContext {
Expand All @@ -32,15 +32,15 @@ export interface IGitLFSCheckModuleFileError {
export const enum GitLFSCheckModuleErrorType {
FileNeedToBeTrackedByLFS,
FixFileLFSStatusFail,
GitAddFail,
GitAddFail
}

export class GitLFSCheckModule extends GitLFSBaseModule {
public isFileNeedToTrack(p: string, pattern: Record<string, number>): boolean {
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;
}
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -97,7 +94,7 @@ export class GitLFSCheckModule extends GitLFSBaseModule {
result,
fix,
option: { checkPattern },
spinner,
spinner
} = ctx;

spinner.start('Start Git LFS check...');
Expand All @@ -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))}${
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand Down

0 comments on commit 874f167

Please sign in to comment.