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 ***** */
|
|
|
|
|
|
|
|
Pluf::loadFunction('Pluf_HTTP_URL_urlForView');
|
|
|
|
Pluf::loadFunction('Pluf_Shortcuts_RenderToResponse');
|
|
|
|
Pluf::loadFunction('Pluf_Shortcuts_GetObjectOr404');
|
|
|
|
Pluf::loadFunction('Pluf_Shortcuts_GetFormForModel');
|
|
|
|
|
|
|
|
/**
|
2009-03-26 11:04:16 +00:00
|
|
|
* View SCM repository.
|
2008-07-26 16:42:41 +00:00
|
|
|
*/
|
|
|
|
class IDF_Views_Source
|
|
|
|
{
|
2009-03-26 11:04:16 +00:00
|
|
|
/**
|
|
|
|
* Extension supported by the syntax highlighter.
|
|
|
|
*/
|
2008-11-15 14:44:47 +00:00
|
|
|
public static $supportedExtenstions = array('c', 'cc', 'cpp', 'cs', 'css',
|
|
|
|
'cyc', 'java', 'bsh', 'csh',
|
|
|
|
'sh', 'cv', 'py', 'perl', 'php',
|
|
|
|
'pl', 'pm', 'rb', 'js', 'html',
|
|
|
|
'html', 'xhtml', 'xml', 'xsl');
|
|
|
|
|
2009-01-22 10:12:41 +00:00
|
|
|
/**
|
|
|
|
* Display help on how to checkout etc.
|
|
|
|
*/
|
|
|
|
public $help_precond = array('IDF_Precondition::accessSource');
|
|
|
|
public function help($request, $match)
|
|
|
|
{
|
|
|
|
$title = sprintf(__('%s Source Help'), (string) $request->project);
|
|
|
|
$scm = IDF_Scm::get($request->project);
|
|
|
|
$scmConf = $request->conf->getVal('scm', 'git');
|
|
|
|
$params = array(
|
|
|
|
'page_title' => $title,
|
|
|
|
'title' => $title,
|
|
|
|
'scm' => $scmConf,
|
|
|
|
);
|
|
|
|
return Pluf_Shortcuts_RenderToResponse('idf/source/'.$scmConf.'/help.html',
|
|
|
|
$params, $request);
|
|
|
|
}
|
|
|
|
|
2008-08-07 13:35:03 +00:00
|
|
|
public $changeLog_precond = array('IDF_Precondition::accessSource');
|
2008-07-26 23:31:10 +00:00
|
|
|
public function changeLog($request, $match)
|
2008-07-26 16:42:41 +00:00
|
|
|
{
|
2008-09-03 07:33:29 +00:00
|
|
|
$title = sprintf(__('%1$s %2$s Change Log'), (string) $request->project,
|
2008-08-29 17:50:10 +00:00
|
|
|
$this->getScmType($request));
|
2008-12-05 10:34:02 +00:00
|
|
|
$scm = IDF_Scm::get($request->project);
|
2008-08-29 17:50:10 +00:00
|
|
|
$branches = $scm->getBranches();
|
2008-07-26 23:31:10 +00:00
|
|
|
$commit = $match[2];
|
2009-04-25 14:24:40 +00:00
|
|
|
if (!$scm->isValidRevision($commit)) {
|
2009-01-25 19:48:02 +00:00
|
|
|
if (count($branches) == 0) {
|
|
|
|
// Redirect to the project source help
|
|
|
|
$url = Pluf_HTTP_URL_urlForView('IDF_Views_Source::help',
|
|
|
|
array($request->project->shortname));
|
|
|
|
return new Pluf_HTTP_Response_Redirect($url);
|
|
|
|
}
|
2008-09-02 15:41:54 +00:00
|
|
|
// Redirect to the first branch
|
|
|
|
$url = Pluf_HTTP_URL_urlForView('IDF_Views_Source::changeLog',
|
|
|
|
array($request->project->shortname,
|
|
|
|
$branches[0]));
|
|
|
|
return new Pluf_HTTP_Response_Redirect($url);
|
|
|
|
}
|
2008-11-14 14:41:51 +00:00
|
|
|
$changes = $scm->getChangeLog($commit, 25);
|
2008-12-05 13:51:51 +00:00
|
|
|
$rchanges = array();
|
2008-11-14 14:41:51 +00:00
|
|
|
// Sync with the database
|
|
|
|
foreach ($changes as $change) {
|
2008-12-05 13:51:51 +00:00
|
|
|
$rchanges[] = IDF_Commit::getOrAdd($change, $request->project);
|
2008-11-14 14:41:51 +00:00
|
|
|
}
|
2008-12-05 13:51:51 +00:00
|
|
|
$rchanges = new Pluf_Template_ContextVars($rchanges);
|
2008-08-29 17:50:10 +00:00
|
|
|
$scmConf = $request->conf->getVal('scm', 'git');
|
2008-11-18 08:15:02 +00:00
|
|
|
return Pluf_Shortcuts_RenderToResponse('idf/source/changelog.html',
|
2008-07-26 23:31:10 +00:00
|
|
|
array(
|
|
|
|
'page_title' => $title,
|
|
|
|
'title' => $title,
|
2008-12-05 13:51:51 +00:00
|
|
|
'changes' => $rchanges,
|
2008-07-26 23:31:10 +00:00
|
|
|
'commit' => $commit,
|
|
|
|
'branches' => $branches,
|
2008-08-29 17:50:10 +00:00
|
|
|
'scm' => $scmConf,
|
2008-07-26 23:31:10 +00:00
|
|
|
),
|
|
|
|
$request);
|
2008-07-26 16:42:41 +00:00
|
|
|
}
|
|
|
|
|
2008-08-07 13:35:03 +00:00
|
|
|
public $treeBase_precond = array('IDF_Precondition::accessSource');
|
2008-07-26 16:42:41 +00:00
|
|
|
public function treeBase($request, $match)
|
|
|
|
{
|
2009-04-21 12:13:44 +00:00
|
|
|
$title = sprintf(__('%1$s %2$s Source Tree'),
|
|
|
|
$request->project, $this->getScmType($request));
|
2008-12-05 10:34:02 +00:00
|
|
|
$scm = IDF_Scm::get($request->project);
|
2009-04-21 12:13:44 +00:00
|
|
|
if (!$scm->isAvailable()) {
|
2009-01-22 10:12:41 +00:00
|
|
|
$url = Pluf_HTTP_URL_urlForView('IDF_Views_Source::help',
|
2009-01-21 18:56:47 +00:00
|
|
|
array($request->project->shortname));
|
|
|
|
return new Pluf_HTTP_Response_Redirect($url);
|
|
|
|
}
|
2009-04-21 12:13:44 +00:00
|
|
|
$commit = $match[2];
|
|
|
|
$cobject = $scm->getCommit($commit);
|
|
|
|
if (!$cobject) {
|
2008-07-27 19:38:49 +00:00
|
|
|
$url = Pluf_HTTP_URL_urlForView('IDF_Views_Source::treeBase',
|
|
|
|
array($request->project->shortname,
|
2009-04-21 12:13:44 +00:00
|
|
|
$scm->getMainBranch()));
|
2008-07-27 19:38:49 +00:00
|
|
|
return new Pluf_HTTP_Response_Redirect($url);
|
|
|
|
}
|
2009-04-21 12:13:44 +00:00
|
|
|
$branches = $scm->getBranches();
|
2009-04-25 14:24:40 +00:00
|
|
|
$in_branches = $scm->inBranches($commit, '');
|
2009-01-18 09:17:39 +00:00
|
|
|
$cache = Pluf_Cache::factory();
|
2009-01-23 14:06:57 +00:00
|
|
|
$key = sprintf('Project:%s::IDF_Views_Source::treeBase:%s::',
|
|
|
|
$request->project->id, $commit);
|
2009-01-18 09:17:39 +00:00
|
|
|
if (null === ($res=$cache->get($key))) {
|
2009-04-21 12:13:44 +00:00
|
|
|
$res = new Pluf_Template_ContextVars($scm->getTree($commit));
|
2009-01-18 09:17:39 +00:00
|
|
|
$cache->set($key, $res);
|
|
|
|
}
|
2009-04-25 14:24:40 +00:00
|
|
|
//$tree_in = in_array($commit, $branches);
|
2008-08-29 17:50:10 +00:00
|
|
|
$scmConf = $request->conf->getVal('scm', 'git');
|
2009-04-25 14:24:40 +00:00
|
|
|
$props = $scm->getProperties($commit);
|
2008-11-18 08:15:02 +00:00
|
|
|
return Pluf_Shortcuts_RenderToResponse('idf/source/'.$scmConf.'/tree.html',
|
2008-07-26 16:42:41 +00:00
|
|
|
array(
|
|
|
|
'page_title' => $title,
|
2008-07-26 18:17:18 +00:00
|
|
|
'title' => $title,
|
2008-07-26 16:42:41 +00:00
|
|
|
'files' => $res,
|
2008-07-26 23:31:10 +00:00
|
|
|
'cobject' => $cobject,
|
2008-07-27 15:43:51 +00:00
|
|
|
'commit' => $commit,
|
2009-04-25 14:24:40 +00:00
|
|
|
'tree_in' => $in_branches,
|
2008-07-26 16:42:41 +00:00
|
|
|
'branches' => $branches,
|
2008-08-29 17:50:10 +00:00
|
|
|
'props' => $props,
|
2008-07-26 16:42:41 +00:00
|
|
|
),
|
|
|
|
$request);
|
|
|
|
}
|
|
|
|
|
2008-08-07 13:35:03 +00:00
|
|
|
public $tree_precond = array('IDF_Precondition::accessSource');
|
2008-07-26 16:42:41 +00:00
|
|
|
public function tree($request, $match)
|
|
|
|
{
|
2009-04-21 12:13:44 +00:00
|
|
|
$title = sprintf(__('%1$s %2$s Source Tree'),
|
|
|
|
$request->project, $this->getScmType($request));
|
2008-12-05 10:34:02 +00:00
|
|
|
$scm = IDF_Scm::get($request->project);
|
2009-04-27 09:39:19 +00:00
|
|
|
$commit = $match[2];
|
|
|
|
$request_file = $match[3];
|
|
|
|
|
2009-04-21 12:13:44 +00:00
|
|
|
if (!$scm->isAvailable()) {
|
|
|
|
$url = Pluf_HTTP_URL_urlForView('IDF_Views_Source::help',
|
|
|
|
array($request->project->shortname));
|
|
|
|
return new Pluf_HTTP_Response_Redirect($url);
|
|
|
|
}
|
2008-08-29 17:50:10 +00:00
|
|
|
$branches = $scm->getBranches();
|
2008-11-23 20:09:44 +00:00
|
|
|
$fburl = Pluf_HTTP_URL_urlForView('IDF_Views_Source::treeBase',
|
|
|
|
array($request->project->shortname,
|
2009-04-21 12:13:44 +00:00
|
|
|
$scm->getMainBranch()));
|
2009-02-16 20:33:19 +00:00
|
|
|
if (substr($request_file, -1) == '/') {
|
|
|
|
$request_file = substr($request_file, 0, -1);
|
2009-02-16 21:18:09 +00:00
|
|
|
$url = Pluf_HTTP_URL_urlForView('IDF_Views_Source::tree',
|
|
|
|
array($match[1], $match[2],
|
|
|
|
$request_file));
|
|
|
|
return new Pluf_HTTP_Response_Redirect($url, 301);
|
2009-02-16 20:33:19 +00:00
|
|
|
}
|
2009-04-25 14:24:40 +00:00
|
|
|
if (!$scm->isValidRevision($commit, $request_file)) {
|
2008-07-27 19:38:49 +00:00
|
|
|
// Redirect to the first branch
|
2008-11-23 20:09:44 +00:00
|
|
|
return new Pluf_HTTP_Response_Redirect($fburl);
|
2008-07-27 19:38:49 +00:00
|
|
|
}
|
2009-04-25 14:24:40 +00:00
|
|
|
$request_file_info = $scm->getPathInfo($request_file, $commit);
|
2008-07-27 19:38:49 +00:00
|
|
|
if (!$request_file_info) {
|
|
|
|
// Redirect to the first branch
|
2008-11-23 20:09:44 +00:00
|
|
|
return new Pluf_HTTP_Response_Redirect($fburl);
|
2008-07-27 19:38:49 +00:00
|
|
|
}
|
2008-07-26 16:42:41 +00:00
|
|
|
if ($request_file_info->type != 'tree') {
|
2009-03-03 09:12:42 +00:00
|
|
|
$info = self::getRequestedFileMimeType($request_file_info,
|
|
|
|
$commit, $scm);
|
2008-11-11 20:21:21 +00:00
|
|
|
if (!self::isText($info)) {
|
2009-04-21 12:13:44 +00:00
|
|
|
|
2009-04-25 14:24:40 +00:00
|
|
|
$rep = new Pluf_HTTP_Response($scm->getFile($request_file_info),
|
2008-11-11 20:21:21 +00:00
|
|
|
$info[0]);
|
|
|
|
$rep->headers['Content-Disposition'] = 'attachment; filename="'.$info[1].'"';
|
2008-11-11 21:32:01 +00:00
|
|
|
return $rep;
|
2008-11-11 20:21:21 +00:00
|
|
|
} else {
|
|
|
|
// We want to display the content of the file as text
|
2008-11-11 21:32:01 +00:00
|
|
|
$extra = array('branches' => $branches,
|
|
|
|
'commit' => $commit,
|
|
|
|
'request_file' => $request_file,
|
|
|
|
'request_file_info' => $request_file_info,
|
|
|
|
'mime' => $info,
|
|
|
|
);
|
|
|
|
return $this->viewFile($request, $match, $extra);
|
2008-11-11 20:21:21 +00:00
|
|
|
}
|
2008-07-26 16:42:41 +00:00
|
|
|
}
|
2009-04-21 12:13:44 +00:00
|
|
|
|
2008-07-27 15:43:51 +00:00
|
|
|
$bc = self::makeBreadCrumb($request->project, $commit, $request_file_info->file);
|
2009-04-21 12:13:44 +00:00
|
|
|
|
2008-07-26 23:31:10 +00:00
|
|
|
$page_title = $bc.' - '.$title;
|
2008-08-29 17:50:10 +00:00
|
|
|
$cobject = $scm->getCommit($commit);
|
2009-04-25 14:24:40 +00:00
|
|
|
$in_branches = $scm->inBranches($commit, $request_file);
|
2008-11-23 20:09:44 +00:00
|
|
|
try {
|
2009-01-18 09:17:39 +00:00
|
|
|
$cache = Pluf_Cache::factory();
|
2009-01-23 14:06:57 +00:00
|
|
|
$key = sprintf('Project:%s::IDF_Views_Source::tree:%s::%s',
|
|
|
|
$request->project->id, $commit, $request_file);
|
2009-01-18 09:17:39 +00:00
|
|
|
if (null === ($res=$cache->get($key))) {
|
2009-04-21 12:13:44 +00:00
|
|
|
$res = new Pluf_Template_ContextVars($scm->getTree($commit, $request_file));
|
2009-01-18 09:17:39 +00:00
|
|
|
$cache->set($key, $res);
|
|
|
|
}
|
2009-04-21 12:13:44 +00:00
|
|
|
|
2008-11-23 20:09:44 +00:00
|
|
|
} catch (Exception $e) {
|
2009-04-21 12:13:44 +00:00
|
|
|
throw $e;
|
2008-11-23 20:09:44 +00:00
|
|
|
}
|
2008-07-26 16:42:41 +00:00
|
|
|
// try to find the previous level if it exists.
|
|
|
|
$prev = split('/', $request_file);
|
|
|
|
$l = array_pop($prev);
|
|
|
|
$previous = substr($request_file, 0, -strlen($l.' '));
|
2008-08-29 17:50:10 +00:00
|
|
|
$scmConf = $request->conf->getVal('scm', 'git');
|
|
|
|
$props = null;
|
|
|
|
if ($scmConf === 'svn') {
|
|
|
|
$props = $scm->getProperties($commit, $request_file);
|
|
|
|
}
|
2008-11-18 08:15:02 +00:00
|
|
|
return Pluf_Shortcuts_RenderToResponse('idf/source/'.$scmConf.'/tree.html',
|
2008-07-26 16:42:41 +00:00
|
|
|
array(
|
|
|
|
'page_title' => $page_title,
|
|
|
|
'title' => $title,
|
|
|
|
'breadcrumb' => $bc,
|
|
|
|
'files' => $res,
|
2008-07-27 15:43:51 +00:00
|
|
|
'commit' => $commit,
|
2008-07-26 23:31:10 +00:00
|
|
|
'cobject' => $cobject,
|
2008-07-26 16:42:41 +00:00
|
|
|
'base' => $request_file_info->file,
|
|
|
|
'prev' => $previous,
|
2009-04-25 14:24:40 +00:00
|
|
|
'tree_in' => $in_branches,
|
2008-07-26 16:42:41 +00:00
|
|
|
'branches' => $branches,
|
2008-08-29 17:50:10 +00:00
|
|
|
'props' => $props,
|
2008-07-26 16:42:41 +00:00
|
|
|
),
|
|
|
|
$request);
|
|
|
|
}
|
|
|
|
|
2008-07-27 15:43:51 +00:00
|
|
|
public static function makeBreadCrumb($project, $commit, $file, $sep='/')
|
2008-07-26 16:42:41 +00:00
|
|
|
{
|
|
|
|
$elts = split('/', $file);
|
|
|
|
$out = array();
|
|
|
|
$stack = '';
|
|
|
|
$i = 0;
|
|
|
|
foreach ($elts as $elt) {
|
|
|
|
$stack .= ($i==0) ? $elt : '/'.$elt;
|
|
|
|
$url = Pluf_HTTP_URL_urlForView('IDF_Views_Source::tree',
|
|
|
|
array($project->shortname,
|
2008-07-27 15:43:51 +00:00
|
|
|
$commit, $stack));
|
2008-07-26 16:42:41 +00:00
|
|
|
$out[] = '<a href="'.$url.'">'.Pluf_esc($elt).'</a>';
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
return '<span class="breadcrumb">'.implode('<span class="sep">'.$sep.'</span>', $out).'</span>';
|
|
|
|
}
|
2008-07-28 20:09:17 +00:00
|
|
|
|
2008-08-07 13:35:03 +00:00
|
|
|
public $commit_precond = array('IDF_Precondition::accessSource');
|
2008-07-28 20:09:17 +00:00
|
|
|
public function commit($request, $match)
|
|
|
|
{
|
2008-12-05 10:34:02 +00:00
|
|
|
$scm = IDF_Scm::get($request->project);
|
2008-07-28 20:09:17 +00:00
|
|
|
$commit = $match[2];
|
2008-08-29 17:50:10 +00:00
|
|
|
$branches = $scm->getBranches();
|
2009-04-25 14:24:40 +00:00
|
|
|
if (!$scm->isValidRevision($commit)) {
|
2008-07-28 20:09:17 +00:00
|
|
|
// Redirect to the first branch
|
|
|
|
$url = Pluf_HTTP_URL_urlForView('IDF_Views_Source::treeBase',
|
|
|
|
array($request->project->shortname,
|
|
|
|
$branches[0]));
|
|
|
|
return new Pluf_HTTP_Response_Redirect($url);
|
|
|
|
}
|
2008-08-06 12:54:01 +00:00
|
|
|
$title = sprintf(__('%s Commit Details'), (string) $request->project);
|
|
|
|
$page_title = sprintf(__('%s Commit Details - %s'), (string) $request->project, $commit);
|
2009-01-20 15:26:36 +00:00
|
|
|
$large = $scm->isCommitLarge($commit);
|
2009-01-17 17:46:26 +00:00
|
|
|
$cobject = $scm->getCommit($commit, !$large);
|
2008-12-05 13:51:51 +00:00
|
|
|
$rcommit = IDF_Commit::getOrAdd($cobject, $request->project);
|
2008-07-31 22:38:29 +00:00
|
|
|
$diff = new IDF_Diff($cobject->changes);
|
|
|
|
$diff->parse();
|
2008-08-29 17:50:10 +00:00
|
|
|
$scmConf = $request->conf->getVal('scm', 'git');
|
2008-11-18 08:15:02 +00:00
|
|
|
return Pluf_Shortcuts_RenderToResponse('idf/source/commit.html',
|
2008-07-28 20:09:17 +00:00
|
|
|
array(
|
|
|
|
'page_title' => $page_title,
|
|
|
|
'title' => $title,
|
2008-07-31 22:38:29 +00:00
|
|
|
'diff' => $diff,
|
2008-07-28 20:09:17 +00:00
|
|
|
'cobject' => $cobject,
|
|
|
|
'commit' => $commit,
|
|
|
|
'branches' => $branches,
|
2008-08-29 17:50:10 +00:00
|
|
|
'scm' => $scmConf,
|
2008-12-05 13:51:51 +00:00
|
|
|
'rcommit' => $rcommit,
|
2009-01-17 17:46:26 +00:00
|
|
|
'large_commit' => $large,
|
2008-07-28 20:09:17 +00:00
|
|
|
),
|
|
|
|
$request);
|
|
|
|
}
|
|
|
|
|
2008-11-25 19:11:09 +00:00
|
|
|
public $downloadDiff_precond = array('IDF_Precondition::accessSource');
|
|
|
|
public function downloadDiff($request, $match)
|
|
|
|
{
|
2008-12-05 10:34:02 +00:00
|
|
|
$scm = IDF_Scm::get($request->project);
|
2008-11-25 19:11:09 +00:00
|
|
|
$commit = $match[2];
|
|
|
|
$branches = $scm->getBranches();
|
2009-04-25 14:24:40 +00:00
|
|
|
if (!$scm->isValidRevision($commit)) {
|
2008-11-25 19:11:09 +00:00
|
|
|
// Redirect to the first branch
|
|
|
|
$url = Pluf_HTTP_URL_urlForView('IDF_Views_Source::treeBase',
|
|
|
|
array($request->project->shortname,
|
|
|
|
$branches[0]));
|
|
|
|
return new Pluf_HTTP_Response_Redirect($url);
|
|
|
|
}
|
2009-01-20 15:33:36 +00:00
|
|
|
$cobject = $scm->getCommit($commit, true);
|
2008-11-25 19:11:09 +00:00
|
|
|
$rep = new Pluf_HTTP_Response($cobject->changes, 'text/plain');
|
|
|
|
$rep->headers['Content-Disposition'] = 'attachment; filename="'.$commit.'.diff"';
|
|
|
|
return $rep;
|
|
|
|
}
|
|
|
|
|
2008-11-11 21:32:01 +00:00
|
|
|
/**
|
|
|
|
* Should only be called through self::tree
|
|
|
|
*/
|
|
|
|
public function viewFile($request, $match, $extra)
|
|
|
|
{
|
|
|
|
$title = sprintf(__('%1$s %2$s Source Tree'), (string) $request->project,
|
|
|
|
$this->getScmType($request));
|
2008-12-05 10:34:02 +00:00
|
|
|
$scm = IDF_Scm::get($request->project);
|
2008-11-11 21:32:01 +00:00
|
|
|
$branches = $extra['branches'];
|
|
|
|
$commit = $extra['commit'];
|
|
|
|
$request_file = $extra['request_file'];
|
|
|
|
$request_file_info = $extra['request_file_info'];
|
|
|
|
$bc = self::makeBreadCrumb($request->project, $commit, $request_file_info->file);
|
|
|
|
$page_title = $bc.' - '.$title;
|
|
|
|
$cobject = $scm->getCommit($commit);
|
|
|
|
$tree_in = in_array($commit, $branches);
|
|
|
|
// try to find the previous level if it exists.
|
|
|
|
$prev = split('/', $request_file);
|
|
|
|
$l = array_pop($prev);
|
|
|
|
$previous = substr($request_file, 0, -strlen($l.' '));
|
|
|
|
$scmConf = $request->conf->getVal('scm', 'git');
|
|
|
|
$props = null;
|
|
|
|
if ($scmConf === 'svn') {
|
|
|
|
$props = $scm->getProperties($commit, $request_file);
|
|
|
|
}
|
2009-04-25 14:24:40 +00:00
|
|
|
$content = self::highLight($extra['mime'], $scm->getFile($request_file_info));
|
2008-11-18 08:15:02 +00:00
|
|
|
return Pluf_Shortcuts_RenderToResponse('idf/source/'.$scmConf.'/file.html',
|
2008-11-11 21:32:01 +00:00
|
|
|
array(
|
|
|
|
'page_title' => $page_title,
|
|
|
|
'title' => $title,
|
|
|
|
'breadcrumb' => $bc,
|
|
|
|
'file' => $content,
|
|
|
|
'commit' => $commit,
|
|
|
|
'cobject' => $cobject,
|
|
|
|
'fullpath' => $request_file,
|
|
|
|
'base' => $request_file_info->file,
|
|
|
|
'prev' => $previous,
|
|
|
|
'tree_in' => $tree_in,
|
|
|
|
'branches' => $branches,
|
|
|
|
'props' => $props,
|
|
|
|
),
|
|
|
|
$request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a given file at a given commit.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public $getFile_precond = array('IDF_Precondition::accessSource');
|
|
|
|
public function getFile($request, $match)
|
|
|
|
{
|
2008-12-05 10:34:02 +00:00
|
|
|
$scm = IDF_Scm::get($request->project);
|
2008-11-11 21:32:01 +00:00
|
|
|
$branches = $scm->getBranches();
|
|
|
|
$commit = $match[2];
|
|
|
|
$request_file = $match[3];
|
2009-04-25 14:24:40 +00:00
|
|
|
if (!$scm->isValidRevision($commit)) {
|
2008-11-11 21:32:01 +00:00
|
|
|
// Redirect to the first branch
|
|
|
|
$url = Pluf_HTTP_URL_urlForView('IDF_Views_Source::treeBase',
|
|
|
|
array($request->project->shortname,
|
|
|
|
$branches[0]));
|
|
|
|
return new Pluf_HTTP_Response_Redirect($url);
|
|
|
|
}
|
2009-04-25 14:24:40 +00:00
|
|
|
$request_file_info = $scm->getPathInfo($request_file, $commit);
|
2008-11-11 21:32:01 +00:00
|
|
|
if (!$request_file_info or $request_file_info->type == 'tree') {
|
|
|
|
// Redirect to the first branch
|
|
|
|
$url = Pluf_HTTP_URL_urlForView('IDF_Views_Source::treeBase',
|
|
|
|
array($request->project->shortname,
|
|
|
|
$branches[0]));
|
|
|
|
return new Pluf_HTTP_Response_Redirect($url);
|
|
|
|
}
|
2009-03-03 09:12:42 +00:00
|
|
|
$info = self::getRequestedFileMimeType($request_file_info,
|
|
|
|
$commit, $scm);
|
2009-04-25 14:24:40 +00:00
|
|
|
$rep = new Pluf_HTTP_Response($scm->getFile($request_file_info),
|
2008-11-11 21:32:01 +00:00
|
|
|
$info[0]);
|
|
|
|
$rep->headers['Content-Disposition'] = 'attachment; filename="'.$info[1].'"';
|
|
|
|
return $rep;
|
|
|
|
}
|
|
|
|
|
2008-08-02 11:51:42 +00:00
|
|
|
/**
|
|
|
|
* Get a zip archive of the current commit.
|
|
|
|
*
|
|
|
|
*/
|
2008-08-07 13:35:03 +00:00
|
|
|
public $download_precond = array('IDF_Precondition::accessSource');
|
2008-08-02 11:51:42 +00:00
|
|
|
public function download($request, $match)
|
|
|
|
{
|
|
|
|
$commit = trim($match[2]);
|
2008-12-05 10:34:02 +00:00
|
|
|
$scm = IDF_Scm::get($request->project);
|
2008-08-29 17:50:10 +00:00
|
|
|
$branches = $scm->getBranches();
|
2009-04-25 14:24:40 +00:00
|
|
|
if (!$scm->isValidRevision($commit)) {
|
2008-08-02 11:51:42 +00:00
|
|
|
// Redirect to the first branch
|
|
|
|
$url = Pluf_HTTP_URL_urlForView('IDF_Views_Source::treeBase',
|
|
|
|
array($request->project->shortname,
|
|
|
|
$branches[0]));
|
|
|
|
return new Pluf_HTTP_Response_Redirect($url);
|
|
|
|
}
|
|
|
|
$base = $request->project->shortname.'-'.$commit;
|
2008-08-29 17:50:10 +00:00
|
|
|
$cmd = $scm->getArchiveCommand($commit, $base.'/');
|
2008-08-02 11:51:42 +00:00
|
|
|
$rep = new Pluf_HTTP_Response_CommandPassThru($cmd, 'application/x-zip');
|
|
|
|
$rep->headers['Content-Transfer-Encoding'] = 'binary';
|
|
|
|
$rep->headers['Content-Disposition'] = 'attachment; filename="'.$base.'.zip"';
|
|
|
|
return $rep;
|
|
|
|
}
|
2008-08-05 10:44:56 +00:00
|
|
|
|
2009-03-03 09:12:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the mime type of a requested file.
|
|
|
|
*
|
|
|
|
* @param stdClass Request file info
|
|
|
|
* @param string Commit at which we want the file
|
|
|
|
* @param IDF_Scm SCM object
|
|
|
|
* @param array Mime type found or 'application/octet-stream', basename, extension
|
|
|
|
*/
|
|
|
|
public static function getRequestedFileMimeType($file_info, $commit, $scm)
|
|
|
|
{
|
|
|
|
$mime = self::getMimeType($file_info->file);
|
|
|
|
if ('application/octet-stream' != $mime[0]) {
|
|
|
|
return $mime;
|
|
|
|
}
|
|
|
|
return self::getMimeTypeFromContent($file_info->file,
|
2009-04-25 14:24:40 +00:00
|
|
|
$scm->getFile($file_info));
|
2009-03-03 09:12:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the mime type of a file using the fileinfo class.
|
|
|
|
*
|
|
|
|
* @param string Filename/Filepath
|
|
|
|
* @param string File content
|
|
|
|
* @return array Mime type found or 'application/octet-stream', basename, extension
|
|
|
|
*/
|
2009-04-25 14:24:40 +00:00
|
|
|
public static function getMimeTypeFromContent($file, &$filedata)
|
2009-03-03 09:12:42 +00:00
|
|
|
{
|
|
|
|
$info = pathinfo($file);
|
|
|
|
$res = array('application/octet-stream',
|
|
|
|
$info['basename'],
|
|
|
|
isset($info['extension']) ? $info['extension'] : 'bin');
|
|
|
|
if (function_exists('finfo_open')) {
|
|
|
|
$finfo = finfo_open(FILEINFO_MIME);
|
|
|
|
$mime = finfo_buffer($finfo, $filedata);
|
|
|
|
finfo_close($finfo);
|
|
|
|
if ($mime) {
|
|
|
|
$res[0] = $mime;
|
|
|
|
}
|
|
|
|
if (!isset($info['extension']) && $mime) {
|
|
|
|
$res[2] = (0 === strpos($mime, 'text/')) ? 'txt' : 'bin';
|
|
|
|
} elseif (!isset($info['extension'])) {
|
|
|
|
$res[2] = 'bin';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-05 10:44:56 +00:00
|
|
|
/**
|
|
|
|
* Find the mime type of a file.
|
|
|
|
*
|
|
|
|
* Use /etc/mime.types to find the type.
|
|
|
|
*
|
|
|
|
* @param string Filename/Filepath
|
2008-11-11 20:21:21 +00:00
|
|
|
* @param array Mime type found or 'application/octet-stream', basename, extension
|
2008-08-05 10:44:56 +00:00
|
|
|
*/
|
2008-11-26 10:12:25 +00:00
|
|
|
public static function getMimeType($file)
|
2008-08-05 10:44:56 +00:00
|
|
|
{
|
2008-11-26 10:12:25 +00:00
|
|
|
$src= Pluf::f('idf_mimetypes_db', '/etc/mime.types');
|
2008-08-05 10:44:56 +00:00
|
|
|
$mimes = preg_split("/\015\012|\015|\012/", file_get_contents($src));
|
|
|
|
$info = pathinfo($file);
|
|
|
|
if (isset($info['extension'])) {
|
|
|
|
foreach ($mimes as $mime) {
|
|
|
|
if ('#' != substr($mime, 0, 1)) {
|
|
|
|
$elts = preg_split('/ |\t/', $mime, -1, PREG_SPLIT_NO_EMPTY);
|
|
|
|
if (in_array($info['extension'], $elts)) {
|
2008-11-11 20:21:21 +00:00
|
|
|
return array($elts[0], $info['basename'], $info['extension']);
|
2008-08-05 10:44:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// we consider that if no extension and base name is all
|
|
|
|
// uppercase, then we have a text file.
|
|
|
|
if ($info['basename'] == strtoupper($info['basename'])) {
|
2008-11-11 20:21:21 +00:00
|
|
|
return array('text/plain', $info['basename'], 'txt');
|
2008-08-05 10:44:56 +00:00
|
|
|
}
|
2008-11-11 20:21:21 +00:00
|
|
|
$info['extension'] = 'bin';
|
|
|
|
}
|
|
|
|
return array('application/octet-stream', $info['basename'], $info['extension']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find if a given mime type is a text file.
|
|
|
|
* This uses the output of the self::getMimeType function.
|
|
|
|
*
|
|
|
|
* @param array (Mime type, file name, extension)
|
|
|
|
* @return bool Is text
|
|
|
|
*/
|
|
|
|
public static function isText($fileinfo)
|
|
|
|
{
|
|
|
|
if (0 === strpos($fileinfo[0], 'text/')) {
|
|
|
|
return true;
|
2008-08-05 10:44:56 +00:00
|
|
|
}
|
2009-04-07 09:38:24 +00:00
|
|
|
$ext = 'mdtext php-dist h gitignore diff patch'
|
2008-12-17 14:08:51 +00:00
|
|
|
.Pluf::f('idf_extra_text_ext', '');
|
2009-04-07 09:38:24 +00:00
|
|
|
$ext = array_merge(self::$supportedExtenstions, explode(' ' , $ext));
|
|
|
|
return (in_array($fileinfo[2], $ext));
|
2008-08-05 10:44:56 +00:00
|
|
|
}
|
2008-08-29 17:50:10 +00:00
|
|
|
|
2008-11-11 21:32:01 +00:00
|
|
|
public static function highLight($fileinfo, $content)
|
|
|
|
{
|
2008-11-15 20:23:29 +00:00
|
|
|
$pretty = '';
|
2009-04-07 09:38:24 +00:00
|
|
|
if (self::isSupportedExtension($fileinfo[2])) {
|
2008-11-15 20:23:29 +00:00
|
|
|
$pretty = ' prettyprint';
|
2008-11-15 14:44:47 +00:00
|
|
|
}
|
2008-11-11 21:32:01 +00:00
|
|
|
$table = array();
|
|
|
|
$i = 1;
|
|
|
|
foreach (preg_split("/\015\012|\015|\012/", $content) as $line) {
|
|
|
|
$table[] = '<tr class="c-line"><td class="code-lc" id="L'.$i.'"><a href="#L'.$i.'">'.$i.'</a></td>'
|
2008-11-15 20:23:29 +00:00
|
|
|
.'<td class="code mono'.$pretty.'">'.IDF_Diff::padLine(Pluf_esc($line)).'</td></tr>';
|
2008-11-11 21:32:01 +00:00
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
return Pluf_Template::markSafe(implode("\n", $table));
|
|
|
|
}
|
|
|
|
|
2008-11-15 14:44:47 +00:00
|
|
|
/**
|
2009-03-26 11:04:16 +00:00
|
|
|
* Test if an extension is supported by the syntax highlighter.
|
|
|
|
*
|
|
|
|
* @param string The extension to test
|
|
|
|
* @return bool
|
2008-11-15 14:44:47 +00:00
|
|
|
*/
|
|
|
|
public static function isSupportedExtension($extension)
|
|
|
|
{
|
2009-04-07 09:38:24 +00:00
|
|
|
return in_array($extension, self::$supportedExtenstions);
|
2008-11-15 14:44:47 +00:00
|
|
|
}
|
|
|
|
|
2008-08-29 17:50:10 +00:00
|
|
|
/**
|
|
|
|
* Get the scm type for page title
|
|
|
|
*
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
private function getScmType($request)
|
|
|
|
{
|
2008-08-29 17:59:53 +00:00
|
|
|
return mb_convert_case($request->conf->getVal('scm', 'git'),
|
|
|
|
MB_CASE_TITLE, 'UTF-8');
|
2008-08-29 17:50:10 +00:00
|
|
|
}
|
2008-07-26 16:42:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function IDF_Views_Source_PrettySize($size)
|
|
|
|
{
|
2008-11-10 21:47:59 +00:00
|
|
|
return Pluf_Template::markSafe(str_replace(' ', ' ',
|
|
|
|
Pluf_Utils::prettySize($size)));
|
2008-07-26 16:42:41 +00:00
|
|
|
}
|
|
|
|
|
2008-11-30 09:52:06 +00:00
|
|
|
function IDF_Views_Source_PrettySizeSimple($size)
|
|
|
|
{
|
|
|
|
return Pluf_Utils::prettySize($size);
|
|
|
|
}
|
|
|
|
|