update-server-info on git pushes and forks
[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
10         \copy($repo->gitDir . '/description', $new->gitDir . '/description');
11         $new->getVc()
12             ->getCommand('config')
13             ->addArgument('remote.origin.title')
14             ->addArgument(file_get_contents($repo->gitDir . '/description'))
15             ->execute();
16
17         $this->index($new);
18
19         $not = new Notificator();
20         $not->create($new);
21
22         return $new;
23     }
24
25     public function forkRemote($cloneUrl, $originalUrl, $title = null)
26     {
27         $new = $this->fork($cloneUrl);
28
29         $new->getVc()
30             ->getCommand('config')
31             ->addArgument('remote.origin.title')
32             ->addArgument($title)
33             ->execute();
34         if ($originalUrl != $cloneUrl) {
35             $new->getVc()
36                 ->getCommand('config')
37                 ->addArgument('remote.origin.homepage')
38                 ->addArgument($originalUrl)
39                 ->execute();
40         }
41
42         if ($title === null) {
43             $title = 'Fork of ' . $originalUrl;
44         }
45         file_put_contents($new->gitDir . '/description', $title);
46
47         $this->index($new);
48
49         $not = new Notificator();
50         $not->create($new);
51
52         return $new;
53     }
54
55
56     protected function fork($pathOrUrl)
57     {
58         $rs = new Repositories();
59         $new = $rs->createNew();
60         $vc = $new->getVc();
61
62         //VersionControl_Git wants an existing dir, git clone not
63         \rmdir($new->gitDir);
64
65         $cmd = $vc->getCommand('clone')
66             //this should be setOption, but it fails with a = between name and value
67             ->addArgument('--separate-git-dir')
68             ->addArgument(
69                 $GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $new->id . '.git'
70             )
71             ->addArgument($pathOrUrl)
72             ->addArgument($new->workDir);
73         try {
74             $cmd->execute();
75         } catch (\Exception $e) {
76             //clean up, we've got no workdir otherwise
77             $new->delete();
78             throw $e;
79         }
80
81         $rs = new Repository_Setup($new);
82         $rs->afterInit();
83
84         //update info for dumb git HTTP transport
85         //the post-update hook should do that IMO, but does not somehow
86         $vc->getCommand('update-server-info')->execute();
87
88         return $new;
89     }
90
91     protected function index($repo)
92     {
93         $db = new Database();
94         $db->getIndexer()->addRepo($repo);
95     }
96 }
97
98 ?>