Skip to content

Commit e1f9c5c

Browse files
Copilotswissspidy
andcommitted
Address code review feedback
- Use substr() instead of str_replace() for path manipulation to avoid edge cases - Add comment explaining the use of 'test' as a probe filename - Replace Bash-specific syntax with portable POSIX shell commands in tests - Fix code style (trailing whitespace) Co-authored-by: swissspidy <[email protected]>
1 parent f914723 commit e1f9c5c

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

features/distignore.feature

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,13 +405,13 @@ Feature: Generate a distribution archive of a project with .distignore
405405
When I run `mkdir -p foo/node_modules/package1 foo/node_modules/package2 foo/node_modules/package3`
406406
Then STDERR should be empty
407407

408-
When I run `for i in {1..50}; do touch foo/node_modules/package1/file$i.js; done`
408+
When I run `sh -c 'i=1; while [ $i -le 50 ]; do touch foo/node_modules/package1/file$i.js; i=$((i+1)); done'`
409409
Then STDERR should be empty
410410

411-
When I run `for i in {1..50}; do touch foo/node_modules/package2/file$i.js; done`
411+
When I run `sh -c 'i=1; while [ $i -le 50 ]; do touch foo/node_modules/package2/file$i.js; i=$((i+1)); done'`
412412
Then STDERR should be empty
413413

414-
When I run `for i in {1..50}; do touch foo/node_modules/package3/file$i.js; done`
414+
When I run `sh -c 'i=1; while [ $i -le 50 ]; do touch foo/node_modules/package3/file$i.js; i=$((i+1)); done'`
415415
Then STDERR should be empty
416416

417417
When I run `wp dist-archive foo`

src/Distignore_Filter_Iterator.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,16 @@ public function hasChildren() {
6969
}
7070

7171
// For directories, check if they should be ignored.
72-
$relative_filepath = str_replace( $this->source_dir_path, '', $item->getPathname() );
72+
$pathname = $item->getPathname();
73+
$source_path_length = strlen( $this->source_dir_path );
74+
75+
// Extract relative path by removing the source directory prefix.
76+
if ( 0 === strpos( $pathname, $this->source_dir_path ) ) {
77+
$relative_filepath = substr( $pathname, $source_path_length );
78+
} else {
79+
// Fallback if path doesn't start with source path (shouldn't happen).
80+
$relative_filepath = $pathname;
81+
}
7382

7483
try {
7584
$is_ignored = $this->checker->isPathIgnored( $relative_filepath );
@@ -87,6 +96,8 @@ public function hasChildren() {
8796
// This is a top-level ignored directory like "/node_modules" or "/.git".
8897
// It's likely safe to skip descent as these are typically simple patterns.
8998
// However, we still need to be conservative. Let's check if a child would be ignored.
99+
// We use 'test' as a probe filename to check if children would be ignored.
100+
// The actual name doesn't matter; we just need to verify the pattern applies to children.
90101
$test_child = $relative_filepath . '/test';
91102
try {
92103
$child_ignored = $this->checker->isPathIgnored( $test_child );

0 commit comments

Comments
 (0)