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