move forking code to separate class
authorChristian Weiske <cweiske@cweiske.de>
Wed, 19 Sep 2012 21:52:07 +0000 (23:52 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Wed, 19 Sep 2012 21:52:07 +0000 (23:52 +0200)
src/phorkie/Forker.php [new file with mode: 0644]
www/fork.php

diff --git a/src/phorkie/Forker.php b/src/phorkie/Forker.php
new file mode 100644 (file)
index 0000000..3425a72
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+namespace phorkie;
+
+class Forker
+{
+    public function forkLocal($repo)
+    {
+        $new = $this->fork($repo->gitDir);
+        \copy($repo->gitDir . '/description', $new->gitDir . '/description');
+        return $new;
+    }
+
+    public function forkRemote($url)
+    {
+        $new = $this->fork($url);
+        file_put_contents(
+            $new->gitDir . '/description',
+            'Fork of ' . $url
+        );
+        return $new;
+    }
+
+
+    protected function fork($pathOrUrl)
+    {
+        $rs = new Repositories();
+        $new = $rs->createNew();
+        $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($pathOrUrl)
+            ->addArgument($new->workDir)
+            ->execute();
+        foreach (\glob($new->gitDir . '/hooks/*') as $hookfile) {
+            \unlink($hookfile);
+        }
+        return $new;
+    }
+}
+
+?>
index 6c96a6acb3b85d2918bcf12c10a353c1ddc53b41..d8a24a2ad91551edcf46ee981b58bbcf2440533e 100644 (file)
@@ -13,21 +13,8 @@ if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
 $repo = new Repository();
 $repo->loadFromRequest();
 
 $repo = new Repository();
 $repo->loadFromRequest();
 
-$rs = new Repositories();
-$new = $rs->createNew();
-$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->gitDir . '/description', $new->gitDir . '/description');
-foreach (\glob($new->gitDir . '/hooks/*') as $hookfile) {
-    \unlink($hookfile);
-}
+$forker = new Forker();
+$new    = $forker->forkLocal($repo);
 
 //FIXME: where to put fork source link?
 redirect($new->getLink('display'));
 
 //FIXME: where to put fork source link?
 redirect($new->getLink('display'));