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 function getSCMMetaData() {
14 'tools' => array('git'),
18 function __construct($ar = null) {
20 parent::__construct($ar);
22 /* transparently handle bare vs. non bare repos */
23 $this->gitdir = $this->repopath;
24 if (is_dir("$this->repopath/.git")) {
25 $this->gitdir .= "/.git";
30 function getServerURL() {
31 $url = parent::getServerURL();
32 if ($url) return $url;
33 $url = MTrackConfig::get('repos', 'serverurl');
35 return "$url:" . $this->getBrowseRootName();
40 public function reconcileRepoSettings(MTrackSCM $r = null) {
45 if (!is_dir($r->repopath)) {
46 $userdata = MTrackAuth::getUserData(MTrackAuth::whoami());
47 $who = $userdata['email'];
48 putenv("GIT_AUTHOR_NAME=$who");
49 putenv("GIT_AUTHOR_EMAIL=$who");
52 $S = MTrack_Repo::loadById($r->clonedfrom);
54 $stm = MTrackSCM::run('git', 'read',
55 array('clone', '--bare', $S->repopath, $r->repopath));
56 $out = stream_get_contents($stm);
58 throw new Exception("git init failed: $out");
62 /* a little peculiar, but bear with it.
63 * We need to have a bare repo so that git doesn't mess around
64 * trying to deal with a checkout in the repo dir.
65 * So we need to create two repos; one bare, one not bare.
66 * We populate the non-bare repo with a dummy file just to have
67 * something to commit, then push non-bare -> bare, and remove non-bare.
70 $stm = MTrackSCM::run('git', 'read',
71 array('init', '--bare', $r->repopath));
72 $out = stream_get_contents($stm);
74 throw new Exception("git init failed: $out");
77 $alt = "$r->repopath.MTRACKINIT";
79 $stm = MTrackSCM::run('git', 'read',
81 $out = stream_get_contents($stm);
83 throw new Exception("git init failed: $out");
89 file_put_contents("$alt/.gitignore", "#\n");
90 $stm = MTrackSCM::run('git', 'read',
91 array('add', '.gitignore'));
92 $out = stream_get_contents($stm);
94 throw new Exception("git add .gitignore failed: $out");
96 $stm = MTrackSCM::run('git', 'read',
97 array('commit', '-a', '-m', 'init'));
98 $out = stream_get_contents($stm);
100 throw new Exception("git commit failed: $out");
102 $stm = MTrackSCM::run('git', 'read',
103 array('push', $r->repopath, 'master'));
104 $out = stream_get_contents($stm);
106 throw new Exception("git push failed: $out");
109 system("rm -rf $alt");
112 $php = MTrackConfig::get('tools', 'php');
113 $hook = realpath(dirname(__FILE__) . '/../../bin/git-commit-hook');
114 $conffile = realpath(MTrackConfig::getLocation());
115 foreach (array('pre', 'post') as $step) {
118 exec $php $hook $step $conffile
121 $target = "$r->repopath/hooks/$step-receive";
122 if (file_put_contents("$target.mtrack", $script)) {
123 chmod("$target.mtrack", 0755);
124 rename("$target.mtrack", $target);
129 system("chmod -R 02777 $r->repopath");
137 public function getBranches()
139 if ($this->branches !== null) {
140 return $this->branches;
142 $this->branches = array();
143 $fp = $this->git('branch', '--no-color', '--verbose');
144 while ($line = fgets($fp)) {
145 // * master 61e7e7d oneliner
146 $line = substr($line, 2);
147 list($branch, $rev) = preg_split('/\s+/', $line);
148 $this->branches[$branch] = $rev;
151 return $this->branches;
154 public function getTags()
156 if ($this->tags !== null) {
159 $this->tags = array();
160 $fp = $this->git('tag');
161 while ($line = fgets($fp)) {
163 $this->tags[$line] = $line;
169 public function readdir($path, $object = null, $ident = null)
173 if ($object === null) {
177 $rev = $this->resolveRevision(null, $object, $ident);
180 $path = rtrim($path, '/') . '/';
183 $fp = $this->git('ls-tree', $rev, $path);
186 require_once 'MTrack/SCM/Git/File.php';
187 while ($line = fgets($fp)) {
188 // blob = file, tree = dir..
189 list($mode, $type, $hash, $name) = preg_split("/\s+/", $line);
190 //echo '<PRE>';echo $line ."\n</PRE>";
191 $res[] = new MTrack_SCM_Git_File($this, "$name", $rev, $type == 'tree', $hash);
196 public function file($path, $object = null, $ident = null)
198 if ($object == null) {
199 $branches = $this->getBranches();
200 if (isset($branches['master'])) {
208 $rev = $this->resolveRevision(null, $object, $ident);
209 require_once 'MTrack/SCM/Git/File.php';
210 return new MTrack_SCM_Git_File($this, $path, $rev);
215 * @param string path (can be empty - eg. '')
216 * @param {number|date} limit how many to fetch
217 * @param {string} object = eg. rev|tag|branch (use 'rev' here and ident=HASH to retrieve a speific revision
218 * @param {string} ident =
221 public function history($path, $limit = null, $object = null, $ident = null)
228 if ($object !== null) {
229 $rev = $this->resolveRevision(null, $object, $ident); // from scm...
236 if ($limit !== null) {
237 if (is_int($limit)) {
238 $args[] = "--max-count=$limit";
239 } else if (is_array($limit)) {
240 $args[] = "--skip={$limit[0]} --max-count={$limit[1]}";
242 $args[] = "--since=$limit";
245 $args[] = "--no-color";
246 //$args[] = "--name-status";
248 $args[] = "--no-abbrev";
249 $args[] = "--numstat";
250 $args[] = "--date=rfc";
253 //echo '<PRE>';print_r($args);echo '</PRE>';
254 $path = ltrim($path, '/');
255 //print_R(array($args, '--' ,$path));
256 $fp = $this->git('log', $args, '--', $path);
262 if ($line === false) {
263 if ($commit !== null) {
264 $commits[] = $commit;
268 if (preg_match("/^commit/", $line)) {
269 if ($commit !== null) {
270 $commits[] = $commit;
277 require_once 'MTrack/SCM/Git/Event.php';
278 foreach ($commits as $commit) {
279 $res[] = MTrack_SCM_Git_Event::newFromCommit($commit, $this);
286 public function diff($path, $from = null, $to = null)
288 require_once 'MTrack/SCMFile.php';
290 if ($path instanceof MTrackSCMFile) {
291 if ($from === null) {
297 // if it's a file event.. we are even lucker..
298 if ($path instanceof MTrackSCMFileEvent) {
299 return $this->git('log', '--max-count=1', '--format=format:', '--patch', $from, '--', $path->name);
305 return $this->git('diff', "$from..$to", '--', $path);
307 return $this->git('diff', "$from^..$from", '--', $path);
310 public function getWorkingCopy()
312 require_once 'MTrack/SCM/Git/WorkingCopy.php';
313 return new MTrack_SCM_Git_WorkingCopy($this);
316 public function getRelatedChanges($revision) // pretty nasty.. could end up with 1000's of changes..
321 $fp = $this->git('rev-parse', "$revision^");
322 while (($line = fgets($fp)) !== false) {
323 $parents[] = trim($line);
326 // Ugh!: http://stackoverflow.com/questions/1761825/referencing-the-child-of-a-commit-in-git
327 $fp = $this->git('rev-list', '--all', '--parents');
328 while (($line = fgets($fp)) !== false) {
329 $hashes = preg_split("/\s+/", $line);
330 $kid = array_shift($hashes);
331 if (in_array($revision, $hashes)) {
336 return array($parents, $kids);
341 $args = func_get_args();
343 "--git-dir=$this->gitdir"
348 if ($this->gitdir != $this->repopath) {
349 // print_r(array($this->gitdir , $this->repopath));
351 //$a[] = "--work-tree=$this->repopath";
353 foreach ($args as $arg) {
354 if (is_array($arg)) {
355 $a = array_merge($a, $arg);
360 var_dump('git ' . join (' ' , $a));
362 return MTrackSCM::run('git', 'read', $a);