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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
<?php
/*-----8<--------------------------------------------------------------------
*
* BEdita - a semantic content management framework
*
* Copyright 2008-2014 ChannelWeb Srl, Chialab Srl
*
* This file is part of BEdita: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* BEdita is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with BEdita (see LICENSE.LGPL).
* If not, see <http://gnu.org/licenses/lgpl-3.0.html>.
*
*------------------------------------------------------------------->8-----
*/
/**
* Revision Control System Model for Git
*/
if (interface_exists('RCS') != true) {
APP::import('Vendor', 'rcs');
}
class Git implements RCS {
public $path = null;
public $username = '';
public $passwd = '';
public $lastCommandCode = null;
public $lastCommand = null;
public $lastError = null;
/**
* Get the repository type for this model
*
* @return string: the repository type
*/
public function type() {
return 'git';
}
/**
* Startup the model initializing a path.
*
* @param string $path: the repository path
* @return void
*/
public function startup($path) {
$this->path = $path;
}
/**
* Set authorization params for the model
*
* @param string $user: the Git username
* @param string $passwd: the Git user password
* @return void
*/
public function authorize($user, $passwd) {
$this->username = $user;
$this->password = $passwd;
}
/**
* Clone a repository
*
* @param string $url: the repository remote url
* @param string $path: where the repository should be checkouted
* @param string $branch: the branch name to checkout
* @return array: the clone command results
*/
public function cloneRemote($url, $path = null, $branch = 'master') {
if (empty($path)) {
$path = $this->path;
}
$cmd = "git clone $url -b $branch $path";
$res = $this->exec($cmd);
return $res;
}
/**
* Update a repository
*
* @param string $path: optional. Can be a repository path to update
* @return array: the update command results
*/
public function up($path = null) {
if (empty($path)) {
$path = $this->path;
}
$res = array();
$currentBranch = $this->branch();
if (empty($currentBranch)) {
// no version control
return null;
} else {
$findFolder = $this->command('cd ' . $path . '; git rev-parse --show-toplevel');
if (empty($findFolder)) {
return null;
} else {
$findFolder = $findFolder[0];
}
$isWriteable = is_writable($findFolder . DS . '.git' . DS . 'ORIG_HEAD');
$updateCmd = 'cd ' . $path . '; git fetch origin; git merge origin/' . $currentBranch;
$res = $this->command($updateCmd);
return $res;
}
}
/**
* Get the current status of the repository
*
* @param string $path: optional. Can be a repository path
* @return array: the status command results
*/
public function status($path = null) {
if (empty($path)) {
$path = $this->path;
}
if (empty($path)) {
$path = $this->path;
}
$cmd = 'cd ' . $path . '; ';
$cmd .= 'git status';
$status = $this->command($cmd);
$status = (!empty($status[0]))? implode("\n", $status[0]) : null;
return $status;
}
/**
* Get the current branch
*
* @param string $path: optional. Can be a repository path
* @return string: the current branch name
*/
public function branch($path = null) {
if (empty($path)) {
$path = $this->path;
}
$cmd = 'cd ' . $path . '; ';
$cmd .= 'git rev-parse --abbrev-ref HEAD';
$branch = $this->command($cmd);
$branch = (!empty($branch[0]))? $branch[0] : null;
return $branch;
}
/**
* Get (remote) branches list
*
* @param string $path: optional. Can be a repository path
* @param boolean $remote: should fetch from remote
* @return array: a list of (remote) branches
*/
public function branches($path = null, $remote = false) {
if (empty($path)) {
$path = $this->path;
}
$cmd = 'cd ' . $path . '; ';
$cmd .= 'git branch';
if ($remote) {
$cmd .= ' -r';
}
$branches = $this->command($cmd);
if (!empty($branches)) {
foreach($branches as &$b) {
$b = preg_replace('/\s?\*?\s+/', '', $b);
}
} else {
$branches = null;
}
return $branches;
}
/**
* Check if the current or given path is a valid Git repository
*
* @param string $path: optional. Can be a repository path
* @return boolean: is a valid repository or not
*/
public function valid($path = null) {
$branch = $this->branch($path);
if (!empty($branch) && $this->lastCommandCode ==0) {
return true;
}
return false;
}
/**
* Get last commit data
*
* @param string $path: optional. Can be a repository path
* @return array: a list of properties for the last commit
*/
public function lastCommit($path = null) {
if (empty($path)) {
$path = $this->path;
}
$cmd = 'cd ' . $path . '; ';
$cmd .= 'git log -n 1';
$output = $this->command($cmd);
$history = array();
$commit = array();
foreach ($output as $line) {
if (strpos($line, 'commit') === 0) {
if (!empty($commit)) {
array_push($history, $commit);
unset($commit);
$commit = array();
}
$commit['hash'] = substr($line, strlen('commit'));
} elseif (strpos($line, 'Author') === 0) {
$commit['author'] = substr($line, strlen('Author:'));
} elseif (strpos($line, 'Date') === 0) {
$commit['date'] = substr($line, strlen('Date:'));
} else {
if (!empty($commit['message'])) {
$commit['message'] .= $line;
} else {
$commit['message'] = $line;
}
}
}
if(!empty($commit)) {
array_push($history, $commit);
unset($commit);
}
return (!empty($history)) ? $history[0] : false;
}
/**
* Exec a given Git command
* fill `lastCommand` and eventually `lastCommandCode` and `lastError` attributes.
*
* @param string $cmd: the command to exec
* @return array: the result of the exec
*/
public function command($cmd) {
$cmd = 'export DYLD_LIBRARY_PATH="/usr/lib/":$DYLD_LIBRARY_PATH && ' . $cmd . ' 2>&1'; //osx patch
$this->lastCommand = $cmd;
exec($cmd, $res, $this->lastCommandCode);
if ($this->lastCommandCode != 0) {
$this->lastError = $res;
}
return $res;
}
}
?>