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.
|
2011-04-02 19:37:07 +00:00
|
|
|
# Copyright (C) 2008-2011 Céondo Ltd and contributors.
|
2008-07-26 16:42:41 +00:00
|
|
|
#
|
|
|
|
# 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
|
|
|
*/
|
2009-04-21 12:13:44 +00:00
|
|
|
class IDF_Scm_Git extends IDF_Scm
|
2008-07-26 16:42:41 +00:00
|
|
|
{
|
2011-01-20 09:25:41 +00:00
|
|
|
public $mediumtree_fmt = 'commit %H%nAuthor: %an <%ae>%nTree: %T%nParents: %P%nDate: %ai%n%n%s%n%n%b';
|
2010-05-01 22:56:04 +00:00
|
|
|
|
2009-04-21 12:13:44 +00:00
|
|
|
/* ============================================== *
|
|
|
|
* *
|
|
|
|
* Common Methods Implemented By All The SCMs *
|
|
|
|
* *
|
2010-05-01 22:56:04 +00:00
|
|
|
* ============================================== */
|
2009-04-21 12:13:44 +00:00
|
|
|
|
2009-05-27 21:08:55 +00:00
|
|
|
public function __construct($repo, $project=null)
|
|
|
|
{
|
|
|
|
$this->repo = $repo;
|
|
|
|
$this->project = $project;
|
|
|
|
}
|
|
|
|
|
2011-02-24 14:21:51 +00:00
|
|
|
/**
|
|
|
|
* @see IDF_Scm::getChanges()
|
|
|
|
*
|
|
|
|
* Git command output is like :
|
|
|
|
* M doc/Guide utilisateur/Manuel_distrib.tex
|
|
|
|
* M doc/Guide utilisateur/Manuel_intro.tex
|
|
|
|
* M doc/Guide utilisateur/Manuel_libpegase_exemples.tex
|
|
|
|
* M doc/Guide utilisateur/Manuel_page1_version.tex
|
|
|
|
* A doc/Guide utilisateur/images/ftp-nautilus.png
|
|
|
|
* M doc/Guide utilisateur/textes/log_boot_PEGASE.txt
|
|
|
|
*
|
|
|
|
* Status letters mean : Added (A), Deleted (D), Modified (M), Renamed (R)
|
|
|
|
*/
|
|
|
|
public function getChanges($commit)
|
|
|
|
{
|
2011-02-25 10:10:10 +00:00
|
|
|
$cmd = sprintf('GIT_DIR=%s '.Pluf::f('git_path', 'git').' show %s --name-status --pretty="format:" --diff-filter="[A|D|M|R]" -M',
|
2011-02-24 14:21:51 +00:00
|
|
|
escapeshellarg($this->repo),
|
|
|
|
escapeshellarg($commit));
|
|
|
|
$out = array();
|
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
|
|
|
|
self::exec('IDF_Scm_Git::getChanges', $cmd, $out);
|
2011-03-16 22:03:36 +00:00
|
|
|
|
2011-02-24 14:21:51 +00:00
|
|
|
$return = (object) array(
|
|
|
|
'additions' => array(),
|
|
|
|
'deletions' => array(),
|
|
|
|
'renames' => array(),
|
2011-06-09 23:34:33 +00:00
|
|
|
'copies' => array(),
|
2011-02-24 14:21:51 +00:00
|
|
|
'patches' => array(),
|
|
|
|
'properties' => array(),
|
2011-03-16 22:03:36 +00:00
|
|
|
);
|
|
|
|
|
2011-02-24 14:21:51 +00:00
|
|
|
foreach ($out as $line) {
|
|
|
|
$line = trim($line);
|
|
|
|
if ($line != '') {
|
|
|
|
$action = $line[0];
|
2011-03-16 22:03:36 +00:00
|
|
|
|
2011-02-24 14:21:51 +00:00
|
|
|
if ($action == 'A') {
|
2011-02-25 10:10:10 +00:00
|
|
|
$filename = trim(substr($line, 1));
|
2011-02-24 14:21:51 +00:00
|
|
|
$return->additions[] = $filename;
|
|
|
|
} else if ($action == 'D') {
|
2011-02-25 10:10:10 +00:00
|
|
|
$filename = trim(substr($line, 1));
|
2011-02-24 14:21:51 +00:00
|
|
|
$return->deletions[] = $filename;
|
|
|
|
} else if ($action == 'M') {
|
2011-02-25 10:10:10 +00:00
|
|
|
$filename = trim(substr($line, 1));
|
2011-02-24 14:21:51 +00:00
|
|
|
$return->patches[] = $filename;
|
|
|
|
} else if ($action == 'R') {
|
2011-02-25 10:10:10 +00:00
|
|
|
$matches = split ("\t", $line);
|
|
|
|
$return->renames[$matches[1]] = $matches[2];
|
2011-03-16 22:03:36 +00:00
|
|
|
}
|
2011-02-24 14:21:51 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-16 22:03:36 +00:00
|
|
|
|
2011-02-24 14:21:51 +00:00
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2009-06-19 15:31:45 +00:00
|
|
|
public function getRepositorySize()
|
|
|
|
{
|
2009-10-26 20:45:42 +00:00
|
|
|
if (!file_exists($this->repo)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2010-02-24 14:06:13 +00:00
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '').'du -sk '
|
2009-06-19 15:31:45 +00:00
|
|
|
.escapeshellarg($this->repo);
|
2010-05-01 22:56:04 +00:00
|
|
|
$out = explode(' ',
|
2010-02-17 20:57:28 +00:00
|
|
|
self::shell_exec('IDF_Scm_Git::getRepositorySize', $cmd),
|
|
|
|
2);
|
2009-06-19 19:37:39 +00:00
|
|
|
return (int) $out[0]*1024;
|
2009-06-19 15:31:45 +00:00
|
|
|
}
|
|
|
|
|
2009-04-21 12:13:44 +00:00
|
|
|
public function isAvailable()
|
|
|
|
{
|
|
|
|
try {
|
2009-05-27 15:04:41 +00:00
|
|
|
$branches = $this->getBranches();
|
2009-04-21 12:13:44 +00:00
|
|
|
} catch (IDF_Scm_Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-27 15:04:41 +00:00
|
|
|
return (count($branches) > 0);
|
2009-04-21 12:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getBranches()
|
|
|
|
{
|
|
|
|
if (isset($this->cache['branches'])) {
|
|
|
|
return $this->cache['branches'];
|
|
|
|
}
|
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '')
|
2010-05-01 22:56:04 +00:00
|
|
|
.sprintf('GIT_DIR=%s '.Pluf::f('git_path', 'git').' branch',
|
2009-04-21 12:13:44 +00:00
|
|
|
escapeshellarg($this->repo));
|
2010-05-01 22:56:04 +00:00
|
|
|
self::exec('IDF_Scm_Git::getBranches',
|
2010-02-17 20:57:28 +00:00
|
|
|
$cmd, $out, $return);
|
2009-04-21 12:13:44 +00:00
|
|
|
if ($return != 0) {
|
|
|
|
throw new IDF_Scm_Exception(sprintf($this->error_tpl,
|
2010-05-01 22:56:04 +00:00
|
|
|
$cmd, $return,
|
2009-04-21 12:13:44 +00:00
|
|
|
implode("\n", $out)));
|
|
|
|
}
|
|
|
|
$res = array();
|
|
|
|
foreach ($out as $b) {
|
2009-07-08 11:49:16 +00:00
|
|
|
$b = substr($b, 2);
|
|
|
|
if (false !== strpos($b, '/')) {
|
|
|
|
$res[$this->getCommit($b)->commit] = $b;
|
|
|
|
} else {
|
|
|
|
$res[$b] = '';
|
|
|
|
}
|
2009-04-21 12:13:44 +00:00
|
|
|
}
|
|
|
|
$this->cache['branches'] = $res;
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMainBranch()
|
|
|
|
{
|
2009-11-06 15:12:54 +00:00
|
|
|
$branches = $this->getBranches();
|
|
|
|
if (array_key_exists('master', $branches))
|
|
|
|
return 'master';
|
|
|
|
static $possible = array('main', 'trunk', 'local');
|
|
|
|
for ($i = 0; 3 > $i; ++$i) {
|
|
|
|
if (array_key_exists($possible[$i], $branches))
|
|
|
|
return $possible[$i];
|
2009-04-25 14:24:40 +00:00
|
|
|
}
|
2009-11-06 15:12:54 +00:00
|
|
|
return key($branches);
|
2009-04-25 14:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Note: Running the `git branch --contains $commit` is
|
|
|
|
* theoritically the best way to do it, until you figure out that
|
|
|
|
* you cannot cache the result and that it takes several seconds
|
|
|
|
* to execute on a big tree.
|
|
|
|
*/
|
|
|
|
public function inBranches($commit, $path)
|
|
|
|
{
|
2011-03-11 10:38:04 +00:00
|
|
|
if (isset($this->cache['inBranches'][$commit])) {
|
|
|
|
return $this->cache['inBranches'][$commit];
|
|
|
|
}
|
2011-03-16 22:03:36 +00:00
|
|
|
|
2011-03-11 10:38:04 +00:00
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '')
|
|
|
|
.sprintf('GIT_DIR=%s %s branch --contains %s',
|
|
|
|
escapeshellarg($this->repo),
|
|
|
|
Pluf::f('git_path', 'git'),
|
|
|
|
escapeshellarg($commit));
|
|
|
|
self::exec('IDF_Scm_Git::inBranches', $cmd, $out, $return);
|
|
|
|
if (0 != $return) {
|
|
|
|
throw new IDF_Scm_Exception(sprintf($this->error_tpl,
|
|
|
|
$cmd, $return,
|
|
|
|
implode("\n", $out)));
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = array();
|
|
|
|
foreach ($out as $line) {
|
|
|
|
$res[] = substr($line, 2);
|
|
|
|
}
|
2011-03-16 22:03:36 +00:00
|
|
|
|
2011-03-11 10:38:04 +00:00
|
|
|
$this->cache['inBranches'][$commit] = $res;
|
2011-03-16 22:03:36 +00:00
|
|
|
return $res;
|
2009-11-06 15:12:54 +00:00
|
|
|
}
|
|
|
|
|
2011-01-24 13:20:33 +00:00
|
|
|
/**
|
|
|
|
* Will find the parents if available.
|
|
|
|
*/
|
|
|
|
public function getExtraProperties($obj)
|
|
|
|
{
|
|
|
|
return (isset($obj->parents)) ? array('parents' => $obj->parents) : array();
|
|
|
|
}
|
|
|
|
|
2009-11-06 15:12:54 +00:00
|
|
|
/**
|
|
|
|
* @see IDF_Scm::getTags()
|
|
|
|
**/
|
2010-02-17 20:57:28 +00:00
|
|
|
public function getTags()
|
|
|
|
{
|
|
|
|
if (isset($this->cache['tags'])) {
|
|
|
|
return $this->cache['tags'];
|
|
|
|
}
|
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '')
|
2011-05-26 07:56:04 +00:00
|
|
|
.sprintf('GIT_DIR=%s %s for-each-ref --format="%%(objectname) %%(refname)" refs/tags',
|
2010-02-17 20:57:28 +00:00
|
|
|
escapeshellarg($this->repo),
|
|
|
|
Pluf::f('git_path', 'git'));
|
|
|
|
self::exec('IDF_Scm_Git::getTags', $cmd, $out, $return);
|
|
|
|
if (0 != $return) {
|
|
|
|
throw new IDF_Scm_Exception(sprintf($this->error_tpl,
|
|
|
|
$cmd, $return,
|
|
|
|
implode("\n", $out)));
|
|
|
|
}
|
|
|
|
$res = array();
|
|
|
|
foreach ($out as $b) {
|
2011-05-26 07:56:04 +00:00
|
|
|
$elts = explode(' ', $b, 2);
|
|
|
|
$tag = substr(trim($elts[1]), 10); // Remove refs/tags/ prefix
|
|
|
|
$res[$tag] = '';
|
2010-02-17 20:57:28 +00:00
|
|
|
}
|
2011-06-01 22:22:00 +00:00
|
|
|
krsort($res);
|
2010-02-17 20:57:28 +00:00
|
|
|
$this->cache['tags'] = $res;
|
2011-06-01 22:22:00 +00:00
|
|
|
|
2010-02-17 20:57:28 +00:00
|
|
|
return $res;
|
2009-11-06 15:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see IDF_Scm::inTags()
|
|
|
|
**/
|
|
|
|
public function inTags($commit, $path)
|
|
|
|
{
|
2011-03-16 22:03:36 +00:00
|
|
|
if (isset($this->cache['inTags'][$commit])) {
|
|
|
|
return $this->cache['inTags'][$commit];
|
|
|
|
}
|
2009-04-21 12:13:44 +00:00
|
|
|
|
2011-03-16 22:03:36 +00:00
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '')
|
|
|
|
.sprintf('GIT_DIR=%s %s tag --contains %s',
|
|
|
|
escapeshellarg($this->repo),
|
|
|
|
Pluf::f('git_path', 'git'),
|
|
|
|
escapeshellarg($commit));
|
|
|
|
self::exec('IDF_Scm_Git::inTags', $cmd, $out, $return);
|
2011-03-17 22:12:27 +00:00
|
|
|
// `git tag` gained the `--contains` option in 1.6.2, earlier
|
|
|
|
// versions report a bad usage error (129) which we ignore here
|
|
|
|
if (129 == $return) {
|
|
|
|
$this->cache['inTags'][$commit] = array();
|
2011-03-17 08:28:43 +00:00
|
|
|
return array();
|
2011-03-17 22:12:27 +00:00
|
|
|
}
|
|
|
|
// any other error should of course get noted
|
|
|
|
if (0 != $return) {
|
2011-03-16 22:03:36 +00:00
|
|
|
throw new IDF_Scm_Exception(sprintf($this->error_tpl,
|
|
|
|
$cmd, $return,
|
|
|
|
implode("\n", $out)));
|
2009-11-06 15:12:54 +00:00
|
|
|
}
|
2011-03-16 22:03:36 +00:00
|
|
|
|
|
|
|
$res = array();
|
|
|
|
foreach ($out as $line) {
|
|
|
|
$res[] = $line;
|
2009-11-06 15:12:54 +00:00
|
|
|
}
|
2011-03-16 22:03:36 +00:00
|
|
|
|
|
|
|
$this->cache['inTags'][$commit] = $res;
|
|
|
|
return $res;
|
2009-11-06 15:12:54 +00:00
|
|
|
}
|
2009-04-25 14:24:40 +00:00
|
|
|
|
2009-04-21 12:13:44 +00:00
|
|
|
/**
|
|
|
|
* Git "tree" is not the same as the tree we get here.
|
|
|
|
*
|
|
|
|
* With git each commit object stores a related tree object. This
|
|
|
|
* tree is basically providing what is in the given folder at the
|
|
|
|
* given commit. It looks something like that:
|
|
|
|
*
|
|
|
|
* <pre>
|
|
|
|
* 100644 blob bcd155e609c51b4651aab9838b270cce964670af AUTHORS
|
|
|
|
* 100644 blob 87b44c5c7df3cc90c031317c1ac8efcfd8a13631 COPYING
|
|
|
|
* 100644 blob 2a0f899cbfe33ea755c343b06a13d7de6c22799f INSTALL.mdtext
|
|
|
|
* 040000 tree 2f469c4c5318aa4ad48756874373370f6112f77b doc
|
|
|
|
* 040000 tree 911e0bd2706f0069b04744d6ef41353faf06a0a7 logo
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* You can then follow what is in the given folder (let say doc)
|
|
|
|
* by using the hash.
|
|
|
|
*
|
|
|
|
* This means that you will have not to confuse the git tree and
|
|
|
|
* the output tree in the following method.
|
|
|
|
*
|
|
|
|
* @see http://www.kernel.org/pub/software/scm/git/docs/git-ls-tree.html
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function getTree($commit, $folder='/', $branch=null)
|
|
|
|
{
|
|
|
|
$folder = ($folder == '/') ? '' : $folder;
|
|
|
|
// now we grab the info about this commit including its tree.
|
2009-06-24 17:33:16 +00:00
|
|
|
if (false == ($co = $this->getCommit($commit))) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-04-21 12:13:44 +00:00
|
|
|
if ($folder) {
|
|
|
|
// As we are limiting to a given folder, we need to find
|
|
|
|
// the tree corresponding to this folder.
|
2010-05-01 22:56:04 +00:00
|
|
|
$tinfo = $this->getTreeInfo($commit, $folder);
|
2009-04-21 12:13:44 +00:00
|
|
|
if (isset($tinfo[0]) and $tinfo[0]->type == 'tree') {
|
|
|
|
$tree = $tinfo[0]->hash;
|
|
|
|
} else {
|
|
|
|
throw new Exception(sprintf(__('Folder %1$s not found in commit %2$s.'), $folder, $commit));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$tree = $co->tree;
|
|
|
|
}
|
|
|
|
$res = array();
|
|
|
|
foreach ($this->getTreeInfo($tree) as $file) {
|
|
|
|
// Now we grab the files in the current tree with as much
|
|
|
|
// information as possible.
|
|
|
|
if ($file->type == 'blob') {
|
|
|
|
$file->date = $co->date;
|
2010-05-01 22:56:04 +00:00
|
|
|
$file->log = '----';
|
2009-04-21 12:13:44 +00:00
|
|
|
$file->author = 'Unknown';
|
|
|
|
}
|
|
|
|
$file->fullpath = ($folder) ? $folder.'/'.$file->file : $file->file;
|
2010-01-20 21:33:53 +00:00
|
|
|
$file->efullpath = self::smartEncode($file->fullpath);
|
2009-04-21 12:13:44 +00:00
|
|
|
if ($file->type == 'commit') {
|
|
|
|
// We have a submodule
|
|
|
|
$file = $this->getSubmodule($file, $commit);
|
|
|
|
}
|
|
|
|
$res[] = $file;
|
|
|
|
}
|
|
|
|
// Grab the details for each blob and return the list.
|
|
|
|
return $this->getTreeDetails($res);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2011-06-15 11:50:02 +00:00
|
|
|
// FIXME: newer git versions know a i18n.commitencoding setting which
|
|
|
|
// leads to another header, "encoding", with which we _could_ try to
|
|
|
|
// decode the string into utf8. Unfortunately this does not always
|
|
|
|
// work, especially not in older repos, so we would then still have
|
|
|
|
// to supply some fallback.
|
|
|
|
if (!mb_check_encoding($match[1], 'UTF-8')) {
|
|
|
|
return null;
|
|
|
|
}
|
2011-02-09 15:35:50 +00:00
|
|
|
$sql = new Pluf_SQL('login=%s', array($match[1]));
|
|
|
|
$users = Pluf::factory('Pluf_User')->getList(array('filter'=>$sql->gen()));
|
|
|
|
if ($users->count() > 0) {
|
|
|
|
return $users[0];
|
2009-11-06 14:58:21 +00:00
|
|
|
}
|
2011-02-09 15:35:50 +00:00
|
|
|
return Pluf::factory('IDF_EmailAddress')->get_user_for_email_address($match[1]);
|
2008-12-05 10:34:02 +00:00
|
|
|
}
|
|
|
|
|
2010-05-01 22:56:04 +00:00
|
|
|
public static function getAnonymousAccessUrl($project, $commit=null)
|
2008-09-02 13:51:57 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2010-05-01 22:56:04 +00:00
|
|
|
public static function getAuthAccessUrl($project, $user, $commit=null)
|
2009-01-26 16:58:58 +00:00
|
|
|
{
|
2011-01-06 10:13:29 +00:00
|
|
|
// if the user haven't registred a public ssh key,
|
|
|
|
// he can't use the write url which use the SSH authentification
|
|
|
|
if ($user != null) {
|
|
|
|
$keys = $user->get_idf_key_list();
|
|
|
|
if (count ($keys) == 0)
|
|
|
|
return self::getAnonymousAccessUrl($project);
|
2011-03-16 22:03:36 +00:00
|
|
|
}
|
|
|
|
|
2009-01-26 16:58:58 +00:00
|
|
|
return sprintf(Pluf::f('git_write_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);
|
2009-04-21 15:45:35 +00:00
|
|
|
return new IDF_Scm_Git($rep, $project);
|
2008-09-02 13:51:57 +00:00
|
|
|
}
|
2008-07-27 15:43:51 +00:00
|
|
|
|
2009-04-25 14:24:40 +00:00
|
|
|
|
2010-09-01 13:13:52 +00:00
|
|
|
public function validateRevision($commit)
|
2009-04-25 14:24:40 +00:00
|
|
|
{
|
2009-11-06 15:12:54 +00:00
|
|
|
$type = $this->testHash($commit);
|
2010-09-01 13:13:52 +00:00
|
|
|
if ('commit' == $type || 'tag' == $type)
|
|
|
|
return IDF_Scm::REVISION_VALID;
|
|
|
|
return IDF_Scm::REVISION_INVALID;
|
2009-04-25 14:24:40 +00:00
|
|
|
}
|
|
|
|
|
2008-07-27 15:43:51 +00:00
|
|
|
/**
|
|
|
|
* Test a given object hash.
|
|
|
|
*
|
|
|
|
* @param string Object hash.
|
2009-11-06 15:12:54 +00:00
|
|
|
* @return mixed false if not valid or 'blob', 'tree', 'commit', 'tag'
|
2008-07-27 15:43:51 +00:00
|
|
|
*/
|
2009-04-25 14:24:40 +00:00
|
|
|
public function testHash($hash)
|
2008-07-27 15:43:51 +00:00
|
|
|
{
|
2009-02-25 13:28:14 +00:00
|
|
|
$cmd = sprintf('GIT_DIR=%s '.Pluf::f('git_path', 'git').' cat-file -t %s',
|
2008-07-27 15:43:51 +00:00
|
|
|
escapeshellarg($this->repo),
|
|
|
|
escapeshellarg($hash));
|
|
|
|
$ret = 0; $out = array();
|
2009-07-02 11:05:43 +00:00
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
|
2010-02-17 20:57:28 +00:00
|
|
|
self::exec('IDF_Scm_Git::testHash', $cmd, $out, $ret);
|
2008-07-27 15:43:51 +00:00
|
|
|
if ($ret != 0) return false;
|
|
|
|
return trim($out[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the tree info.
|
|
|
|
*
|
2010-05-01 22:56:04 +00:00
|
|
|
* @param string Tree hash
|
2008-07-27 15:43:51 +00:00
|
|
|
* @param bool Do we recurse in subtrees (true)
|
2009-04-21 12:13:44 +00:00
|
|
|
* @param string Folder in which we want to get the info ('')
|
2008-07-27 15:43:51 +00:00
|
|
|
* @return array Array of file information.
|
|
|
|
*/
|
2009-04-21 12:13:44 +00:00
|
|
|
public function getTreeInfo($tree, $folder='')
|
2008-07-27 15:43:51 +00:00
|
|
|
{
|
2009-11-06 15:12:54 +00:00
|
|
|
if (!in_array($this->testHash($tree), array('tree', 'commit', 'tag'))) {
|
2008-07-27 15:43:51 +00:00
|
|
|
throw new Exception(sprintf(__('Not a valid tree: %s.'), $tree));
|
|
|
|
}
|
2009-04-21 12:13:44 +00:00
|
|
|
$cmd_tmpl = 'GIT_DIR=%s '.Pluf::f('git_path', 'git').' ls-tree -l %s %s';
|
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '')
|
2010-05-01 22:56:04 +00:00
|
|
|
.sprintf($cmd_tmpl, escapeshellarg($this->repo),
|
2009-04-21 12:13:44 +00:00
|
|
|
escapeshellarg($tree), escapeshellarg($folder));
|
2008-07-27 15:43:51 +00:00
|
|
|
$out = array();
|
|
|
|
$res = array();
|
2010-02-17 20:57:28 +00:00
|
|
|
self::exec('IDF_Scm_Git::getTreeInfo', $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);
|
2010-05-01 22:56:04 +00:00
|
|
|
$res[] = (object) array('perm' => $perm, 'type' => $type,
|
|
|
|
'size' => $size, 'hash' => $hash,
|
2008-07-26 16:42:41 +00:00
|
|
|
'file' => $file);
|
|
|
|
}
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
2009-04-25 14:24:40 +00:00
|
|
|
public function getPathInfo($totest, $commit='HEAD')
|
2008-07-26 16:42:41 +00:00
|
|
|
{
|
2009-02-25 13:28:14 +00:00
|
|
|
$cmd_tmpl = 'GIT_DIR=%s '.Pluf::f('git_path', 'git').' ls-tree -r -t -l %s';
|
2010-05-01 22:56:04 +00:00
|
|
|
$cmd = sprintf($cmd_tmpl,
|
|
|
|
escapeshellarg($this->repo),
|
2008-07-27 15:43:51 +00:00
|
|
|
escapeshellarg($commit));
|
2008-07-26 16:42:41 +00:00
|
|
|
$out = array();
|
2009-07-02 11:05:43 +00:00
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
|
2010-02-17 20:57:28 +00:00
|
|
|
self::exec('IDF_Scm_Git::getPathInfo', $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) {
|
2009-06-19 13:51:31 +00:00
|
|
|
$pathinfo = pathinfo($file);
|
2010-05-01 22:56:04 +00:00
|
|
|
return (object) array('perm' => $perm, 'type' => $type,
|
|
|
|
'size' => $size, 'hash' => $hash,
|
2009-06-08 11:21:03 +00:00
|
|
|
'fullpath' => $file,
|
2009-06-19 13:51:31 +00:00
|
|
|
'file' => $pathinfo['basename']);
|
2008-07-26 16:42:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-04-25 14:24:40 +00:00
|
|
|
public function getFile($def, $cmd_only=false)
|
2008-07-26 16:42:41 +00:00
|
|
|
{
|
2009-04-25 14:24:40 +00:00
|
|
|
$cmd = sprintf(Pluf::f('idf_exec_cmd_prefix', '').
|
|
|
|
'GIT_DIR=%s '.Pluf::f('git_path', 'git').' cat-file blob %s',
|
2010-05-01 22:56:04 +00:00
|
|
|
escapeshellarg($this->repo),
|
2009-04-25 14:24:40 +00:00
|
|
|
escapeshellarg($def->hash));
|
2010-05-01 22:56:04 +00:00
|
|
|
return ($cmd_only)
|
2010-02-17 20:57:28 +00:00
|
|
|
? $cmd : self::shell_exec('IDF_Scm_Git::getFile', $cmd);
|
2008-07-26 16:42:41 +00:00
|
|
|
}
|
|
|
|
|
2008-07-26 23:31:10 +00:00
|
|
|
/**
|
|
|
|
* Get commit details.
|
|
|
|
*
|
2009-04-21 12:13:44 +00:00
|
|
|
* @param string Commit
|
|
|
|
* @param bool Get commit diff (false)
|
|
|
|
* @return array Changes
|
2008-07-26 23:31:10 +00:00
|
|
|
*/
|
2009-04-21 12:13:44 +00:00
|
|
|
public function getCommit($commit, $getdiff=false)
|
2008-07-26 23:31:10 +00:00
|
|
|
{
|
2009-01-17 17:10:55 +00:00
|
|
|
if ($getdiff) {
|
2009-02-25 13:28:14 +00:00
|
|
|
$cmd = sprintf('GIT_DIR=%s '.Pluf::f('git_path', 'git').' show --date=iso --pretty=format:%s %s',
|
2010-05-01 22:56:04 +00:00
|
|
|
escapeshellarg($this->repo),
|
|
|
|
"'".$this->mediumtree_fmt."'",
|
2009-01-17 17:10:55 +00:00
|
|
|
escapeshellarg($commit));
|
|
|
|
} else {
|
2009-02-25 13:28:14 +00:00
|
|
|
$cmd = sprintf('GIT_DIR=%s '.Pluf::f('git_path', 'git').' log -1 --date=iso --pretty=format:%s %s',
|
2010-05-01 22:56:04 +00:00
|
|
|
escapeshellarg($this->repo),
|
|
|
|
"'".$this->mediumtree_fmt."'",
|
2009-01-17 17:10:55 +00:00
|
|
|
escapeshellarg($commit));
|
|
|
|
}
|
2008-07-26 23:31:10 +00:00
|
|
|
$out = array();
|
2009-07-02 11:05:43 +00:00
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
|
2010-02-17 20:57:28 +00:00
|
|
|
self::exec('IDF_Scm_Git::getCommit', $cmd, $out, $ret);
|
2009-06-24 17:33:16 +00:00
|
|
|
if ($ret != 0 or count($out) == 0) {
|
2009-05-26 09:28:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-11-06 15:12:54 +00:00
|
|
|
if ($getdiff) {
|
|
|
|
$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-26 23:31:10 +00:00
|
|
|
}
|
2009-11-06 15:12:54 +00:00
|
|
|
$out = self::parseLog($log);
|
2010-12-08 00:48:26 +00:00
|
|
|
$out[0]->diff = implode("\n", $change);
|
2009-11-06 15:12:54 +00:00
|
|
|
} else {
|
|
|
|
$out = self::parseLog($out);
|
2010-12-08 00:48:26 +00:00
|
|
|
$out[0]->diff = '';
|
2008-07-26 23:31:10 +00:00
|
|
|
}
|
2010-11-17 00:53:17 +00:00
|
|
|
|
2011-03-16 22:03:36 +00:00
|
|
|
$out[0]->branch = implode(', ', $this->inBranches($out[0]->commit, null));
|
2008-07-26 23:31:10 +00:00
|
|
|
return $out[0];
|
|
|
|
}
|
|
|
|
|
2009-01-17 17:46:26 +00:00
|
|
|
/**
|
2009-01-20 15:26:36 +00:00
|
|
|
* Check if a commit is big.
|
2009-01-17 17:46:26 +00:00
|
|
|
*
|
|
|
|
* @param string Commit ('HEAD')
|
2009-01-20 15:26:36 +00:00
|
|
|
* @return bool The commit is big
|
2009-01-17 17:46:26 +00:00
|
|
|
*/
|
2009-01-20 15:26:36 +00:00
|
|
|
public function isCommitLarge($commit='HEAD')
|
2009-01-17 17:46:26 +00:00
|
|
|
{
|
2009-02-25 13:28:14 +00:00
|
|
|
$cmd = sprintf('GIT_DIR=%s '.Pluf::f('git_path', 'git').' log --numstat -1 --pretty=format:%s %s',
|
2010-05-01 22:56:04 +00:00
|
|
|
escapeshellarg($this->repo),
|
|
|
|
"'commit %H%n'",
|
2009-01-17 17:46:26 +00:00
|
|
|
escapeshellarg($commit));
|
|
|
|
$out = array();
|
2009-07-02 11:05:43 +00:00
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
|
2010-02-17 20:57:28 +00:00
|
|
|
self::exec('IDF_Scm_Git::isCommitLarge', $cmd, $out);
|
2009-01-17 17:46:26 +00:00
|
|
|
$affected = count($out) - 2;
|
|
|
|
$added = 0;
|
|
|
|
$removed = 0;
|
|
|
|
$c=0;
|
|
|
|
foreach ($out as $line) {
|
|
|
|
$c++;
|
|
|
|
if ($c < 3) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
list($a, $r, $f) = preg_split("/[\s]+/", $line, 3, PREG_SPLIT_NO_EMPTY);
|
|
|
|
$added+=$a;
|
|
|
|
$removed+=$r;
|
|
|
|
}
|
2009-01-20 15:26:36 +00:00
|
|
|
return ($affected > 100 or ($added + $removed) > 20000);
|
2009-01-17 17:46:26 +00:00
|
|
|
}
|
|
|
|
|
2008-07-26 23:31:10 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
2009-02-25 13:28:14 +00:00
|
|
|
$cmd = sprintf('GIT_DIR=%s '.Pluf::f('git_path', 'git').' log%s --date=iso --pretty=format:\'%s\' %s',
|
2010-05-01 22:56:04 +00:00
|
|
|
escapeshellarg($this->repo), $n, $this->mediumtree_fmt,
|
2008-07-27 15:43:51 +00:00
|
|
|
escapeshellarg($commit));
|
2008-07-26 23:31:10 +00:00
|
|
|
$out = array();
|
2009-07-02 11:05:43 +00:00
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
|
2010-02-17 20:57:28 +00:00
|
|
|
self::exec('IDF_Scm_Git::getChangeLog', $cmd, $out);
|
2009-11-06 15:12:54 +00:00
|
|
|
return self::parseLog($out);
|
2008-07-26 23:31:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the log lines of a --pretty=medium log output.
|
|
|
|
*
|
|
|
|
* @param array Lines.
|
|
|
|
* @return array Change log.
|
|
|
|
*/
|
2009-07-03 08:23:49 +00:00
|
|
|
public static function parseLog($lines)
|
2008-07-26 23:31:10 +00:00
|
|
|
{
|
|
|
|
$res = array();
|
|
|
|
$c = array();
|
2008-12-23 21:29:11 +00:00
|
|
|
$inheads = true;
|
|
|
|
$next_is_title = false;
|
2008-07-26 23:31:10 +00:00
|
|
|
foreach ($lines as $line) {
|
2008-12-23 21:29:11 +00:00
|
|
|
if (preg_match('/^commit (\w{40})$/', $line)) {
|
2008-07-26 23:31:10 +00:00
|
|
|
if (count($c) > 0) {
|
|
|
|
$c['full_message'] = trim($c['full_message']);
|
2010-03-26 19:40:50 +00:00
|
|
|
$c['full_message'] = IDF_Commit::toUTF8($c['full_message']);
|
|
|
|
$c['title'] = IDF_Commit::toUTF8($c['title']);
|
2011-01-20 09:25:41 +00:00
|
|
|
if (isset($c['parents'])) {
|
|
|
|
$c['parents'] = explode(' ', trim($c['parents']));
|
|
|
|
}
|
2008-07-26 23:31:10 +00:00
|
|
|
$res[] = (object) $c;
|
|
|
|
}
|
|
|
|
$c = array();
|
2008-12-23 21:29:11 +00:00
|
|
|
$c['commit'] = trim(substr($line, 7, 40));
|
2008-07-26 23:31:10 +00:00
|
|
|
$c['full_message'] = '';
|
2008-12-23 21:29:11 +00:00
|
|
|
$inheads = true;
|
|
|
|
$next_is_title = false;
|
2008-07-26 23:31:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
2008-12-23 21:29:11 +00:00
|
|
|
if ($next_is_title) {
|
2008-07-26 23:31:10 +00:00
|
|
|
$c['title'] = trim($line);
|
2008-12-23 21:29:11 +00:00
|
|
|
$next_is_title = false;
|
2008-07-26 23:31:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$match = array();
|
2008-12-23 21:29:11 +00:00
|
|
|
if ($inheads and preg_match('/(\S+)\s*:\s*(.*)/', $line, $match)) {
|
2008-07-26 23:31:10 +00:00
|
|
|
$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;
|
|
|
|
}
|
2008-12-23 21:29:11 +00:00
|
|
|
if ($inheads and !$next_is_title and $line == '') {
|
|
|
|
$next_is_title = true;
|
|
|
|
$inheads = false;
|
|
|
|
}
|
|
|
|
if (!$inheads) {
|
2008-07-26 23:31:10 +00:00
|
|
|
$c['full_message'] .= trim($line)."\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2009-05-25 13:19:22 +00:00
|
|
|
$c['full_message'] = !empty($c['full_message']) ? trim($c['full_message']) : '';
|
2010-03-26 19:40:50 +00:00
|
|
|
$c['full_message'] = IDF_Commit::toUTF8($c['full_message']);
|
|
|
|
$c['title'] = IDF_Commit::toUTF8($c['title']);
|
2011-01-20 09:25:41 +00:00
|
|
|
if (isset($c['parents'])) {
|
|
|
|
$c['parents'] = explode(' ', trim($c['parents']));
|
|
|
|
}
|
2008-07-26 23:31:10 +00:00
|
|
|
$res[] = (object) $c;
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2010-10-30 21:52:40 +00:00
|
|
|
public function getArchiveStream($commit, $prefix='repository/')
|
2008-08-02 11:51:42 +00:00
|
|
|
{
|
2010-10-30 21:52:40 +00:00
|
|
|
$cmd = sprintf(Pluf::f('idf_exec_cmd_prefix', '').
|
2009-02-25 13:28:14 +00:00
|
|
|
'GIT_DIR=%s '.Pluf::f('git_path', 'git').' archive --format=zip --prefix=%s %s',
|
2008-08-02 11:51:42 +00:00
|
|
|
escapeshellarg($this->repo),
|
|
|
|
escapeshellarg($prefix),
|
|
|
|
escapeshellarg($commit));
|
2010-10-30 21:52:40 +00:00
|
|
|
return new Pluf_HTTP_Response_CommandPassThru($cmd, 'application/x-zip');
|
2008-08-02 11:51:42 +00:00
|
|
|
}
|
|
|
|
|
2011-03-16 22:50:41 +00:00
|
|
|
/**
|
|
|
|
* @see IDF_Scm::getDiffPathStripLevel()
|
2011-03-17 22:12:27 +00:00
|
|
|
*/
|
2011-03-16 22:50:41 +00:00
|
|
|
public function getDiffPathStripLevel()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-02-27 12:38:22 +00:00
|
|
|
/*
|
|
|
|
* =====================================================
|
|
|
|
* Specific Git Commands
|
|
|
|
* =====================================================
|
|
|
|
*/
|
2009-11-06 15:12:54 +00:00
|
|
|
|
2009-02-27 12:38:22 +00:00
|
|
|
/**
|
|
|
|
* Get submodule details.
|
|
|
|
*
|
|
|
|
* Given a "commit" file in the tree, find the submodule details.
|
|
|
|
*
|
|
|
|
* @param stdClass File description of the module
|
|
|
|
* @param string Current commit
|
|
|
|
* @return stdClass File description
|
|
|
|
*/
|
|
|
|
public function getSubmodule($file, $commit)
|
|
|
|
{
|
|
|
|
$file->type = 'extern';
|
2009-04-21 12:13:44 +00:00
|
|
|
$file->extern = '';
|
2009-04-25 14:24:40 +00:00
|
|
|
$info = $this->getPathInfo('.gitmodules', $commit);
|
2009-02-27 12:38:22 +00:00
|
|
|
if ($info == false) {
|
|
|
|
return $file;
|
|
|
|
}
|
2009-04-25 14:24:40 +00:00
|
|
|
$gitmodules = $this->getFile($info);
|
2009-02-27 12:38:22 +00:00
|
|
|
if (preg_match('#\[submodule\s+\"'.$file->fullpath.'\"\]\s+path\s=\s(\S+)\s+url\s=\s(\S+)#mi', $gitmodules, $matches)) {
|
|
|
|
$file->extern = $matches[2];
|
2009-04-21 12:13:44 +00:00
|
|
|
}
|
2009-02-27 12:38:22 +00:00
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
2009-04-21 12:13:44 +00:00
|
|
|
/**
|
|
|
|
* Foreach file in the tree, find the details.
|
|
|
|
*
|
|
|
|
* @param array Tree information
|
|
|
|
* @return array Updated tree information
|
|
|
|
*/
|
|
|
|
public function getTreeDetails($tree)
|
|
|
|
{
|
|
|
|
$n = count($tree);
|
|
|
|
$details = array();
|
|
|
|
for ($i=0;$i<$n;$i++) {
|
|
|
|
if ($tree[$i]->type == 'blob') {
|
|
|
|
$details[$tree[$i]->hash] = $i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!count($details)) {
|
|
|
|
return $tree;
|
|
|
|
}
|
|
|
|
$res = $this->getCachedBlobInfo($details);
|
|
|
|
$toapp = array();
|
|
|
|
foreach ($details as $blob => $idx) {
|
|
|
|
if (isset($res[$blob])) {
|
|
|
|
$tree[$idx]->date = $res[$blob]->date;
|
|
|
|
$tree[$idx]->log = $res[$blob]->title;
|
|
|
|
$tree[$idx]->author = $res[$blob]->author;
|
|
|
|
} else {
|
|
|
|
$toapp[$blob] = $idx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (count($toapp)) {
|
|
|
|
$res = $this->appendBlobInfoCache($toapp);
|
|
|
|
foreach ($details as $blob => $idx) {
|
|
|
|
if (isset($res[$blob])) {
|
|
|
|
$tree[$idx]->date = $res[$blob]->date;
|
|
|
|
$tree[$idx]->log = $res[$blob]->title;
|
|
|
|
$tree[$idx]->author = $res[$blob]->author;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $tree;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append build info cache.
|
|
|
|
*
|
|
|
|
* The append method tries to get only the necessary details, so
|
|
|
|
* instead of going through all the commits one at a time, it will
|
|
|
|
* try to find a smarter way with regex.
|
|
|
|
*
|
|
|
|
* @see self::buildBlobInfoCache
|
|
|
|
*
|
|
|
|
* @param array The blob for which we need the information
|
|
|
|
* @return array The information
|
|
|
|
*/
|
|
|
|
public function appendBlobInfoCache($blobs)
|
|
|
|
{
|
|
|
|
$rawlog = array();
|
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '')
|
|
|
|
.sprintf('GIT_DIR=%s '.Pluf::f('git_path', 'git').' log --raw --abbrev=40 --pretty=oneline -5000 --skip=%%s',
|
|
|
|
escapeshellarg($this->repo));
|
|
|
|
$skip = 0;
|
|
|
|
$res = array();
|
2010-02-17 20:57:28 +00:00
|
|
|
self::exec('IDF_Scm_Git::appendBlobInfoCache',
|
|
|
|
sprintf($cmd, $skip), $rawlog);
|
2009-04-21 12:13:44 +00:00
|
|
|
while (count($rawlog) and count($blobs)) {
|
|
|
|
$rawlog = implode("\n", array_reverse($rawlog));
|
|
|
|
foreach ($blobs as $blob => $idx) {
|
|
|
|
if (preg_match('/^\:\d{6} \d{6} [0-9a-f]{40} '
|
|
|
|
.$blob.' .*^([0-9a-f]{40})/msU',
|
|
|
|
$rawlog, $matches)) {
|
|
|
|
$fc = $this->getCommit($matches[1]);
|
|
|
|
$res[$blob] = (object) array('hash' => $blob,
|
|
|
|
'date' => $fc->date,
|
|
|
|
'title' => $fc->title,
|
|
|
|
'author' => $fc->author);
|
|
|
|
unset($blobs[$blob]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$rawlog = array();
|
|
|
|
$skip += 5000;
|
|
|
|
if ($skip > 20000) {
|
|
|
|
// We are in the case of the import of a big old
|
|
|
|
// repository, we can store as unknown the commit info
|
|
|
|
// not to try to retrieve them each time.
|
|
|
|
foreach ($blobs as $blob => $idx) {
|
|
|
|
$res[$blob] = (object) array('hash' => $blob,
|
|
|
|
'date' => '0',
|
|
|
|
'title' => '----',
|
|
|
|
'author' => 'Unknown');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2010-02-17 20:57:28 +00:00
|
|
|
self::exec('IDF_Scm_Git::appendBlobInfoCache',
|
|
|
|
sprintf($cmd, $skip), $rawlog);
|
2009-04-21 12:13:44 +00:00
|
|
|
}
|
|
|
|
$this->cacheBlobInfo($res);
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the blob info cache.
|
|
|
|
*
|
2010-05-01 22:56:04 +00:00
|
|
|
* We build the blob info cache 500 commits at a time.
|
2009-04-21 12:13:44 +00:00
|
|
|
*/
|
|
|
|
public function buildBlobInfoCache()
|
|
|
|
{
|
|
|
|
$rawlog = array();
|
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '')
|
|
|
|
.sprintf('GIT_DIR=%s '.Pluf::f('git_path', 'git').' log --raw --abbrev=40 --pretty=oneline -500 --skip=%%s',
|
|
|
|
escapeshellarg($this->repo));
|
|
|
|
$skip = 0;
|
2010-02-17 20:57:28 +00:00
|
|
|
self::exec('IDF_Scm_Git::buildBlobInfoCache',
|
|
|
|
sprintf($cmd, $skip), $rawlog);
|
2009-04-21 12:13:44 +00:00
|
|
|
while (count($rawlog)) {
|
|
|
|
$commit = '';
|
|
|
|
$data = array();
|
|
|
|
foreach ($rawlog as $line) {
|
|
|
|
if (substr($line, 0, 1) != ':') {
|
|
|
|
$commit = $this->getCommit(substr($line, 0, 40));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$blob = substr($line, 56, 40);
|
|
|
|
$data[] = (object) array('hash' => $blob,
|
|
|
|
'date' => $commit->date,
|
|
|
|
'title' => $commit->title,
|
|
|
|
'author' => $commit->author);
|
|
|
|
}
|
|
|
|
$this->cacheBlobInfo($data);
|
|
|
|
$rawlog = array();
|
|
|
|
$skip += 500;
|
2010-02-17 20:57:28 +00:00
|
|
|
self::exec('IDF_Scm_Git::buildBlobInfoCache',
|
|
|
|
sprintf($cmd, $skip), $rawlog);
|
2009-04-21 12:13:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get blob info.
|
|
|
|
*
|
|
|
|
* When we display the tree, we want to know when a given file was
|
|
|
|
* created, who was the author and at which date. This is a very
|
|
|
|
* slow operation for git as we need to go through the full
|
|
|
|
* history, find when then blob was introduced, then grab the
|
|
|
|
* corresponding commit. This is why we need a cache.
|
|
|
|
*
|
|
|
|
* @param array List as keys of blob hashs to get info for
|
|
|
|
* @return array Hash indexed results, when not found not set
|
|
|
|
*/
|
|
|
|
public function getCachedBlobInfo($hashes)
|
2009-04-21 15:45:35 +00:00
|
|
|
{
|
|
|
|
$cache = new IDF_Scm_Cache_Git();
|
|
|
|
$cache->_project = $this->project;
|
|
|
|
return $cache->retrieve(array_keys($hashes));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache blob info.
|
2010-05-01 22:56:04 +00:00
|
|
|
*
|
2009-04-21 15:45:35 +00:00
|
|
|
* Given a series of blob info, cache them.
|
|
|
|
*
|
|
|
|
* @param array Blob info
|
|
|
|
* @return bool Success
|
|
|
|
*/
|
|
|
|
public function cacheBlobInfo($info)
|
|
|
|
{
|
|
|
|
$cache = new IDF_Scm_Cache_Git();
|
|
|
|
$cache->_project = $this->project;
|
|
|
|
return $cache->store($info);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFileCachedBlobInfo($hashes)
|
2009-04-21 12:13:44 +00:00
|
|
|
{
|
|
|
|
$res = array();
|
|
|
|
$cache = Pluf::f('tmp_folder').'/IDF_Scm_Git-'.md5($this->repo).'.cache.db';
|
|
|
|
if (!file_exists($cache)) {
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
$data = file_get_contents($cache);
|
|
|
|
if (false === $data) {
|
|
|
|
return $res;
|
|
|
|
}
|
2009-09-24 18:40:22 +00:00
|
|
|
$data = explode(chr(30), $data);
|
2009-04-21 12:13:44 +00:00
|
|
|
foreach ($data as $rec) {
|
|
|
|
if (isset($hashes[substr($rec, 0, 40)])) {
|
2009-09-24 18:40:22 +00:00
|
|
|
$tmp = explode(chr(31), substr($rec, 40), 3);
|
2010-05-01 22:56:04 +00:00
|
|
|
$res[substr($rec, 0, 40)] =
|
2009-04-21 12:13:44 +00:00
|
|
|
(object) array('hash' => substr($rec, 0, 40),
|
|
|
|
'date' => $tmp[0],
|
|
|
|
'title' => $tmp[2],
|
|
|
|
'author' => $tmp[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-04-21 15:45:35 +00:00
|
|
|
* File cache blob info.
|
2010-05-01 22:56:04 +00:00
|
|
|
*
|
2009-04-21 12:13:44 +00:00
|
|
|
* Given a series of blob info, cache them.
|
|
|
|
*
|
|
|
|
* @param array Blob info
|
|
|
|
* @return bool Success
|
|
|
|
*/
|
2009-04-21 15:45:35 +00:00
|
|
|
public function fileCacheBlobInfo($info)
|
2009-04-21 12:13:44 +00:00
|
|
|
{
|
|
|
|
// Prepare the data
|
|
|
|
$data = array();
|
|
|
|
foreach ($info as $file) {
|
|
|
|
$data[] = $file->hash.$file->date.chr(31).$file->author.chr(31).$file->title;
|
|
|
|
}
|
|
|
|
$data = implode(chr(30), $data).chr(30);
|
|
|
|
$cache = Pluf::f('tmp_folder').'/IDF_Scm_Git-'.md5($this->repo).'.cache.db';
|
2010-05-01 22:56:04 +00:00
|
|
|
$fp = fopen($cache, 'ab');
|
2009-04-21 12:13:44 +00:00
|
|
|
if ($fp) {
|
2010-05-01 22:56:04 +00:00
|
|
|
flock($fp, LOCK_EX);
|
2009-04-21 12:13:44 +00:00
|
|
|
fwrite($fp, $data, strlen($data));
|
|
|
|
fclose($fp); // releases the lock too
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-30 21:52:40 +00:00
|
|
|
}
|