2 require_once 'MTrack/Repo.php';
5 class MTrack_SCM_Git_Repo extends MTrack_Repo
7 protected $branches = null;
8 protected $tags = null;
11 public $debug = false;
15 public function getSCMMetaData() {
18 'tools' => array('git'),
22 function __construct($ar = null) {
24 parent::__construct($ar);
26 /* transparently handle bare vs. non bare repos */
27 $this->gitdir = $this->repopath;
28 if (is_dir("$this->repopath/.git")) {
29 $this->gitdir .= "/.git";
34 function getServerURL() {
35 $url = parent::getServerURL();
36 if ($url) return $url;
37 $url = MTrackConfig::get('repos', 'serverurl');
39 return "$url:" . $this->getBrowseRootName();
44 public function reconcileRepoSettings(MTrackSCM $r = null) {
49 if (!is_dir($r->repopath)) {
50 $userdata = MTrackAuth::getUserData(MTrackAuth::whoami());
51 $who = $userdata['email'];
52 putenv("GIT_AUTHOR_NAME=$who");
53 putenv("GIT_AUTHOR_EMAIL=$who");
56 $S = MTrack_Repo::loadById($r->clonedfrom);
58 $stm = MTrackSCM::run('git', 'read',
59 array('clone', '--bare', $S->repopath, $r->repopath));
60 $out = stream_get_contents($stm);
62 throw new Exception("git init failed: $out");
66 /* a little peculiar, but bear with it.
67 * We need to have a bare repo so that git doesn't mess around
68 * trying to deal with a checkout in the repo dir.
69 * So we need to create two repos; one bare, one not bare.
70 * We populate the non-bare repo with a dummy file just to have
71 * something to commit, then push non-bare -> bare, and remove non-bare.
74 $stm = MTrackSCM::run('git', 'read',
75 array('init', '--bare', $r->repopath));
76 $out = stream_get_contents($stm);
78 throw new Exception("git init failed: $out");
81 $alt = "$r->repopath.MTRACKINIT";
83 $stm = MTrackSCM::run('git', 'read',
85 $out = stream_get_contents($stm);
87 throw new Exception("git init failed: $out");
93 file_put_contents("$alt/.gitignore", "#\n");
94 $stm = MTrackSCM::run('git', 'read',
95 array('add', '.gitignore'));
96 $out = stream_get_contents($stm);
98 throw new Exception("git add .gitignore failed: $out");
100 $stm = MTrackSCM::run('git', 'read',
101 array('commit', '-a', '-m', 'init'));
102 $out = stream_get_contents($stm);
104 throw new Exception("git commit failed: $out");
106 $stm = MTrackSCM::run('git', 'read',
107 array('push', $r->repopath, 'master'));
108 $out = stream_get_contents($stm);
110 throw new Exception("git push failed: $out");
113 system("rm -rf $alt");
116 $php = MTrackConfig::get('tools', 'php');
117 $hook = realpath(dirname(__FILE__) . '/../../bin/git-commit-hook');
118 $conffile = realpath(MTrackConfig::getLocation());
119 foreach (array('pre', 'post') as $step) {
122 exec $php $hook $step $conffile
125 $target = "$r->repopath/hooks/$step-receive";
126 if (file_put_contents("$target.mtrack", $script)) {
127 chmod("$target.mtrack", 0755);
128 rename("$target.mtrack", $target);
133 system("chmod -R 02777 $r->repopath");
141 public function getBranches()
143 if ($this->branches !== null) {
144 return $this->branches;
146 $this->branches = array();
147 $fp = $this->git('branch', '--no-color', '--verbose');
148 while ($line = fgets($fp)) {
149 // * master 61e7e7d oneliner
150 $line = substr($line, 2);
151 list($branch, $rev) = preg_split('/\s+/', $line);
152 $this->branches[$branch] = $rev;
155 return $this->branches;
158 public function getTags()
160 if ($this->tags !== null) {
163 $this->tags = array();
164 $fp = $this->git('tag');
165 while ($line = fgets($fp)) {
167 $this->tags[$line] = $line;
173 public function readdir($path, $object = null, $ident = null)
177 if ($object === null) {
181 $rev = $this->resolveRevision(null, $object, $ident);
184 $path = rtrim($path, '/') . '/';
187 $fp = $this->git('ls-tree', $rev, $path);
190 require_once 'MTrack/SCM/Git/File.php';
191 while ($line = fgets($fp)) {
192 // blob = file, tree = dir..
193 list($mode, $type, $hash, $name) = preg_split("/\s+/", $line);
194 //echo '<PRE>';echo $line ."\n</PRE>";
195 $res[] = new MTrack_SCM_Git_File($this, "$name", $rev, $type == 'tree', $hash);
200 public function file($path, $object = null, $ident = null)
202 if ($object == null) {
203 $branches = $this->getBranches();
204 if (isset($branches['master'])) {
212 $rev = $this->resolveRevision(null, $object, $ident);
213 require_once 'MTrack/SCM/Git/File.php';
214 return new MTrack_SCM_Git_File($this, $path, $rev);
219 * @param string path (can be empty - eg. '')
220 * @param {number|date} limit how many to fetch
224 * @param {string} object = eg. rev|tag|branch (use 'rev' here and ident=HASH to retrieve a speific revision
225 * @param {string} ident =
228 * - fetch on revision?: '.',1,'rev','xxxxxxxxxx'
231 * range... object ='rev' ident ='master..release'
234 public function history($path, $limit = null, $object = null, $ident = null)
241 if ($object !== null) {
242 $rev = $this->resolveRevision(null, $object, $ident); // from scm...
248 if ($limit !== null) {
249 if (is_int($limit)) {
250 $args[] = "--max-count=$limit";
251 } else if (is_array($limit) && isset($limit[0]) && isset($limit[1])) {
253 $args[] = "--skip={$limit[0]} --max-count={$limit[1]}";
256 /// oh what a horible hack.. - bad api design here.
257 } else if (is_array($limit) ) {
258 foreach($limit as $k=>$v) {
260 $args[] = ($k == '-') ? $v : ('--'. $k .'='. $v);
264 } else if (strpos($limit,'..') !== false ) {
267 $args[] = "--since=$limit";
270 $args[] = "--no-color";
271 //$args[] = "--name-status";
273 $args[] = "--no-abbrev";
274 $args[] = "--numstat";
275 $args[] = "--date=rfc";
276 $args[] = "--source"; // show the branch..
279 //echo '<PRE>';print_r($args);echo '</PRE>';
280 $path = ltrim($path, '/');
284 // print_R(array($args, '--' ,$path));exit;
285 $fp = $this->git('log', $args, '--', $path);
291 if ($line === false) { //end of file..
292 if ($commit !== null) {
293 $commits[] = $commit;
297 if (preg_match("/^commit/", $line)) {
298 if ($commit !== null) {
299 $commits[] = $commit;
306 require_once 'MTrack/SCM/Git/Event.php';
307 foreach ($commits as $commit) {
308 $res[] = MTrack_SCM_Git_Event::newFromCommit($commit, $this);
315 public function diff($path, $from = null, $to = null)
317 require_once 'MTrack/SCMFile.php';
319 if ($path instanceof MTrackSCMFile) {
320 if ($from === null) {
326 // if it's a file event.. we are even lucker..
327 if ($path instanceof MTrackSCMFileEvent) {
328 return $this->git('log', '--max-count=1', '--format=format:', '--patch', $from, '--', $path->name);
334 return $this->git('diff', "$from..$to", '--', $path);
336 return $this->git('diff', "$from^..$from", '--', $path);
339 public function getWorkingCopy()
341 require_once 'MTrack/SCM/Git/WorkingCopy.php';
342 return new MTrack_SCM_Git_WorkingCopy($this);
345 public function getRelatedChanges($revision) // pretty nasty.. could end up with 1000's of changes..
350 $fp = $this->git('rev-parse', "$revision^");
351 while (($line = fgets($fp)) !== false) {
352 $parents[] = trim($line);
355 // Ugh!: http://stackoverflow.com/questions/1761825/referencing-the-child-of-a-commit-in-git
356 $fp = $this->git('rev-list', '--all', '--parents');
357 while (($line = fgets($fp)) !== false) {
358 $hashes = preg_split("/\s+/", $line);
359 $kid = array_shift($hashes);
360 if (in_array($revision, $hashes)) {
365 return array($parents, $kids);
370 $args = func_get_args();
372 "--git-dir=$this->gitdir"
377 if ($this->gitdir != $this->repopath) {
378 // print_r(array($this->gitdir , $this->repopath));
380 //$a[] = "--work-tree=$this->repopath";
382 foreach ($args as $arg) {
383 if (is_array($arg)) {
384 $a = array_merge($a, $arg);
390 var_dump('git ' . join (' ' , $a));
393 //echo "git " . implode(" " , $a) . "\n";
394 return MTrackSCM::run('git', 'read', $a);
398 function commitLogToEvent($str) {
399 require_once 'MTrack/SCM/Git/Event.php';
400 return MTrack_SCM_Git_Event::newFromCommit($str,$this);