From 0c3558510af594e9a740b1b5f0265db64886864a Mon Sep 17 00:00:00 2001 From: Daniel Convissor Date: Tue, 15 Dec 2015 09:39:29 -0500 Subject: [PATCH] Import Git plugin from github.com/armen/PEAR_PackageFileManager_Git --- PEAR/PackageFileManager/Git.php | 107 ++++++++++++++++++++++++++++++++ package_plugins.php | 1 + 2 files changed, 108 insertions(+) create mode 100644 PEAR/PackageFileManager/Git.php diff --git a/PEAR/PackageFileManager/Git.php b/PEAR/PackageFileManager/Git.php new file mode 100644 index 0000000..69fd8aa --- /dev/null +++ b/PEAR/PackageFileManager/Git.php @@ -0,0 +1,107 @@ + + * @license New BSD, Revised + * @version CVS: $Id:$ + * @link http://pear.php.net/package/PEAR_PackageFileManager_Plugins + * @since File available since Release 1.0.1 + */ + +require_once 'PEAR/PackageFileManager/File.php'; + +/** + * Generate a file list from a Git workingcopy. + * + * Note that this will NOT work on a + * repository, only on a Git workingcopy + * + * @category PEAR + * @package PEAR_PackageFileManager_Plugins + * @author Armen Baghumian + * @license New BSD, Revised + * @version Release: 1.0.0 + * @link http://pear.php.net/package/PEAR_PackageFileManager_Plugins + * @since Class available since Release 1.0.1 + */ +class PEAR_PackageFileManager_Git extends PEAR_PackageFileManager_File +{ + function getFileList() + { + $directory = $this->_options['packagedirectory']; + $git = $this->_findGitRootDir($directory); + + if ($git) { + $content = null; + $content .= file_exists($git.'.gitignore') AND file_get_contents($git.'.gitignore'); + $content .= file_exists($git.'.git/info/exclude') AND file_get_contents($git.'.git/info/exclude'); + $content = trim($content, "\n"); + $content = explode("\n", $content); + $gitignore = array('.git/*', '.gitignore'); + $gitinclude = array(); + + foreach ($content as $pattern) { + if (preg_match('/^\s*#.*$/', $pattern) || preg_match('/^\s*$/', $pattern)) { + continue; + } + + if (preg_match('/^\s*!(.*)$/', $pattern, $match)) { + $gitinclude[] = $match[1]; + + } else { + $gitignore[] = $pattern; + } + } + + $this->_options['ignore'] = is_array($this->_options['ignore']) ? array_merge($gitignore, $this->_options['ignore']) : $gitignore; + } + + $fileslist = parent::getFileList(); + + if (is_array($fileslist) && isset($gitignore) && count($gitinclude)) { + // in gitignore you can ignore whole directory then include specified subdirectories + // or files with "!" modifier at the begining of patterns. + // we have to generate files list and merge it with the main result + + $this->_options['ignore'] = array(); + $this->_options['include'] = array(); + + foreach ($gitinclude as $entry) { + // make sure that entry exists in current include paths if not just ignore it. + if ($this->_checkIgnore($entry, $git.$entry, 0)) { + continue; + } + + $this->_options['include'][] = $entry; + } + + if (count($this->_options['include'])) { + $fileslist = array_merge_recursive($fileslist, parent::getFileList()); + } + } + + return $fileslist; + } + + function _findGitRootDir($directory) + { + $directory = realpath($directory); + + if (!file_exists($directory.DIRECTORY_SEPARATOR.'.git') && $directory != DIRECTORY_SEPARATOR) { + $directory = realpath($directory.DIRECTORY_SEPARATOR.'..'); + return $this->_findGitRootDir($directory); + } + + if (file_exists($directory.DIRECTORY_SEPARATOR.'.git')) { + return $directory.DIRECTORY_SEPARATOR; + } + + return False; + } +} diff --git a/package_plugins.php b/package_plugins.php index b499f15..12de3fd 100755 --- a/package_plugins.php +++ b/package_plugins.php @@ -40,6 +40,7 @@ 'LICENSE_PLUGINS', 'PackageFileManager/Plugins.php', 'PackageFileManager/File.php', + 'PackageFileManager/Git.php', 'PackageFileManager/Svn.php', 'PackageFileManager/Cvs.php', 'PackageFileManager/Perforce.php',