1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | <?php /** * GitPHP GitExe * * Class to wrap git executable * * @author Christopher Han <xiphux@gmail.com> * @copyright Copyright (c) 2010 Christopher Han * @package GitPHP * @subpackage Git */ define('GIT_CAT_FILE','cat-file'); define('GIT_DIFF_TREE','diff-tree'); define('GIT_LS_TREE','ls-tree'); define('GIT_REV_LIST','rev-list'); define('GIT_REV_PARSE','rev-parse'); define('GIT_SHOW_REF','show-ref'); define('GIT_ARCHIVE','archive'); define('GIT_GREP','grep'); define('GIT_BLAME','blame'); define('GIT_NAME_REV','name-rev'); define('GIT_FOR_EACH_REF','for-each-ref'); /** * Git Executable class * * @package GitPHP * @subpackage Git */ class GitPHP_GitExe { /** * project * * Stores the project internally * * @access protected */ protected $project; /** * bin * * Stores the binary path internally * * @access protected */ protected $binary; /** * __construct * * Constructor * * @param string $binary path to git binary * @param mixed $project project to operate on * @return mixed git executable class */ public function __construct($project = null) { $binary = GitPHP_Config::GetInstance()->GetValue('gitbin'); if (empty($binary)) { // try to pick a reasonable default if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { $this->binary = 'C:\\Progra~1\\Git\\bin\\git.exe'; } else { $this->binary = 'git'; } } else { $this->binary = $binary; } $this->SetProject($project); } /** * SetProject * * Sets the project for this executable * * @param mixed $project project to set */ public function SetProject($project = null) { $this->project = $project; } /** * Execute * * Executes a command * * @param string $command the command to execute * @param array $args arguments * @return string result of command */ public function Execute($command, $args) { $gitDir = ''; if ($this->project) { $gitDir = '--git-dir=' . $this->project->GetPath(); } $fullCommand = $this->binary . ' ' . $gitDir . ' ' . $command . ' ' . implode(' ', $args); GitPHP_Log::GetInstance()->Log('Begin executing "' . $fullCommand . '"'); $ret = shell_exec($fullCommand); GitPHP_Log::GetInstance()->Log('Finish executing "' . $fullCommand . '"'); return $ret; } /** * GetBinary * * Gets the binary for this executable * * @return string binary * @access public */ public function GetBinary() { return $this->binary; } /** * GetVersion * * Gets the version of the git binary * * @return string version * @access public */ public function GetVersion() { $versionCommand = $this->binary . ' --version'; $ret = trim(shell_exec($versionCommand)); if (preg_match('/^git version ([0-9\.]+)$/i', $ret, $regs)) { return $regs[1]; } return ''; } /** * CanSkip * * Tests if this version of git can skip through the revision list * * @access public * @return boolean true if we can skip */ public function CanSkip() { $version = $this->GetVersion(); if (!empty($version)) { $splitver = explode('.', $version); /* Skip only appears in git >= 1.5.0 */ if (($splitver[0] < 1) || (($splitver[0] == 1) && ($splitver[1] < 5))) { return false; } } return true; } /** * CanShowSizeInTree * * Tests if this version of git can show the size of a blob when listing a tree * * @access public * @return true if we can show sizes */ public function CanShowSizeInTree() { $version = $this->GetVersion(); if (!empty($version)) { $splitver = explode('.', $version); /* * ls-tree -l only appears in git 1.5.3 * (technically 1.5.3-rc0 but i'm not getting that fancy) */ if (($splitver[0] < 1) || (($splitver[0] == 1) && ($splitver[1] < 5)) || (($splitver[0] == 1) && ($splitver[1] == 5) && ($splitver[2] < 3))) { return false; } } return true; } /** * CanIgnoreRegexpCase * * Tests if this version of git has the regexp tuning option to ignore regexp case * * @access public * @return true if we can ignore regexp case */ public function CanIgnoreRegexpCase() { $version = $this->GetVersion(); if (!empty($version)) { $splitver = explode('.', $version); /* * regexp-ignore-case only appears in git 1.5.3 */ if (($splitver[0] < 1) || (($splitver[0] == 1) && ($splitver[1] < 5)) || (($splitver[0] == 1) && ($splitver[1] == 5) && ($splitver[2] < 3))) { return false; } } return true; } } |