forked from modess/git-pretty-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
99 lines (79 loc) · 2.97 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
ini_set('memory_limit', '512M');
set_time_limit(300);
require_once __DIR__.'/vendor/autoload.php';
use Symfony\Component\HttpFoundation\Response;
use GitPrettyStats\Repository;
use GitPrettyStats\RepositoryList;
$app = new Silex\Application();
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__.'/views',
));
$app->error(function(\Exception $e) use($app) {
return $app["twig"]->render("error.html", array("message" => $e->getMessage()));
});
$app->before(function() use ($app) {
$repositoriesPath = 'repositories';
$configFilePath = __DIR__ . '/config.php';
if (file_exists($configFilePath)) {
$configFile = require_once __DIR__ . '/config.php';
if (isset($configFile['repositoriesPath'])) {
$repositoriesPath = $configFile['repositoriesPath'];
}
}
$config['repositoriesPath'] = $repositoriesPath;
$app['config'] = $config;
$repositoryList = new RepositoryList($repositoriesPath);
$app['repositories'] = $repositoryList->getRepositories();
$app['repositoryList'] = new RepositoryList;
if (count($app['repositories']) == 0) {
throw new RuntimeException("No repositories found in path: $repositoriesPath", 0);
}
});
function loadRepository ($app, $path) {
if (isset($app['config']['repositoriesPath'][$path])) {
$repositoryPath = $app['config']['repositoriesPath'][$path];
} elseif (!realpath($path)) {
$repositoryPath = $app['config']['repositoriesPath'] . '/' . $path;
}
if (!$repository = $app['repositoryList']->loadRepository($repositoryPath)) {
throw new RuntimeException('The repository path does not contain a valid git repository', 0, $e);
}
try {
$repository->loadCommits();
} catch (Exception $e) {
// Catch all possible errors while loading the repository, re-wrap it with a friendlier
// message and re-throw so it's caught by the error handler:
// (the original exception is chained to the new one):
throw new RuntimeException('Could not load commits from repository', 0, $e);
}
return $repository;
}
$app->get('/', function () use ($app) {
return $app['twig']->render(
'index.html',
array(
'repositories' => $app['repositories']
)
);
});
$app->get('repository/{path}', function ($path) use ($app) {
$repository = loadRepository($app, $path);
return $app['twig']->render(
'repository.html',
array(
'repositories' => $app['repositories'],
'name' => $repository->getName(),
'branch' => $repository->gitter->getCurrentBranch(),
'statsEndpoint' => $app["request"]->getBaseUrl() . "/git-stats/" . $path,
)
);
});
$app->get('/git-stats/{path}', function($path) use($app) {
$repository = loadRepository($app, $path);
return $app->json(
$repository->getStatistics()
);
});
$app['debug'] = true;
$app->run();