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

Can't use includeFilter on folders #67

Closed
RemyVULTAGGIO opened this issue Oct 14, 2022 · 5 comments
Closed

Can't use includeFilter on folders #67

RemyVULTAGGIO opened this issue Oct 14, 2022 · 5 comments

Comments

@RemyVULTAGGIO
Copy link

When using the "includeFilter" option, it's impossible to filter folders.

Checking the code (

if ((entry.stat.isFile() && options.includeFilter) && (!match(path, options.includeFilter))) {
) shows that "includeFilter" apply only on files and not folders.

Could it be possible to replace the line
if ((entry.stat.isFile() && options.includeFilter) && (!match(path, options.includeFilter))) {
by
if (options.includeFilter && !match(path, options.includeFilter)) {

It then will match what is done for the excludeFilter.

What do you think about it ?
Thanks in advance.

@gliviu
Copy link
Owner

gliviu commented Oct 20, 2022

Let's say we stick to your proposal and use includeFilter: '*.js'.
In this case we will get no result because no directory will match '*.js' and the traverse function will not get to any files.

I agree it is a little bit tricky, but if you have a certain usecase we could investigate more.

Also don't forget that currently it is possible to use the 'includeFilter' on directories using glob patterns.
For example

includeFilter: '/src/Statistics/*.ts,**/expected/*001_1*'

will include these files:
image

@RemyVULTAGGIO
Copy link
Author

Thanks for your reply.
I made new tests from it. But, I still did not get the expected results.

Here is my usecase : I want to compare two folders content on specific subfolders & files only.

Example :

Left structure
/folderA
...
/folderD
...
file1
file3

Right structure
/folderA
...
/folderB
...
/folderC
...
file1
file2

Filter :
includeFilter: '/folderA,/file1'
(I also tried with '/folderA/,/file1' and '/folderA//.,/file1')

Expected Result :
Only 'file1' and files & subfolders from 'folderA'

Actual Result :
Not just the expected Result

I have made many tests. I also tested the 'negate' option from minimatch on the excludeFilter.

From your point of view, what would be the best way to restrict comparison of two folders on specific files & subfolders ?
Thanks in advance for you help.

@gliviu
Copy link
Owner

gliviu commented Oct 25, 2022

Yes, this could be improved. I will think about it.

Meanwhile you can use the Custom result builder and implement your own logic.
Take a look at the attached zip fie and see test.js.

I am using two options.

includeFilter: '**/folderA1/**,**/folderA2/**,/file1',   // this already exists and filters files
includeFolders: '**/folderA1, **/folderA2',       // this new one is used to filter folders

myResultBuilder is identical to default result builder except for the first two conditions that are used to exclude folders that do not match 'includeFolders'.

You can run the example like this:

unzip test.zip
npm install
node test.js

Note that the end statistics are no longer to be trusted since we are altering he result after the statistics are created.
Only the *Files stats are still true.

If you need all of them, you would have to create your own stats based on Result.diffSet.

Hope it helps.

test.zip

@RemyVULTAGGIO
Copy link
Author

Thank you very much, @gliviu !

Before opening this ticket, I read the documentation and though the Custom Result Builder could be an option but wasn't really confident on how. Your example was perfect.

I made some modifications and it worked perfectly for what I intended to do.

My dir-compare Options:
const options: Options = {
includeFilterWorkaround: commaSeparatedFilesAndDirectoriesPatterns,
excludeFilter: '**/.gitkeep',
resultBuilder: myResultBuilder,
};

My Custom Builder :
export function myResultBuilder(
entry1: Entry,
entry2: Entry,
state: DifferenceState,
level: number,
relativePath: string,
options: ExtOptions,
statistics: Statistics,
diffSet: DiffSet,
reason: Reason,
permissionDeniedState: PermissionDeniedState
): void {
if (
options.includeFilterWorkaround &&
entry1 &&
!match(path.join(relativePath, entry1.name), options.includeFilterWorkaround as string)
) {
return;
}
if (
options.includeFilterWorkaround &&
entry2 &&
!match(path.join(relativePath, entry2.name), options.includeFilterWorkaround as string)
) {
return;
}

if (options.noDiffSet) {
return;
}

diffSet.push({
path1: entry1 ? path.dirname(entry1.path) : undefined,
path2: entry2 ? path.dirname(entry2.path) : undefined,
relativePath,
name1: entry1 ? entry1.name : undefined,
name2: entry2 ? entry2.name : undefined,
state,
permissionDeniedState,
type1: getType(entry1),
type2: getType(entry2),
level,
size1: entry1 ? entry1.stat.size : undefined,
size2: entry2 ? entry2.stat.size : undefined,
date1: entry1 ? entry1.stat.mtime : undefined,
date2: entry2 ? entry2.stat.mtime : undefined,
reason,
});
}

It's all OK for me.

@gliviu
Copy link
Owner

gliviu commented Aug 16, 2023

Decided to leave includeFilter and excludeFilter logic unchanged, just to preserve backwards compatibility.
Starting with v4.1.0, different logic can be implemented. See https://github.com/gliviu/dir-compare#glob-filter.

@gliviu gliviu closed this as completed Aug 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants