Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommitController.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace GitList\Controller;
  3. use Silex\Application;
  4. use Silex\ControllerProviderInterface;
  5. use Symfony\Component\HttpFoundation\Request;
  6. class CommitController implements ControllerProviderInterface
  7. {
  8. public function connect(Application $app)
  9. {
  10. $route = $app['controllers_factory'];
  11. $route->get('{repo}/commits/search', function (Request $request, $repo) use ($app) {
  12. $subRequest = Request::create(
  13. '/' . $repo . '/commits/master/search',
  14. 'POST',
  15. array('query' => $request->get('query'))
  16. );
  17. return $app->handle($subRequest, \Symfony\Component\HttpKernel\HttpKernelInterface::SUB_REQUEST);
  18. })->assert('repo', $app['util.routing']->getRepositoryRegex());
  19. $route->get('{repo}/commits/{commitishPath}', function ($repo, $commitishPath) use ($app) {
  20. $repository = $app['git']->getRepositoryFromName($app['git.repos'], $repo);
  21. if ($commitishPath === null) {
  22. $commitishPath = $repository->getHead();
  23. }
  24. list($branch, $file) = $app['util.routing']
  25. ->parseCommitishPathParam($commitishPath, $repo);
  26. list($branch, $file) = $app['util.repository']->extractRef($repository, $branch, $file);
  27. $type = $file ? "$branch -- \"$file\"" : $branch;
  28. $pager = $app['util.view']->getPager($app['request']->get('page'), $repository->getTotalCommits($type));
  29. $commits = $repository->getPaginatedCommits($type, $pager['current']);
  30. $categorized = array();
  31. foreach ($commits as $commit) {
  32. $date = $commit->getDate();
  33. $date = $date->format('Y-m-d');
  34. $categorized[$date][] = $commit;
  35. }
  36. krsort($categorized);
  37. $template = $app['request']->isXmlHttpRequest() ? 'commits_list.twig' : 'commits.twig';
  38. return $app['twig']->render($template, array(
  39. 'page' => 'commits',
  40. 'pager' => $pager,
  41. 'repo' => $repo,
  42. 'branch' => $branch,
  43. 'branches' => $repository->getBranches(),
  44. 'tags' => $repository->getTags(),
  45. 'commits' => $categorized,
  46. 'file' => $file,
  47. ));
  48. })->assert('repo', $app['util.routing']->getRepositoryRegex())
  49. ->assert('commitishPath', $app['util.routing']->getCommitishPathRegex())
  50. ->value('commitishPath', null)
  51. ->convert('commitishPath', 'escaper.argument:escape')
  52. ->bind('commits');
  53. $route->post('{repo}/commits/{branch}/search', function (Request $request, $repo, $branch = '') use ($app) {
  54. $repository = $app['git']->getRepositoryFromName($app['git.repos'], $repo);
  55. $query = $request->get('query');
  56. $commits = $repository->searchCommitLog($request->get('query'));
  57. $categorized = array();
  58. foreach ($commits as $commit) {
  59. $date = $commit->getDate();
  60. $date = $date->format('Y-m-d');
  61. $categorized[$date][] = $commit;
  62. }
  63. krsort($categorized);
  64. return $app['twig']->render('searchcommits.twig', array(
  65. 'repo' => $repo,
  66. 'branch' => $branch,
  67. 'file' => '',
  68. 'commits' => $categorized,
  69. 'branches' => $repository->getBranches(),
  70. 'tags' => $repository->getTags(),
  71. 'query' => $query
  72. ));
  73. })->assert('repo', $app['util.routing']->getRepositoryRegex())
  74. ->assert('branch', $app['util.routing']->getBranchRegex())
  75. ->convert('branch', 'escaper.argument:escape')
  76. ->bind('searchcommits');
  77. $route->get('{repo}/commit/{commit}', function ($repo, $commit) use ($app) {
  78. $repository = $app['git']->getRepositoryFromName($app['git.repos'], $repo);
  79. $commit = $repository->getCommit($commit);
  80. $branch = $repository->getHead();
  81. return $app['twig']->render('commit.twig', array(
  82. 'branch' => $branch,
  83. 'repo' => $repo,
  84. 'commit' => $commit,
  85. ));
  86. })->assert('repo', $app['util.routing']->getRepositoryRegex())
  87. ->assert('commit', '[a-f0-9^]+')
  88. ->bind('commit');
  89. $route->get('{repo}/blame/{commitishPath}', function ($repo, $commitishPath) use ($app) {
  90. $repository = $app['git']->getRepositoryFromName($app['git.repos'], $repo);
  91. list($branch, $file) = $app['util.routing']
  92. ->parseCommitishPathParam($commitishPath, $repo);
  93. list($branch, $file) = $app['util.repository']->extractRef($repository, $branch, $file);
  94. $blames = $repository->getBlame("$branch -- \"$file\"");
  95. return $app['twig']->render('blame.twig', array(
  96. 'file' => $file,
  97. 'repo' => $repo,
  98. 'branch' => $branch,
  99. 'branches' => $repository->getBranches(),
  100. 'tags' => $repository->getTags(),
  101. 'blames' => $blames,
  102. ));
  103. })->assert('repo', $app['util.routing']->getRepositoryRegex())
  104. ->assert('commitishPath', $app['util.routing']->getCommitishPathRegex())
  105. ->convert('commitishPath', 'escaper.argument:escape')
  106. ->bind('blame');
  107. return $route;
  108. }
  109. }