revision display support
authorChristian Weiske <cweiske@cweiske.de>
Tue, 17 Apr 2012 17:15:54 +0000 (19:15 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Tue, 17 Apr 2012 17:15:54 +0000 (19:15 +0200)
data/templates/display-sidebar-history.htm
src/phorkie/File.php
src/phorkie/Repository.php
src/phorkie/Repository/Post.php
src/phorkie/Tool/PHPlint.php
src/phorkie/Tool/Xmllint.php
www/.htaccess
www/raw.php
www/revision.php [new file with mode: 0644]

index 7deacd0d038b7646a2ae2ca3eff8bd9018dc955b..fc7cbc517cf7b52a19bf17914e76af70006a82a0 100644 (file)
@@ -8,7 +8,7 @@
   <img src="/phorkie/dot-{{dot}}.png" alt="" style="padding-left:1px" width="4" height="4"/>
   {% endfor %}
   {% endspaceless %}
-  <a class="hash" href="{{repo.getLink('commit', commit.hash)}}">{{commit.hash|slice(0, 6)}}</a>
+  <a class="hash" href="{{repo.getLink('revision', commit.hash)}}">{{commit.hash|slice(0, 6)}}</a>
   <img src="{{commit.getIconUrl}}" alt="{{commit.committerName}}" title="{{commit.committerName}}" width="20"/>
   <span title="{{commit.committerTime|date('c')}}">{{dh.get(commit.committerTime)}}</span>
  </li>
index 53925eeee131618b8cbb4545a2c54f24d53a9c5f..3c6c56d7e432912d00c47db9546fd7aed9cc62ee 100644 (file)
@@ -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)
index aa1ea4c8b8551f12dee39ddfae14bfcc6c5b6d20..6c71b4733006130fa06b915f7ddcec75001c3887 100644 (file)
@@ -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');
     }
index 045bba10c3399b7024ce254a7f69ebb0fc70c26b..96e5c1135153203a43eef3934024eb585a2f226e 100644 (file)
@@ -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();
index 1aa672d705f6ea54d395e2bfd9dbdedbfe76b0df..c1d4f98ab3a8542f73e5229f52dad3e25a2fdf17 100644 (file)
@@ -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();
index a724e6cb69762cc5014d187e816a5d196eaaad2f..84881384c56b932bf5146578c38d6205c5828b51 100644 (file)
@@ -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();
index 10833c70f177d18b150f258572e6e15963680881..73ba7dbc38a0372ff04b706c96e9dd3329dea724 100644 (file)
@@ -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
index 69b5f569719baa4630d872cc9a21bd5975df2f10..28f7c56ee6ca3475a68507e3de2e27ec422a48d7 100644 (file)
@@ -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 (file)
index 0000000..c8df8a9
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+namespace phorkie;
+/**
+ * Display paste contents
+ */
+require_once 'www-header.php';
+
+$repo = new Repository();
+$repo->loadFromRequest();
+
+render(
+    'display',
+    array(
+        'repo' => $repo,
+        'dh'   => new \Date_HumanDiff(),
+    )
+);
+?>