1<?php
2
3namespace GitList\Controller;
4
5use Silex\ControllerProviderInterface;
6use Silex\Application;
7
8class TreeGraphController implements ControllerProviderInterface
9{
10    public function connect(Application $app)
11    {
12        $route = $app['controllers_factory'];
13
14        $route->get(
15            '{repo}/treegraph/{commitishPath}',
16            function ($repo, $commitishPath) use ($app) {
17                /** @var \GitList\Git\Repository $repository */
18                $repository = $app['git']->getRepositoryFromName($app['git.repos'], $repo);
19
20                $command = 'log --graph --date-order --all -C -M -n 100 --date=iso ' .
21                    '--pretty=format:"B[%d] C[%H] D[%ad] A[%an] E[%ae] H[%h] S[%s]"';
22                $rawRows = $repository->getClient()->run($repository, $command);
23                $rawRows = explode("\n", $rawRows);
24                $graphItems = array();
25
26                foreach ($rawRows as $row) {
27                    if (preg_match("/^(.+?)(\s(B\[(.*?)\])? C\[(.+?)\] D\[(.+?)\] A\[(.+?)\] E\[(.+?)\] H\[(.+?)\] S\[(.+?)\])?$/", $row, $output)) {
28                        if (!isset($output[4])) {
29                            $graphItems[] = array(
30                                'relation' => $output[1],
31                            );
32                            continue;
33                        }
34                        $graphItems[] = array(
35                            'relation' => $output[1],
36                            'branch' => $output[4],
37                            'rev' => $output[5],
38                            'date' => $output[6],
39                            'author' => $output[7],
40                            'author_email' => $output[8],
41                            'short_rev' => $output[9],
42                            'subject' => preg_replace('/(^|\s)(#[[:xdigit:]]+)(\s|$)/', '$1<a href="$2">$2</a>$3', $output[10]),
43                        );
44                    }
45                }
46
47                if ($commitishPath === null) {
48                    $commitishPath = $repository->getHead();
49                }
50
51                list($branch, $file) = $app['util.routing']->parseCommitishPathParam($commitishPath, $repo);
52                list($branch, $file) = $app['util.repository']->extractRef($repository, $branch, $file);
53
54                return $app['twig']->render(
55                    'treegraph.twig',
56                    array(
57                        'repo' => $repo,
58                        'branch' => $branch,
59                        'commitishPath' => $commitishPath,
60                        'graphItems' => $graphItems,
61                    )
62                );
63            }
64        )->assert('repo', $app['util.routing']->getRepositoryRegex())
65            ->assert('commitishPath', $app['util.routing']->getCommitishPathRegex())
66            ->value('commitishPath', null)
67            ->convert('commitishPath', 'escaper.argument:escape')
68            ->bind('treegraph');
69
70        return $route;
71    }
72}
73