419db1edd3bff9c00092c63e10ee75bb5892c764
[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
12         $not = new Notificator();
13         $not->create($new);
14
15         return $new;
16     }
17
18     public function forkRemote($cloneUrl, $originalUrl, $title = null)
19     {
20         if ($title === null) {
21             $title = 'Fork of ' . $originalUrl;
22         }
23         $new = $this->fork($cloneUrl);
24         file_put_contents($new->gitDir . '/description', $title);
25         $this->index($new);
26
27         $not = new Notificator();
28         $not->create($new);
29
30         return $new;
31     }
32
33
34     protected function fork($pathOrUrl)
35     {
36         $rs = new Repositories();
37         $new = $rs->createNew();
38         $vc = $new->getVc();
39
40         //VersionControl_Git wants an existing dir, git clone not
41         \rmdir($new->gitDir);
42
43         $cmd = $vc->getCommand('clone')
44             //this should be setOption, but it fails with a = between name and value
45             ->addArgument('--separate-git-dir')
46             ->addArgument(
47                 $GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $new->id . '.git'
48             )
49             ->addArgument($pathOrUrl)
50             ->addArgument($new->workDir);
51         try {
52             $cmd->execute();
53         } catch (\Exception $e) {
54             //clean up, we've got no workdir otherwise
55             $new->delete();
56             throw $e;
57         }
58
59         $rs = new Repository_Setup($new);
60         $rs->afterInit();
61
62         return $new;
63     }
64
65     protected function index($repo)
66     {
67         $db = new Database();
68         $db->getIndexer()->addRepo($repo);
69     }
70 }
71
72 ?>