From 21109ada221ffe993c1ce294bdf1c858f76a983d Mon Sep 17 00:00:00 2001 From: Sandy Pleyte Date: Thu, 27 Feb 2014 15:17:15 +0100 Subject: [PATCH 1/3] Fix for #2739 (and #1755), added support for directory excludes in the .gitignore file like /directory or directory/ --- .../Package/Archiver/BaseExcludeFilter.php | 4 +-- .../Archiver/ArchivableFilesFinderTest.php | 35 +++++++++++++++++++ .../Package/Archiver/GitExcludeFilterTest.php | 4 +-- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/Composer/Package/Archiver/BaseExcludeFilter.php b/src/Composer/Package/Archiver/BaseExcludeFilter.php index f8c390e41..032a32d35 100644 --- a/src/Composer/Package/Archiver/BaseExcludeFilter.php +++ b/src/Composer/Package/Archiver/BaseExcludeFilter.php @@ -140,8 +140,8 @@ abstract class BaseExcludeFilter $pattern .= '/'; } - // remove delimiters as well as caret (^) from the regex - $pattern .= substr(Finder\Glob::toRegex($rule), 2, -1); + // remove delimiters as well as caret (^) and dollar sign ($) from the regex + $pattern .= substr(Finder\Glob::toRegex($rule), 2, -2) . '/*.*$'; return array($pattern . '#', $negate, false); } diff --git a/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php b/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php index 536f2128c..7200a699f 100644 --- a/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php +++ b/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php @@ -46,6 +46,24 @@ class ArchivableFilesFinderTest extends \PHPUnit_Framework_TestCase 'B/sub/prefixD.foo', 'B/sub/prefixE.foo', 'B/sub/prefixF.foo', + 'C/prefixA.foo', + 'C/prefixB.foo', + 'C/prefixC.foo', + 'C/prefixD.foo', + 'C/prefixE.foo', + 'C/prefixF.foo', + 'D/prefixA', + 'D/prefixB', + 'D/prefixC', + 'D/prefixD', + 'D/prefixE', + 'D/prefixF', + 'E/prefixA.foo', + 'E/prefixB.foo', + 'E/prefixC.foo', + 'E/prefixD.foo', + 'E/prefixE.foo', + 'E/prefixF.foo', 'toplevelA.foo', 'toplevelB.foo', 'prefixA.foo', @@ -91,6 +109,20 @@ class ArchivableFilesFinderTest extends \PHPUnit_Framework_TestCase '/B/sub/prefixD.foo', '/B/sub/prefixE.foo', '/B/sub/prefixF.foo', + '/C/prefixA.foo', + '/C/prefixD.foo', + '/C/prefixE.foo', + '/C/prefixF.foo', + '/D/prefixA', + '/D/prefixB', + '/D/prefixC', + '/D/prefixD', + '/D/prefixE', + '/D/prefixF', + '/E/prefixA.foo', + '/E/prefixD.foo', + '/E/prefixE.foo', + '/E/prefixF.foo', '/prefixB.foo', '/prefixD.foo', '/prefixE.foo', @@ -120,6 +152,9 @@ class ArchivableFilesFinderTest extends \PHPUnit_Framework_TestCase '!/*/*/prefixF.foo', '', 'refixD.foo', + '/C', + 'D/prefixA', + 'E/' ))); // git does not currently support negative git attributes diff --git a/tests/Composer/Test/Package/Archiver/GitExcludeFilterTest.php b/tests/Composer/Test/Package/Archiver/GitExcludeFilterTest.php index a6d473a1e..e2eef1722 100644 --- a/tests/Composer/Test/Package/Archiver/GitExcludeFilterTest.php +++ b/tests/Composer/Test/Package/Archiver/GitExcludeFilterTest.php @@ -29,8 +29,8 @@ class GitExcludeFilterTest extends \PHPUnit_Framework_TestCase public function patterns() { return array( - array('app/config/parameters.yml', array('#(?=[^\.])app/(?=[^\.])config/(?=[^\.])parameters\.yml$#', false, false)), - array('!app/config/parameters.yml', array('#(?=[^\.])app/(?=[^\.])config/(?=[^\.])parameters\.yml$#', true, false)), + array('app/config/parameters.yml', array('#(?=[^\.])app/(?=[^\.])config/(?=[^\.])parameters\.yml/*.*$#', false, false)), + array('!app/config/parameters.yml', array('#(?=[^\.])app/(?=[^\.])config/(?=[^\.])parameters\.yml/*.*$#', true, false)), ); } } From 1d51e54a31dfe0225b3ebc5aaac8618555ec5856 Mon Sep 17 00:00:00 2001 From: Sandy Pleyte Date: Fri, 28 Feb 2014 11:43:28 +0100 Subject: [PATCH 2/3] Updated the regexp and added more test cases. --- .../Package/Archiver/BaseExcludeFilter.php | 7 ++- .../Archiver/ArchivableFilesFinderTest.php | 43 +++++++++++++------ .../Package/Archiver/GitExcludeFilterTest.php | 4 +- 3 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/Composer/Package/Archiver/BaseExcludeFilter.php b/src/Composer/Package/Archiver/BaseExcludeFilter.php index 032a32d35..a62d56596 100644 --- a/src/Composer/Package/Archiver/BaseExcludeFilter.php +++ b/src/Composer/Package/Archiver/BaseExcludeFilter.php @@ -136,12 +136,15 @@ abstract class BaseExcludeFilter if (strlen($rule) && $rule[0] === '/') { $pattern .= '^/'; $rule = substr($rule, 1); - } elseif (false === strpos($rule, '/') || strlen($rule) - 1 === strpos($rule, '/')) { + } elseif (strlen($rule) - 1 === strpos($rule, '/')) { + $pattern .= '^/'; + $rule = substr($rule, 0, -1); + } elseif (false === strpos($rule, '/')) { $pattern .= '/'; } // remove delimiters as well as caret (^) and dollar sign ($) from the regex - $pattern .= substr(Finder\Glob::toRegex($rule), 2, -2) . '/*.*$'; + $pattern .= substr(Finder\Glob::toRegex($rule), 2, -2) . '(?=$|/)'; return array($pattern . '#', $negate, false); } diff --git a/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php b/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php index 7200a699f..ed87ee2f0 100644 --- a/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php +++ b/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php @@ -58,12 +58,10 @@ class ArchivableFilesFinderTest extends \PHPUnit_Framework_TestCase 'D/prefixD', 'D/prefixE', 'D/prefixF', - 'E/prefixA.foo', - 'E/prefixB.foo', - 'E/prefixC.foo', - 'E/prefixD.foo', - 'E/prefixE.foo', - 'E/prefixF.foo', + '/E/subtestA.foo', + '/F/subtestA.foo', + '/G/subtestA.foo', + '/H/subtestA.foo', 'toplevelA.foo', 'toplevelB.foo', 'prefixA.foo', @@ -72,6 +70,10 @@ class ArchivableFilesFinderTest extends \PHPUnit_Framework_TestCase 'prefixD.foo', 'prefixE.foo', 'prefixF.foo', + 'parameters.yml', + 'parameters.yml.dist', + '!important!.txt', + '!important_too!.txt' ); foreach ($fileTree as $relativePath) { @@ -100,6 +102,8 @@ class ArchivableFilesFinderTest extends \PHPUnit_Framework_TestCase $this->finder = new ArchivableFilesFinder($this->sources, $excludes); $this->assertArchivableFiles(array( + '/!important!.txt', + '/!important_too!.txt', '/A/prefixA.foo', '/A/prefixD.foo', '/A/prefixE.foo', @@ -119,10 +123,12 @@ class ArchivableFilesFinderTest extends \PHPUnit_Framework_TestCase '/D/prefixD', '/D/prefixE', '/D/prefixF', - '/E/prefixA.foo', - '/E/prefixD.foo', - '/E/prefixE.foo', - '/E/prefixF.foo', + '/E/subtestA.foo', + '/F/subtestA.foo', + '/G/subtestA.foo', + '/H/subtestA.foo', + '/parameters.yml', + '/parameters.yml.dist', '/prefixB.foo', '/prefixD.foo', '/prefixE.foo', @@ -154,7 +160,12 @@ class ArchivableFilesFinderTest extends \PHPUnit_Framework_TestCase 'refixD.foo', '/C', 'D/prefixA', - 'E/' + 'E', + 'F/', + 'G/*', + 'H/**', + 'parameters.yml', + '\!important!.txt' ))); // git does not currently support negative git attributes @@ -195,9 +206,15 @@ class ArchivableFilesFinderTest extends \PHPUnit_Framework_TestCase '# comments', '', '^prefixD.foo', + 'D/prefixA', + 'parameters.yml', + '\!important!.txt', + 'E', + 'F/', 'syntax: glob', 'prefixF.*', 'B/*', + 'H/**', ))); $this->finder = new ArchivableFilesFinder($this->sources, array()); @@ -208,7 +225,9 @@ class ArchivableFilesFinderTest extends \PHPUnit_Framework_TestCase 'hg archive archive.zip' ); - array_shift($expectedFiles); // remove .hg_archival.txt + // Remove .hg_archival.txt from the expectedFiles + $archiveKey = array_search('/.hg_archival.txt', $expectedFiles); + array_splice($expectedFiles, $archiveKey, 1); $this->assertArchivableFiles($expectedFiles); } diff --git a/tests/Composer/Test/Package/Archiver/GitExcludeFilterTest.php b/tests/Composer/Test/Package/Archiver/GitExcludeFilterTest.php index e2eef1722..97c02c8e6 100644 --- a/tests/Composer/Test/Package/Archiver/GitExcludeFilterTest.php +++ b/tests/Composer/Test/Package/Archiver/GitExcludeFilterTest.php @@ -29,8 +29,8 @@ class GitExcludeFilterTest extends \PHPUnit_Framework_TestCase public function patterns() { return array( - array('app/config/parameters.yml', array('#(?=[^\.])app/(?=[^\.])config/(?=[^\.])parameters\.yml/*.*$#', false, false)), - array('!app/config/parameters.yml', array('#(?=[^\.])app/(?=[^\.])config/(?=[^\.])parameters\.yml/*.*$#', true, false)), + array('app/config/parameters.yml', array('#(?=[^\.])app/(?=[^\.])config/(?=[^\.])parameters\.yml(?=$|/)#', false, false)), + array('!app/config/parameters.yml', array('#(?=[^\.])app/(?=[^\.])config/(?=[^\.])parameters\.yml(?=$|/)#', true, false)), ); } } From 3e161e8ea988543ab7ba3eb35dca8101f9f525ef Mon Sep 17 00:00:00 2001 From: Sandy Pleyte Date: Fri, 28 Feb 2014 16:30:55 +0100 Subject: [PATCH 3/3] Updated the regexp and added an other test case for sub dirs --- src/Composer/Package/Archiver/BaseExcludeFilter.php | 2 +- .../Package/Archiver/ArchivableFilesFinderTest.php | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Composer/Package/Archiver/BaseExcludeFilter.php b/src/Composer/Package/Archiver/BaseExcludeFilter.php index a62d56596..dca24eb67 100644 --- a/src/Composer/Package/Archiver/BaseExcludeFilter.php +++ b/src/Composer/Package/Archiver/BaseExcludeFilter.php @@ -137,7 +137,7 @@ abstract class BaseExcludeFilter $pattern .= '^/'; $rule = substr($rule, 1); } elseif (strlen($rule) - 1 === strpos($rule, '/')) { - $pattern .= '^/'; + $pattern .= '/'; $rule = substr($rule, 0, -1); } elseif (false === strpos($rule, '/')) { $pattern .= '/'; diff --git a/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php b/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php index ed87ee2f0..bc74be1e9 100644 --- a/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php +++ b/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php @@ -58,10 +58,12 @@ class ArchivableFilesFinderTest extends \PHPUnit_Framework_TestCase 'D/prefixD', 'D/prefixE', 'D/prefixF', - '/E/subtestA.foo', - '/F/subtestA.foo', - '/G/subtestA.foo', - '/H/subtestA.foo', + 'E/subtestA.foo', + 'F/subtestA.foo', + 'G/subtestA.foo', + 'H/subtestA.foo', + 'I/J/subtestA.foo', + 'K/dirJ/subtestA.foo', 'toplevelA.foo', 'toplevelB.foo', 'prefixA.foo', @@ -127,6 +129,8 @@ class ArchivableFilesFinderTest extends \PHPUnit_Framework_TestCase '/F/subtestA.foo', '/G/subtestA.foo', '/H/subtestA.foo', + '/I/J/subtestA.foo', + '/K/dirJ/subtestA.foo', '/parameters.yml', '/parameters.yml.dist', '/prefixB.foo', @@ -164,6 +168,7 @@ class ArchivableFilesFinderTest extends \PHPUnit_Framework_TestCase 'F/', 'G/*', 'H/**', + 'J/', 'parameters.yml', '\!important!.txt' )));