From 4b8fb4c41f042b4b76776b52b11a3afe40585513 Mon Sep 17 00:00:00 2001 From: Gunnar Wrobel
Date: Tue, 2 Nov 2010 09:16:57 +0100 Subject: [PATCH] Draft for excluding files based on the gitignore information. --- components/lib/Components/Helper/Root.php | 21 ++++++- components/lib/Components/Pear/Factory.php | 13 ---- .../Components/Pear/Package/Contents/Factory.php | 15 ++++- .../Components/Pear/Package/Contents/Ignore.php | 69 ++++++++++++++++++++-- 4 files changed, 98 insertions(+), 20 deletions(-) diff --git a/components/lib/Components/Helper/Root.php b/components/lib/Components/Helper/Root.php index fc8312cf9..e832b0f22 100644 --- a/components/lib/Components/Helper/Root.php +++ b/components/lib/Components/Helper/Root.php @@ -37,6 +37,13 @@ class Components_Helper_Root private $_root_path; /** + * Relative position of the path that has been used to determine the root positionq. + * + * @var string + */ + private $_base = ''; + + /** * Constructor. * * @param string $path The helper will try to determine the root of the @@ -55,6 +62,7 @@ class Components_Helper_Root break; } } + $this->_base .= basename($current) . DIRECTORY_SEPARATOR; $current = dirname($current); $i++; } @@ -96,8 +104,19 @@ class Components_Helper_Root * * @return string The information from the gitignore file. */ - public function fetchGitIgnore() + public function getGitIgnore() { return ''; } + + /** + * Return the relative position of the path originally used to determine the + * root position of the repository. + * + * @return string The relative path. + */ + public function getBase() + { + return $this->_base; + } } \ No newline at end of file diff --git a/components/lib/Components/Pear/Factory.php b/components/lib/Components/Pear/Factory.php index f6d2f8d08..4747e7c6e 100644 --- a/components/lib/Components/Pear/Factory.php +++ b/components/lib/Components/Pear/Factory.php @@ -300,19 +300,6 @@ class Components_Pear_Factory 'clearcontents' => false, 'clearchangelog' => false, 'simpleoutput' => true, - 'dir_roles' => - array( - 'bin' => 'script', - 'script' => 'script', - 'doc' => 'doc', - 'example' => 'doc', - 'js' => 'horde', - 'horde' => 'horde', - 'lib' => 'php', - 'migration' => 'data', - 'scripts' => 'data', - 'test' => 'test', - ), ) ) ); diff --git a/components/lib/Components/Pear/Package/Contents/Factory.php b/components/lib/Components/Pear/Package/Contents/Factory.php index 86c934a3a..0f23b78da 100644 --- a/components/lib/Components/Pear/Package/Contents/Factory.php +++ b/components/lib/Components/Pear/Package/Contents/Factory.php @@ -41,10 +41,23 @@ class Components_Pear_Package_Contents_Factory $root = new Components_Helper_Root( $package->_options['packagedirectory'] ); + $package->_options['dir_roles'] = array( + 'bin' => 'script', + 'script' => 'script', + 'doc' => 'doc', + 'example' => 'doc', + 'js' => 'horde', + 'horde' => 'horde', + 'lib' => 'php', + 'migration' => 'data', + 'scripts' => 'data', + 'test' => 'test', + ); return new Components_Pear_Package_Contents_List( $package->_options['packagedirectory'], new Components_Pear_Package_Contents_Ignore( - $root->fetchGitIgnore() + $root->getGitIgnore(), + $root->getBase() ) ); } diff --git a/components/lib/Components/Pear/Package/Contents/Ignore.php b/components/lib/Components/Pear/Package/Contents/Ignore.php index 5d1883bba..50118a3c4 100644 --- a/components/lib/Components/Pear/Package/Contents/Ignore.php +++ b/components/lib/Components/Pear/Package/Contents/Ignore.php @@ -30,20 +30,28 @@ class Components_Pear_Package_Contents_Ignore { /** - * The gitignore information. + * The regular expressions for files to ignore. * - * @var string + * @var array */ - private $_gitignore; + private $_ignore = array(); + + /** + * The regular expressions for files to exclude from ignoring. + * + * @var array + */ + private $_include = array(); /** * Constructor. * * @param string $gitignore The gitignore information + * @param string $base An optional base for the files that should be checked. */ - public function __construct($gitignore) + public function __construct($gitignore, $base = '') { - $this->_gitignore = $gitignore; + $this->_base = $base; } /** @@ -62,6 +70,57 @@ class Components_Pear_Package_Contents_Ignore */ public function checkIgnore($file, $path, $return = 1) { + if (!$return) { + // This class is not about identifying included files. + return !$return; + } + + if ($this->_matches($this->_ignore, $this->_base . $path) + && !$this->_matches($this->_include, $this->_base . $path)) { + return $return; + } + if ($this->_matches($this->_ignore, $file) + && !$this->_matches($this->_include, $file)) { + return $return; + } return !$return; } + + private function _matches($matches, $path) + { + foreach ($matches as $match) { + preg_match('/^' . strtoupper($match).'$/', strtoupper($path), $find); + if (count($find)) { + return true; + } + } + return false; + } + + /** + * Converts $s into a string that can be used with preg_match + * + * @param string $s string with wildcards ? and * + * + * @return string converts * to .*, ? to ., etc. + * @access private + */ + private function _getRegExpableSearchString($s) + { + $y = '\/'; + if (DIRECTORY_SEPARATOR == '\\') { + $y = '\\\\'; + } + + $s = str_replace('/', DIRECTORY_SEPARATOR, $s); + $x = strtr($s, array('?' => '.', '*' => '.*', '.' => '\\.', '\\' => '\\\\', '/' => '\\/', + '[' => '\\[', ']' => '\\]', '-' => '\\-')); + + if (strpos($s, DIRECTORY_SEPARATOR) !== false && + strrpos($s, DIRECTORY_SEPARATOR) === strlen($s) - 1) { + $x = "(?:.*$y$x?.*|$x.*)"; + } + + return $x; + } } \ No newline at end of file -- 2.11.0