Skip to content

Commit

Permalink
Import Git plugin from github.com/armen/PEAR_PackageFileManager_Git
Browse files Browse the repository at this point in the history
  • Loading branch information
convissor committed Dec 15, 2015
1 parent aae4130 commit 0c35585
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
107 changes: 107 additions & 0 deletions PEAR/PackageFileManager/Git.php
@@ -0,0 +1,107 @@
<?php
/**
* The git list plugin generator for both PEAR_PackageFileManager,
* and PEAR_PackageFileManager2 classes.
*
* PHP versions 4 and 5
*
* @category PEAR
* @package PEAR_PackageFileManager_Plugins
* @author Armen Baghumian <armen@OpenSourceClub.org>
* @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 <b>NOT</b> work on a
* repository, only on a Git workingcopy
*
* @category PEAR
* @package PEAR_PackageFileManager_Plugins
* @author Armen Baghumian <armen@OpenSourceClub.org>
* @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;
}
}
1 change: 1 addition & 0 deletions package_plugins.php
Expand Up @@ -40,6 +40,7 @@
'LICENSE_PLUGINS',
'PackageFileManager/Plugins.php',
'PackageFileManager/File.php',
'PackageFileManager/Git.php',
'PackageFileManager/Svn.php',
'PackageFileManager/Cvs.php',
'PackageFileManager/Perforce.php',
Expand Down

0 comments on commit 0c35585

Please sign in to comment.