From 39bd200baed00b5b63cc62ce947ef708710ac81c Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Tue, 17 Apr 2012 19:15:54 +0200 Subject: [PATCH] revision display support --- data/templates/display-sidebar-history.htm | 2 +- src/phorkie/File.php | 21 ++++++++++---- src/phorkie/Repository.php | 33 ++++++++++++++++------ src/phorkie/Repository/Post.php | 4 +-- src/phorkie/Tool/PHPlint.php | 2 +- src/phorkie/Tool/Xmllint.php | 2 +- www/.htaccess | 1 + www/raw.php | 2 +- www/revision.php | 18 ++++++++++++ 9 files changed, 65 insertions(+), 20 deletions(-) create mode 100644 www/revision.php diff --git a/data/templates/display-sidebar-history.htm b/data/templates/display-sidebar-history.htm index 7deacd0..fc7cbc5 100644 --- a/data/templates/display-sidebar-history.htm +++ b/data/templates/display-sidebar-history.htm @@ -8,7 +8,7 @@ {% endfor %} {% endspaceless %} - {{commit.hash|slice(0, 6)}} + {{commit.hash|slice(0, 6)}} {{commit.committerName}} {{dh.get(commit.committerTime)}} diff --git a/src/phorkie/File.php b/src/phorkie/File.php index 53925ee..3c6c56d 100644 --- a/src/phorkie/File.php +++ b/src/phorkie/File.php @@ -4,7 +4,7 @@ namespace phorkie; class File { /** - * Full path to the file + * Path to the file, relative to repository work directory * * @var string */ @@ -17,6 +17,11 @@ class File */ public $repo; + /** + * Commit revision this file is at + */ + public $hash; + public function __construct($path, Repository $repo = null) { $this->path = $path; @@ -30,7 +35,7 @@ class File */ public function getFilename() { - return basename($this->path); + return $this->path; } /** @@ -38,9 +43,9 @@ class File * * @return string */ - public function getPath() + public function getFullPath() { - return $this->path; + return $this->repo->workDir . '/' . $this->path; } /** @@ -55,7 +60,13 @@ class File public function getContent() { - return file_get_contents($this->path); + if ($this->repo->hash) { + return $this->repo->getVc()->getCommand('show') + ->addArgument($this->repo->hash . ':' . $this->path) + ->execute(); + } + + return file_get_contents($this->getFullPath()); } public function getRenderedContent(Tool_Result $res = null) diff --git a/src/phorkie/Repository.php b/src/phorkie/Repository.php index aa1ea4c..6c71b47 100644 --- a/src/phorkie/Repository.php +++ b/src/phorkie/Repository.php @@ -49,6 +49,10 @@ class Repository if (!is_numeric($_GET['id'])) { throw new Exception_Input('Paste ID not numeric'); } + if (isset($_GET['rev'])) { + $this->hash = $_GET['rev']; + } + $this->id = (int)$_GET['id']; $this->loadDirs(); $this->loadHash(); @@ -75,6 +79,7 @@ class Repository public function loadHash() { + return; if ($this->hash !== null) { return; } @@ -114,10 +119,20 @@ class Repository */ public function getFiles() { - $files = glob($this->workDir . '/*'); + if ($this->hash === null) { + $hash = 'HEAD'; + } else { + $hash = $this->hash; + } + $output = $this->getVc()->getCommand('ls-tree') + ->setOption('r') + ->setOption('name-only') + ->addArgument($hash) + ->execute(); + $files = explode("\n", trim($output)); $arFiles = array(); - foreach ($files as $path) { - $arFiles[] = new File($path, $this); + foreach ($files as $name) { + $arFiles[] = new File($name, $this); } return $arFiles; } @@ -131,11 +146,11 @@ class Repository if ($name == '') { throw new Exception_Input('Empty file name given'); } - $path = $this->workDir . '/' . $base; - if ($bHasToExist && !is_readable($path)) { + $fullpath = $this->workDir . '/' . $base; + if ($bHasToExist && !is_readable($fullpath)) { throw new Exception_Input('File does not exist'); } - return new File($path, $this); + return new File($base, $this); } public function hasFile($name) @@ -177,12 +192,12 @@ class Repository * Get a link to the repository * * @param string $type Link type. Supported are: - * - "commit" * - "edit" * - "delete" * - "delete-confirm" * - "display" * - "fork" + * - "revision" * @param string $option * * @return string @@ -199,8 +214,8 @@ class Repository return '/' . $this->id . '/delete'; } else if ($type == 'delete-confirm') { return '/' . $this->id . '/delete/confirm'; - } else if ($type == 'commit') { - return '/' . $this->id . '/' . $option; + } else if ($type == 'revision') { + return '/' . $this->id . '/rev/' . $option; } throw new Exception('Unknown link type'); } diff --git a/src/phorkie/Repository/Post.php b/src/phorkie/Repository/Post.php index 045bba1..96e5c11 100644 --- a/src/phorkie/Repository/Post.php +++ b/src/phorkie/Repository/Post.php @@ -88,14 +88,14 @@ class Repository_Post $bChanged = true; } else if ($bUpload) { move_uploaded_file( - $_FILES['files']['tmp_name'][$num]['upload'], $file->getPath() + $_FILES['files']['tmp_name'][$num]['upload'], $file->getFullPath() ); $command = $vc->getCommand('add') ->addArgument($file->getFilename()) ->execute(); $bChanged = true; } else if ($bNew || (isset($arFile['content']) && $file->getContent() != $arFile['content'])) { - file_put_contents($file->getPath(), $arFile['content']); + file_put_contents($file->getFullPath(), $arFile['content']); $command = $vc->getCommand('add') ->addArgument($file->getFilename()) ->execute(); diff --git a/src/phorkie/Tool/PHPlint.php b/src/phorkie/Tool/PHPlint.php index 1aa672d..c1d4f98 100644 --- a/src/phorkie/Tool/PHPlint.php +++ b/src/phorkie/Tool/PHPlint.php @@ -9,7 +9,7 @@ class Tool_PHPlint public function run(File $file) { - $fpath = $file->getPath(); + $fpath = $file->getFullPath(); $fpathlen = strlen($fpath); $res = new Tool_Result(); diff --git a/src/phorkie/Tool/Xmllint.php b/src/phorkie/Tool/Xmllint.php index a724e6c..8488138 100644 --- a/src/phorkie/Tool/Xmllint.php +++ b/src/phorkie/Tool/Xmllint.php @@ -9,7 +9,7 @@ class Tool_Xmllint public function run(File $file) { - $fpath = $file->getPath(); + $fpath = $file->getFullPath(); $fpathlen = strlen($fpath); $res = new Tool_Result(); diff --git a/www/.htaccess b/www/.htaccess index 10833c7..73ba7db 100644 --- a/www/.htaccess +++ b/www/.htaccess @@ -8,6 +8,7 @@ RewriteRule ^([0-9]+)/delete/confirm$ /delete.php?id=$1&confirm=1 RewriteRule ^([0-9]+)/edit$ /edit.php?id=$1 RewriteRule ^([0-9]+)/fork$ /fork.php?id=$1 RewriteRule ^([0-9]+)/raw/(.+)$ /raw.php?id=$1&file=$2 +RewriteRule ^([0-9]+)/rev/(.+)$ /revision.php?id=$1&rev=$2 RewriteRule ^([0-9]+)/tool/([^/]+)/(.+)$ /tool.php?id=$1&tool=$2&file=$3 RewriteRule ^list$ /list.php diff --git a/www/raw.php b/www/raw.php index 69b5f56..28f7c56 100644 --- a/www/raw.php +++ b/www/raw.php @@ -17,5 +17,5 @@ if ($mimetype === null) { $mimetype = 'text/plain'; } header('Content-Type: ' . $mimetype); -readfile($file->path); +readfile($file->getFullPath()); ?> diff --git a/www/revision.php b/www/revision.php new file mode 100644 index 0000000..c8df8a9 --- /dev/null +++ b/www/revision.php @@ -0,0 +1,18 @@ +loadFromRequest(); + +render( + 'display', + array( + 'repo' => $repo, + 'dh' => new \Date_HumanDiff(), + ) +); +?> -- 2.30.2