Skip to content

Filter '.' and '..' from wglob results#152

Merged
magicant merged 4 commits into
magicant:trunkfrom
eilefsen:filter-dots
Apr 8, 2025
Merged

Filter '.' and '..' from wglob results#152
magicant merged 4 commits into
magicant:trunkfrom
eilefsen:filter-dots

Conversation

@eilefsen

@eilefsen eilefsen commented Apr 5, 2025

Copy link
Copy Markdown
Contributor

Addresses issue #97

Iterate over the results from wglob_search and remove any verbatim match of . and ...

NOTE: This does not apply to results like component/.. or recursive globs. This is a naive solution. Perhaps a more general solution could be done in wglob_search?

I did a small optimization by conditionally stopping the loop as soon as two results are removed. This relies on the assumption that only two results from glob_search would match . or ...
The filtering happens after the results are sorted, this likely limits the number of iterations, but this is another assumption.

@eilefsen

eilefsen commented Apr 5, 2025

Copy link
Copy Markdown
Contributor Author

The amend commit is a much simpler and more correct solution than the first.

Before we compared the final result. Now we compare each component before they get matched in xfnm_match.

This filter applies in more cases than the previous solution.

@eilefsen
eilefsen marked this pull request as draft April 5, 2025 20:34
@eilefsen

eilefsen commented Apr 5, 2025

Copy link
Copy Markdown
Contributor Author

Nope. Both solutions have glaring oversights. sorry about that. back to the drawing board.

@eilefsen

eilefsen commented Apr 5, 2025

Copy link
Copy Markdown
Contributor Author

At this point im not confident that i have not overlooked something, but the last commit seems to really work this time.
This time i check if the basename of the path equals '.' or '..' before i add the result to the list. I only do this for LITERAL and MATCH patterns, but it seems to also work as expected on recursive globs.

I think its about time to write a test...

@magicant magicant left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thank you for the contribution!

For tests, see tests/path-p.tst, which contains test cases for checking POSIX-compliance of glob. You might also want to check tests/path-y.tst, which covers non-POSIX aspects of glob, but I think we will probably only need to add a test to the former.

Comment thread path.c Outdated
Comment thread path.c Outdated
Comment thread po/ja.po
@magicant magicant linked an issue Apr 6, 2025 that may be closed by this pull request
@eilefsen

eilefsen commented Apr 6, 2025

Copy link
Copy Markdown
Contributor Author

Are you sure that tests for this belong in the posix category?
I see there is already some dotglob tests in path-y.tst, and as you said in the issue, filtering . and .. is not strictly required by the standard.

@magicant

magicant commented Apr 6, 2025

Copy link
Copy Markdown
Owner

filtering . and .. is not strictly required by the standard.

You're right. Since the new behavior is not yet mandatory, the tests should be in path-y.tst. Thanks for pointing this out!

Comment thread path.c
@eilefsen

eilefsen commented Apr 6, 2025

Copy link
Copy Markdown
Contributor Author

I added a test case for the echo **/. example you gave earlier which isn't directly related to this issue. We can take it out if you don't think its necessary.

The test case for combined extendedglob and dotglob has a fairly long expected output (372 characters), it could perhaps be shortened a bit.

@magicant magicant left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thank you. Can we mark the code "ready for review" now? It looks good to me.

@eilefsen
eilefsen marked this pull request as ready for review April 8, 2025 08:49
@magicant
magicant merged commit e231437 into magicant:trunk Apr 8, 2025
@magicant

magicant commented Apr 8, 2025

Copy link
Copy Markdown
Owner

Merged. Thank you so much!

@magicant magicant added this to the 2.59 milestone Apr 8, 2025
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.

Exclude . and .. from pathname expansion results

2 participants