c75cb393a1dc6dea2e524d3b4758454215bf765b
[phorkie.git] / src / phorkie / Forker.php
1 <?php
2 namespace phorkie;
3
4 class Forker
5 {
6     public function forkLocal($repo)
7     {
8         $new = $this->fork($repo->gitDir);
9         \copy($repo->gitDir . '/description', $new->gitDir . '/description');
10         $this->index($new);
11         return $new;
12     }
13
14     public function forkRemote($cloneUrl, $originalUrl)
15     {
16         $new = $this->fork($cloneUrl);
17         file_put_contents(
18             $new->gitDir . '/description',
19             'Fork of ' . $originalUrl
20         );
21         $this->index($new);
22         return $new;
23     }
24
25
26     protected function fork($pathOrUrl)
27     {
28         $rs = new Repositories();
29         $new = $rs->createNew();
30         $vc = $new->getVc();
31         \rmdir($new->gitDir);//VersionControl_Git wants an existing dir, git clone not
32         $vc->getCommand('clone')
33             //this should be setOption, but it fails with a = between name and value
34             ->addArgument('--separate-git-dir')
35             ->addArgument($GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $new->id . '.git')
36             ->addArgument($pathOrUrl)
37             ->addArgument($new->workDir)
38             ->execute();
39         foreach (\glob($new->gitDir . '/hooks/*') as $hookfile) {
40             \unlink($hookfile);
41         }
42
43         return $new;
44     }
45
46     protected function index($repo)
47     {
48         $db = new Database();
49         $db->getIndexer()->addRepo($repo);
50     }
51 }
52
53 ?>