separate git and work directories - gives nicer public git clone urls
authorChristian Weiske <cweiske@cweiske.de>
Wed, 11 Apr 2012 18:22:33 +0000 (20:22 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Wed, 11 Apr 2012 18:22:33 +0000 (20:22 +0200)
data/config.default.php
data/config.php.dist
src/phorkie/Repositories.php
src/phorkie/Repository.php
src/phorkie/Repository/Post.php
www/fork.php

index 7c9969e41912a254905d8daa2a26af5c77d36126..cec379989f11cf1395a11570259e03be1e43467c 100644 (file)
@@ -1,8 +1,9 @@
 <?php
 $GLOBALS['phorkie']['cfg'] = array(
-    'repos' => __DIR__ . '/../repos/',
-    'tpl'   => __DIR__ . '/templates/',
-    'css'   => 'http://twitter.github.com/bootstrap/assets/css/bootstrap.css',
+    'gitdir'  => __DIR__ . '/../repos/git/',
+    'workdir' => __DIR__ . '/../repos/work/',
+    'tpl'     => __DIR__ . '/templates/',
+    'css'     => 'http://twitter.github.com/bootstrap/assets/css/bootstrap.css',
 );
 /**
  * Array of supported file types / languages.
index 8ef80e9ddb06bea77d586ecf0b6585caad55182a..0d60a10a18805fd9a5609c00017d06ac5a0a427f 100644 (file)
@@ -1,5 +1,6 @@
 <?php
-//$GLOBALS['phorkie']['cfg']['repos'] = '/var/cache/git/paste/';
+//$GLOBALS['phorkie']['cfg']['gitdir']  = '/var/cache/git/paste/git/';
+//$GLOBALS['phorkie']['cfg']['workdir'] = '/var/cache/git/paste/work/';
 //$GLOBALS['phorkie']['cfg']['git']['public'] = 'git://bogo/git/paste/';
 //$GLOBALS['phorkie']['cfg']['git']['private'] = 'ssh://git@bogo:paste/';
 ?>
index ef59dd70bf3682339980000527790a234eb3cc73..dc3387bcc1faad481c6061e67c7b7a90025bbb64 100644 (file)
@@ -5,7 +5,8 @@ class Repositories
 {
     public function __construct()
     {
-        $this->reposDir = $GLOBALS['phorkie']['cfg']['repos'];
+        $this->workDir = $GLOBALS['phorkie']['cfg']['workdir'];
+        $this->gitDir  = $GLOBALS['phorkie']['cfg']['gitdir'];
     }
 
     /**
@@ -13,17 +14,21 @@ class Repositories
      */
     public function createNew()
     {
-        chdir($this->reposDir);
-        $dirs = glob('*', GLOB_ONLYDIR);
+        chdir($this->gitDir);
+        $dirs = glob('*.git', GLOB_ONLYDIR);
+        array_walk($dirs, function ($dir) { return substr($dir, 0, -4); });
         sort($dirs, SORT_NUMERIC);
         $n = end($dirs) + 1;
-        unset($dirs);
 
-        $dir = $this->reposDir . '/' . $n . '/'; 
+        chdir($this->workDir);
+        $dir = $this->workDir . '/' . $n . '/';
         mkdir($dir, 0777);//FIXME
         $r = new Repository();
         $r->id = $n;
-        $r->repoDir = $dir;
+        $r->workDir = $dir;
+        $r->gitDir = $this->gitDir . '/' . $n . '.git/';
+        mkdir($r->gitDir, 0777);//FIXME
+
         return $r;
     }
 
@@ -37,15 +42,15 @@ class Repositories
      */
     public function getList($page = 0, $perPage = 10)
     {
-        chdir($this->reposDir);
-        $dirs = glob('*', GLOB_ONLYDIR);
+        chdir($this->gitDir);
+        $dirs = glob('*.git', GLOB_ONLYDIR);
         sort($dirs, SORT_NUMERIC);
 
         $some = array_slice($dirs, $page * $perPage, $perPage);
         $repos = array();
         foreach ($some as $oneDir) {
             $r = new Repository();
-            $r->loadById($oneDir);
+            $r->loadById(substr($oneDir, 0, -4));
             $repos[] = $r;
         }
         return $repos;
index 6dec015538b788e5b556fbbef66a45bf2302d76c..0117ec2c012b972c7b46ea745767211fbfabb8e4 100644 (file)
@@ -12,11 +12,18 @@ class Repository
     public $id;
 
     /**
-     * Full path to the git repository
+     * Full path to the .git repository
      *
      * @var string
      */
-    public $repoDir;
+    public $gitDir;
+
+    /**
+     * Full path to the work tree directory
+     *
+     * @var string
+     */
+    public $workDir;
 
     /**
      * Load Repository data from GET-Request
@@ -34,12 +41,26 @@ class Repository
             throw new Exception_Input('Paste ID not numeric');
         }
         $this->id = (int)$_GET['id'];
+        $this->loadDirs();
+    }
 
-        $repoDir = $GLOBALS['phorkie']['cfg']['repos'] . '/' . $this->id;
-        if (!is_dir($repoDir)) {
-            throw new Exception_NotFound('Paste not found');
+    protected function loadDirs()
+    {
+        $gitDir = $GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $this->id . '.git';
+        if (!is_dir($gitDir)) {
+            throw new Exception_NotFound(
+                sprintf('Paste %d .git dir not found', $this->id)
+            );
+        }
+        $this->gitDir = $gitDir;
+
+        $workDir = $GLOBALS['phorkie']['cfg']['workdir'] . '/' . $this->id;
+        if (!is_dir($workDir)) {
+            throw new Exception_NotFound(
+                sprintf('Paste %d work dir not found', $this->id)
+            );
         }
-        $this->repoDir = $repoDir;
+        $this->workDir = $workDir;
     }
 
     public function loadById($id)
@@ -48,17 +69,12 @@ class Repository
             throw new Exception_Input('Paste ID not numeric');
         }
         $this->id = (int)$id;
-
-        $repoDir = $GLOBALS['phorkie']['cfg']['repos'] . '/' . $this->id;
-        if (!is_dir($repoDir)) {
-            throw new Exception_NotFound('Paste not found');
-        }
-        $this->repoDir = $repoDir;
+        $this->loadDirs();
     }
 
     public function getVc()
     {
-        return new \VersionControl_Git($this->repoDir);
+        return new \VersionControl_Git($this->gitDir);
     }
 
     /**
@@ -68,7 +84,7 @@ class Repository
      */
     public function getFiles()
     {
-        $files = glob($this->repoDir . '/*');
+        $files = glob($this->workDir . '/*');
         $arFiles = array();
         foreach ($files as $path) {
             $arFiles[] = new File($path, $this);
@@ -85,7 +101,7 @@ class Repository
         if ($name == '') {
             throw new Exception_Input('Empty file name given');
         }
-        $path = $this->repoDir . '/' . $base;
+        $path = $this->workDir . '/' . $base;
         if ($bHasToExist && !is_readable($path)) {
             throw new Exception_Input('File does not exist');
         }
@@ -110,20 +126,21 @@ class Repository
      */
     public function delete()
     {
-        return Tools::recursiveDelete($this->repoDir);
+        return Tools::recursiveDelete($this->workDir)
+            && Tools::recursiveDelete($this->gitDir);
     }
 
     public function getDescription()
     {
-        if (!is_readable($this->repoDir . '/.git/description')) {
+        if (!is_readable($this->gitDir . '/description')) {
             return null;
         }
-        return file_get_contents($this->repoDir . '/.git/description');
+        return file_get_contents($this->gitDir . '/description');
     }
 
     public function setDescription($description)
     {
-        file_put_contents($this->repoDir . '/.git/description', $description);
+        file_put_contents($this->gitDir . '/description', $description);
     }
 
     /**
index 8cd9323ca1625f082f2dd2e55eb464a22596b4d2..6ff8df60310e7428acc3c3d48260b03937a7e43a 100644 (file)
@@ -114,13 +114,19 @@ class Repository_Post
         $rs = new Repositories();
         $repo = $rs->createNew();
         $vc = $repo->getVc();
-        $vc->initRepository();
-
-        foreach (glob($repo->repoDir . '/.git/hooks/*') as $hookfile) {
+        //$vc->initRepository();
+        $vc->getCommand('init')
+            //this should be setOption, but it fails with a = between name and value
+            ->addArgument('--separate-git-dir')
+            ->addArgument($GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $repo->id)
+            ->addArgument($repo->workDir)
+            ->execute();
+
+        foreach (glob($repo->gitDir . '/hooks/*') as $hookfile) {
             unlink($hookfile);
         }
 
-        touch($repo->repoDir . '/.git/git-daemon-export-ok');
+        touch($repo->gitDir . '/git-daemon-export-ok');
 
         return $repo;
     }
@@ -130,7 +136,7 @@ class Repository_Post
         $num = -1;
         do {
             ++$num;
-            $files = glob($this->repo->repoDir . '/' . $prefix . $num . '.*');
+            $files = glob($this->repo->workDir . '/' . $prefix . $num . '.*');
         } while (count($files));
 
         return $prefix . $num;
index cc98df0cfd80ae2ca7634e38aea43fa6471a8b2d..799763273667ec8388b1f94e70ab3688c9af60f9 100644 (file)
@@ -14,13 +14,18 @@ $repo->loadFromRequest();
 
 $rs = new Repositories();
 $new = $rs->createNew();
-$new->getVc()->getCommand('clone')
-    ->addArgument($repo->repoDir)
-    ->addArgument($new->repoDir)
+$vc = $new->getVc();
+\rmdir($new->gitDir);//VersionControl_Git wants an existing dir, git clone not
+$vc->getCommand('clone')
+    //this should be setOption, but it fails with a = between name and value
+    ->addArgument('--separate-git-dir')
+    ->addArgument($GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $new->id . '.git')
+    ->addArgument($repo->gitDir)
+    ->addArgument($new->workDir)
     ->execute();
-\copy($repo->repoDir . '/.git/description', $new->repoDir . '/.git/description');
-foreach (glob($new->repoDir . '/.git/hooks/*') as $hookfile) {
-    unlink($hookfile);
+\copy($repo->gitDir . '/description', $new->gitDir . '/description');
+foreach (\glob($new->gitDir . '/hooks/*') as $hookfile) {
+    \unlink($hookfile);
 }
 
 //FIXME: where to put fork source link?