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

Add support for path beginning with parent directory (..) #5

Merged
merged 2 commits into from Mar 16, 2021

Conversation

fdinel
Copy link
Contributor

@fdinel fdinel commented Mar 9, 2021

This PR adds support for paths starting with the parent directory (..). It modifies the is_hidden() function so that paths starting with parent directory ( ../ ) or current directory ( ./ ) should not be considered hidden.

Comment on lines 184 to 189
bool is_hidden(const std::string &pathname) {
// path is hidden if it starts with dot and is not parent dir nor current dir
if (pathname.size() >= 2)
return pathname.at(0) == '.' && pathname.at(1) != '.' && pathname.at(1) != '/';
return pathname[0] == '.';
}
Copy link
Owner

Choose a reason for hiding this comment

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

What should be the result of is_hidden("../.foobar")? This implementation would return false.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi,
indeed I agree that my pretty basic implementation would fail. However to be fair, the actual implementation would also fail, returning false for any path that starts with . such as for example ../foo or ./bar, leading to the path not being searched at all using the glob/rglob functions. I wanted a very simple and nonintrusive implementation that fit my needs of globing parent paths but I'm very open to alternatives.

I personally think that the function should look for any directory that is hidden within the given path and not only the first character. If any of those directories begins with dot and followed by something else than dot or slash, then the function should consider this path as hidden.

Also but unrelated, unless there is verification somewhere else, is_hidden will crash if given an empty string as parameter (line 188 accesses index 0 without checking if pathname is empty).

BTW, thanks for the library, very useful and simple to integrate!

Copy link
Owner

Choose a reason for hiding this comment

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

Fully agree with your points and support the argument that this needs to be fixed.

I was just wondering what should be the right result in the imagined scenarios - the scenario I imagined here is "../.foobar", i.e., a hidden file in a parent directory. A good place to start is to probably see what Python's implementation does in this regard.

I really want to accept this PR but maybe we can add (1) the check for empty string, and (2) consider the case I have imagined?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed. I'll rework my PR to support those.

Copy link
Owner

Choose a reason for hiding this comment

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

Great, thanks!

@fdinel
Copy link
Contributor Author

fdinel commented Mar 16, 2021

Hi again,
I came up with a regex implementation that gives the following results:
(is_hidden vs test string)
0 (empty string)
0 file.test
0 /a
0 a/b
1 .a
1 a/.b
1 ../.foo
0 ../bar
0 a//
0 a/../../c
0 b/../.c/d
0 b/..
0 a/b/.c/..
1 a/.b//
0 a/.b/../

What do you think?

@p-ranav p-ranav merged commit 238f674 into p-ranav:master Mar 16, 2021
@p-ranav
Copy link
Owner

p-ranav commented Mar 16, 2021

Looks great! Thanks!!

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

Successfully merging this pull request may close these issues.

None yet

2 participants