do not try to delete if file does not exist
[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
32         //VersionControl_Git wants an existing dir, git clone not
33         \rmdir($new->gitDir);
34
35         $vc->getCommand('clone')
36             //this should be setOption, but it fails with a = between name and value
37             ->addArgument('--separate-git-dir')
38             ->addArgument(
39                 $GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $new->id . '.git'
40             )
41             ->addArgument($pathOrUrl)
42             ->addArgument($new->workDir)
43             ->execute();
44         foreach (\glob($new->gitDir . '/hooks/*') as $hookfile) {
45             \unlink($hookfile);
46         }
47
48         return $new;
49     }
50
51     protected function index($repo)
52     {
53         $db = new Database();
54         $db->getIndexer()->addRepo($repo);
55     }
56 }
57
58 ?>