2008-07-26 16:42:41 +00:00
|
|
|
<?php
|
|
|
|
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
# ***** BEGIN LICENSE BLOCK *****
|
|
|
|
# This file is part of InDefero, an open source project management application.
|
|
|
|
# Copyright (C) 2008 Céondo Ltd and contributors.
|
|
|
|
#
|
|
|
|
# InDefero is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# InDefero is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
#
|
|
|
|
# ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Git utils.
|
2008-07-27 15:43:51 +00:00
|
|
|
*
|
2008-07-26 16:42:41 +00:00
|
|
|
*/
|
2008-09-01 19:42:18 +00:00
|
|
|
class IDF_Scm_Git
|
2008-07-26 16:42:41 +00:00
|
|
|
{
|
|
|
|
public $repo = '';
|
2008-07-27 15:43:51 +00:00
|
|
|
public $mediumtree_fmt = 'commit %H%nAuthor: %an <%ae>%nTree: %T%nDate: %ai%n%n%s%n%n%b';
|
2008-07-26 16:42:41 +00:00
|
|
|
|
|
|
|
public function __construct($repo)
|
|
|
|
{
|
|
|
|
$this->repo = $repo;
|
|
|
|
}
|
|
|
|
|
2008-12-05 10:34:02 +00:00
|
|
|
/**
|
|
|
|
* Given the string describing the author from the log find the
|
|
|
|
* author in the database.
|
|
|
|
*
|
|
|
|
* @param string Author
|
|
|
|
* @return mixed Pluf_User or null
|
|
|
|
*/
|
|
|
|
public function findAuthor($author)
|
|
|
|
{
|
|
|
|
// We extract the email.
|
|
|
|
$match = array();
|
|
|
|
if (!preg_match('/<(.*)>/', $author, $match)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$sql = new Pluf_SQL('email=%s', array($match[1]));
|
|
|
|
$users = Pluf::factory('Pluf_User')->getList(array('filter'=>$sql->gen()));
|
|
|
|
return ($users->count() > 0) ? $users[0] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-02 13:51:57 +00:00
|
|
|
/**
|
|
|
|
* Returns the URL of the git daemon.
|
|
|
|
*
|
|
|
|
* @param IDF_Project
|
|
|
|
* @return string URL
|
|
|
|
*/
|
|
|
|
public static function getRemoteAccessUrl($project)
|
|
|
|
{
|
2008-11-24 19:27:03 +00:00
|
|
|
return sprintf(Pluf::f('git_remote_url'), $project->shortname);
|
2008-09-02 13:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns this object correctly initialized for the project.
|
|
|
|
*
|
|
|
|
* @param IDF_Project
|
|
|
|
* @return IDF_Scm_Git
|
|
|
|
*/
|
|
|
|
public static function factory($project)
|
|
|
|
{
|
2008-11-24 19:27:03 +00:00
|
|
|
$rep = sprintf(Pluf::f('git_repositories'), $project->shortname);
|
2008-09-02 13:51:57 +00:00
|
|
|
return new IDF_Scm_Git($rep);
|
|
|
|
}
|
2008-07-27 15:43:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test a given object hash.
|
|
|
|
*
|
|
|
|
* @param string Object hash.
|
2008-08-29 17:50:10 +00:00
|
|
|
* @param null to be svn client compatible
|
2008-07-27 15:43:51 +00:00
|
|
|
* @return mixed false if not valid or 'blob', 'tree', 'commit'
|
|
|
|
*/
|
2008-08-29 17:50:10 +00:00
|
|
|
public function testHash($hash, $dummy=null)
|
2008-07-27 15:43:51 +00:00
|
|
|
{
|
|
|
|
$cmd = sprintf('GIT_DIR=%s git cat-file -t %s',
|
|
|
|
escapeshellarg($this->repo),
|
|
|
|
escapeshellarg($hash));
|
|
|
|
$ret = 0; $out = array();
|
2008-11-25 20:18:12 +00:00
|
|
|
IDF_Scm::exec($cmd, $out, $ret);
|
2008-07-27 15:43:51 +00:00
|
|
|
if ($ret != 0) return false;
|
|
|
|
return trim($out[0]);
|
|
|
|
}
|
|
|
|
|
2008-07-26 16:42:41 +00:00
|
|
|
/**
|
2008-07-27 15:43:51 +00:00
|
|
|
* Given a commit hash returns an array of files in it.
|
2008-07-26 16:42:41 +00:00
|
|
|
*
|
|
|
|
* A file is a class with the following properties:
|
|
|
|
*
|
|
|
|
* 'perm', 'type', 'size', 'hash', 'file'
|
|
|
|
*
|
2008-07-27 15:43:51 +00:00
|
|
|
* @param string Commit ('HEAD')
|
2008-07-26 16:42:41 +00:00
|
|
|
* @param string Base folder ('')
|
|
|
|
* @return array
|
|
|
|
*/
|
2008-07-27 15:43:51 +00:00
|
|
|
public function filesAtCommit($commit='HEAD', $folder='')
|
2008-07-26 16:42:41 +00:00
|
|
|
{
|
2008-07-27 15:43:51 +00:00
|
|
|
// now we grab the info about this commit including its tree.
|
|
|
|
$co = $this->getCommit($commit);
|
|
|
|
if ($folder) {
|
|
|
|
// As we are limiting to a given folder, we need to find
|
|
|
|
// the tree corresponding to this folder.
|
|
|
|
$found = false;
|
|
|
|
foreach ($this->getTreeInfo($co->tree) as $file) {
|
|
|
|
if ($file->type == 'tree' and $file->file == $folder) {
|
|
|
|
$found = true;
|
|
|
|
$tree = $file->hash;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$found) {
|
|
|
|
throw new Exception(sprintf(__('Folder %1$s not found in commit %2$s.'), $folder, $commit));
|
|
|
|
}
|
2008-07-26 16:42:41 +00:00
|
|
|
} else {
|
2008-07-27 15:43:51 +00:00
|
|
|
$tree = $co->tree;
|
2008-07-26 16:42:41 +00:00
|
|
|
}
|
|
|
|
$res = array();
|
2008-07-27 15:43:51 +00:00
|
|
|
// get the raw log corresponding to this commit to find the
|
|
|
|
// origin of each file.
|
2008-07-26 23:31:10 +00:00
|
|
|
$rawlog = array();
|
2008-07-27 15:43:51 +00:00
|
|
|
$cmd = sprintf('GIT_DIR=%s git log --raw --abbrev=40 --pretty=oneline %s',
|
|
|
|
escapeshellarg($this->repo), escapeshellarg($commit));
|
2008-11-25 20:18:12 +00:00
|
|
|
IDF_Scm::exec($cmd, $rawlog);
|
2008-07-27 15:43:51 +00:00
|
|
|
// We reverse the log to be able to use a fixed efficient
|
|
|
|
// regex without back tracking.
|
2008-07-26 23:31:10 +00:00
|
|
|
$rawlog = implode("\n", array_reverse($rawlog));
|
2008-07-27 15:43:51 +00:00
|
|
|
foreach ($this->getTreeInfo($tree, false) as $file) {
|
|
|
|
// Now we grab the files in the current tree with as much
|
|
|
|
// information as possible.
|
2008-07-26 23:31:10 +00:00
|
|
|
$matches = array();
|
2008-07-27 15:43:51 +00:00
|
|
|
if ($file->type == 'blob' and preg_match('/^\:\d{6} \d{6} [0-9a-f]{40} '.$file->hash.' .*^([0-9a-f]{40})/msU',
|
2008-11-25 20:18:12 +00:00
|
|
|
$rawlog, $matches)) {
|
2008-07-27 15:43:51 +00:00
|
|
|
$fc = $this->getCommit($matches[1]);
|
|
|
|
$file->date = $fc->date;
|
|
|
|
$file->log = $fc->title;
|
2008-11-07 13:32:22 +00:00
|
|
|
$file->author = $fc->author;
|
2008-07-27 15:43:51 +00:00
|
|
|
} else if ($file->type == 'blob') {
|
|
|
|
$file->date = $co->date;
|
|
|
|
$file->log = $co->title;
|
2008-11-07 13:32:22 +00:00
|
|
|
$file->author = $co->author; // May be wrong in some cases.
|
2008-07-26 23:31:10 +00:00
|
|
|
}
|
2008-07-27 15:43:51 +00:00
|
|
|
$file->fullpath = ($folder) ? $folder.'/'.$file->file : $file->file;
|
|
|
|
$res[] = $file;
|
|
|
|
}
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the tree info.
|
|
|
|
*
|
|
|
|
* @param string Tree hash
|
|
|
|
* @param bool Do we recurse in subtrees (true)
|
|
|
|
* @return array Array of file information.
|
|
|
|
*/
|
|
|
|
public function getTreeInfo($tree, $recurse=true)
|
|
|
|
{
|
|
|
|
if ('tree' != $this->testHash($tree)) {
|
|
|
|
throw new Exception(sprintf(__('Not a valid tree: %s.'), $tree));
|
|
|
|
}
|
|
|
|
$cmd_tmpl = 'GIT_DIR=%s git-ls-tree%s -t -l %s';
|
|
|
|
$cmd = sprintf($cmd_tmpl,
|
|
|
|
escapeshellarg($this->repo),
|
|
|
|
($recurse) ? ' -r' : '',
|
|
|
|
escapeshellarg($tree));
|
|
|
|
$out = array();
|
|
|
|
$res = array();
|
2008-11-25 20:18:12 +00:00
|
|
|
IDF_Scm::exec($cmd, $out);
|
2008-07-27 15:43:51 +00:00
|
|
|
foreach ($out as $line) {
|
|
|
|
list($perm, $type, $hash, $size, $file) = preg_split('/ |\t/', $line, 5, PREG_SPLIT_NO_EMPTY);
|
2008-07-26 16:42:41 +00:00
|
|
|
$res[] = (object) array('perm' => $perm, 'type' => $type,
|
|
|
|
'size' => $size, 'hash' => $hash,
|
|
|
|
'file' => $file);
|
|
|
|
}
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2008-07-27 15:43:51 +00:00
|
|
|
|
2008-07-26 16:42:41 +00:00
|
|
|
/**
|
|
|
|
* Get the file info.
|
|
|
|
*
|
2008-07-26 23:31:10 +00:00
|
|
|
* @param string File
|
2008-07-27 15:43:51 +00:00
|
|
|
* @param string Commit ('HEAD')
|
2008-07-26 23:31:10 +00:00
|
|
|
* @return false Information
|
2008-07-26 16:42:41 +00:00
|
|
|
*/
|
2008-07-27 15:43:51 +00:00
|
|
|
public function getFileInfo($totest, $commit='HEAD')
|
2008-07-26 16:42:41 +00:00
|
|
|
{
|
|
|
|
$cmd_tmpl = 'GIT_DIR=%s git-ls-tree -r -t -l %s';
|
2008-07-27 15:43:51 +00:00
|
|
|
$cmd = sprintf($cmd_tmpl,
|
|
|
|
escapeshellarg($this->repo),
|
|
|
|
escapeshellarg($commit));
|
2008-07-26 16:42:41 +00:00
|
|
|
$out = array();
|
2008-11-25 20:18:12 +00:00
|
|
|
IDF_Scm::exec($cmd, $out);
|
2008-07-26 16:42:41 +00:00
|
|
|
foreach ($out as $line) {
|
|
|
|
list($perm, $type, $hash, $size, $file) = preg_split('/ |\t/', $line, 5, PREG_SPLIT_NO_EMPTY);
|
|
|
|
if ($totest == $file) {
|
|
|
|
return (object) array('perm' => $perm, 'type' => $type,
|
|
|
|
'size' => $size, 'hash' => $hash,
|
|
|
|
'file' => $file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a blob.
|
|
|
|
*
|
2008-09-12 10:26:51 +00:00
|
|
|
* @param string request_file_info
|
|
|
|
* @param null to be svn client compatible
|
2008-07-26 16:42:41 +00:00
|
|
|
* @return string Raw blob
|
|
|
|
*/
|
2008-09-12 10:26:51 +00:00
|
|
|
public function getBlob($request_file_info, $dummy=null)
|
2008-07-26 16:42:41 +00:00
|
|
|
{
|
2008-11-21 20:47:43 +00:00
|
|
|
return shell_exec(sprintf('GIT_DIR=%s git-cat-file blob %s',
|
|
|
|
escapeshellarg($this->repo),
|
|
|
|
escapeshellarg($request_file_info->hash)));
|
2008-07-26 16:42:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the branches.
|
2008-07-26 23:37:43 +00:00
|
|
|
*
|
|
|
|
* @return array Branches.
|
2008-07-26 16:42:41 +00:00
|
|
|
*/
|
|
|
|
public function getBranches()
|
|
|
|
{
|
|
|
|
$out = array();
|
2008-11-07 22:54:40 +00:00
|
|
|
IDF_Scm::exec(sprintf('GIT_DIR=%s git branch',
|
2008-11-25 20:18:12 +00:00
|
|
|
escapeshellarg($this->repo)), $out);
|
2008-07-26 16:42:41 +00:00
|
|
|
$res = array();
|
|
|
|
foreach ($out as $b) {
|
|
|
|
$res[] = substr($b, 2);
|
|
|
|
}
|
|
|
|
return $res;
|
|
|
|
}
|
2008-07-26 23:31:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get commit details.
|
|
|
|
*
|
|
|
|
* @param string Commit ('HEAD').
|
|
|
|
* @return array Changes.
|
|
|
|
*/
|
|
|
|
public function getCommit($commit='HEAD')
|
|
|
|
{
|
2008-07-27 15:43:51 +00:00
|
|
|
$cmd = sprintf('GIT_DIR=%s git show --date=iso --pretty=format:%s %s',
|
|
|
|
escapeshellarg($this->repo),
|
|
|
|
"'".$this->mediumtree_fmt."'",
|
2008-07-27 15:46:04 +00:00
|
|
|
escapeshellarg($commit));
|
2008-07-26 23:31:10 +00:00
|
|
|
$out = array();
|
2008-11-25 20:18:12 +00:00
|
|
|
IDF_Scm::exec($cmd, $out);
|
2008-07-26 23:31:10 +00:00
|
|
|
$log = array();
|
|
|
|
$change = array();
|
|
|
|
$inchange = false;
|
|
|
|
foreach ($out as $line) {
|
|
|
|
if (!$inchange and 0 === strpos($line, 'diff --git a')) {
|
|
|
|
$inchange = true;
|
|
|
|
}
|
|
|
|
if ($inchange) {
|
|
|
|
$change[] = $line;
|
|
|
|
} else {
|
|
|
|
$log[] = $line;
|
|
|
|
}
|
|
|
|
}
|
2008-07-27 15:43:51 +00:00
|
|
|
$out = self::parseLog($log, 4);
|
2008-07-28 20:09:17 +00:00
|
|
|
$out[0]->changes = implode("\n", $change);
|
2008-07-26 23:31:10 +00:00
|
|
|
return $out[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get latest changes.
|
|
|
|
*
|
2008-07-27 15:43:51 +00:00
|
|
|
* @param string Commit ('HEAD').
|
2008-07-26 23:31:10 +00:00
|
|
|
* @param int Number of changes (10).
|
|
|
|
* @return array Changes.
|
|
|
|
*/
|
2008-07-27 15:43:51 +00:00
|
|
|
public function getChangeLog($commit='HEAD', $n=10)
|
2008-07-26 23:31:10 +00:00
|
|
|
{
|
|
|
|
if ($n === null) $n = '';
|
|
|
|
else $n = ' -'.$n;
|
|
|
|
$cmd = sprintf('GIT_DIR=%s git log%s --date=iso --pretty=format:\'%s\' %s',
|
2008-07-27 15:43:51 +00:00
|
|
|
escapeshellarg($this->repo), $n, $this->mediumtree_fmt,
|
|
|
|
escapeshellarg($commit));
|
2008-07-26 23:31:10 +00:00
|
|
|
$out = array();
|
2008-11-25 20:18:12 +00:00
|
|
|
IDF_Scm::exec($cmd, $out);
|
2008-07-26 23:31:10 +00:00
|
|
|
return self::parseLog($out, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the log lines of a --pretty=medium log output.
|
|
|
|
*
|
|
|
|
* @param array Lines.
|
|
|
|
* @param int Number of lines in the headers (3)
|
|
|
|
* @return array Change log.
|
|
|
|
*/
|
|
|
|
public static function parseLog($lines, $hdrs=3)
|
|
|
|
{
|
|
|
|
$res = array();
|
|
|
|
$c = array();
|
|
|
|
$i = 0;
|
|
|
|
$hdrs += 2;
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
$i++;
|
|
|
|
if (0 === strpos($line, 'commit')) {
|
|
|
|
if (count($c) > 0) {
|
|
|
|
$c['full_message'] = trim($c['full_message']);
|
|
|
|
$res[] = (object) $c;
|
|
|
|
}
|
|
|
|
$c = array();
|
|
|
|
$c['commit'] = trim(substr($line, 7));
|
|
|
|
$c['full_message'] = '';
|
|
|
|
$i=1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($i == $hdrs) {
|
|
|
|
$c['title'] = trim($line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$match = array();
|
|
|
|
if (preg_match('/(\S+)\s*:\s*(.*)/', $line, $match)) {
|
|
|
|
$match[1] = strtolower($match[1]);
|
|
|
|
$c[$match[1]] = trim($match[2]);
|
|
|
|
if ($match[1] == 'date') {
|
|
|
|
$c['date'] = gmdate('Y-m-d H:i:s', strtotime($match[2]));
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($i > ($hdrs+1)) {
|
|
|
|
$c['full_message'] .= trim($line)."\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$c['full_message'] = trim($c['full_message']);
|
2008-08-29 17:50:10 +00:00
|
|
|
|
2008-07-26 23:31:10 +00:00
|
|
|
$res[] = (object) $c;
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2008-08-02 11:51:42 +00:00
|
|
|
/**
|
|
|
|
* Generate the command to create a zip archive at a given commit.
|
|
|
|
*
|
|
|
|
* @param string Commit
|
|
|
|
* @param string Prefix ('git-repo-dump')
|
|
|
|
* @return string Command
|
|
|
|
*/
|
|
|
|
public function getArchiveCommand($commit, $prefix='git-repo-dump/')
|
|
|
|
{
|
|
|
|
return sprintf('GIT_DIR=%s git archive --format=zip --prefix=%s %s',
|
|
|
|
escapeshellarg($this->repo),
|
|
|
|
escapeshellarg($prefix),
|
|
|
|
escapeshellarg($commit));
|
|
|
|
}
|
|
|
|
|
2008-07-26 16:42:41 +00:00
|
|
|
}
|