diff --git a/src/Controller/HistoryController.php b/src/Controller/HistoryController.php new file mode 100644 index 0000000..26145c8 --- /dev/null +++ b/src/Controller/HistoryController.php @@ -0,0 +1,36 @@ +render('history/index.html.twig', [ + 'snip' => $snip, + 'commits' => $this->snipServiceFactory->create($snip)->getRepo()->getAllCommits(), + ]); + } + + #[Route('/set/{commit}', name: '_set')] + public function set(Snip $snip, string $commit): Response + { + $this->snipServiceFactory->create($snip)->getRepo()->checkout($commit); + $this->addFlash('success', 'Snip version set to ' . $commit); + return $this->redirectToRoute('snip_single', ['snip' => $snip->getId()]); + } +} \ No newline at end of file diff --git a/src/Git/CustomGitRepository.php b/src/Git/CustomGitRepository.php index 14acfef..99c3eb7 100644 --- a/src/Git/CustomGitRepository.php +++ b/src/Git/CustomGitRepository.php @@ -7,6 +7,10 @@ use DateTime; class CustomGitRepository extends GitRepository { + /** + * @return SimpleCommit[] + * @throws \CzProject\GitPhp\GitException + */ public function getAllCommits(): array { $result = $this->run('log', '--pretty=%H,%cI'); @@ -18,10 +22,10 @@ class CustomGitRepository extends GitRepository $commits = []; foreach ($result->getOutput() as $line) { $parts = explode(',', $line); - $commits[] = [ - 'hash' => $parts[0], - 'date' => new DateTime($parts[1]), - ]; + $commits[] = new SimpleCommit( + $parts[0], + new DateTime($parts[1]) + ); } return $commits; diff --git a/src/Git/SimpleCommit.php b/src/Git/SimpleCommit.php new file mode 100644 index 0000000..254c846 --- /dev/null +++ b/src/Git/SimpleCommit.php @@ -0,0 +1,26 @@ +hash; + } + + public function getDate(): DateTime + { + return $this->date; + } +} \ No newline at end of file diff --git a/src/Service/SnipService.php b/src/Service/SnipService.php index 24d841d..93acf8f 100644 --- a/src/Service/SnipService.php +++ b/src/Service/SnipService.php @@ -4,7 +4,6 @@ namespace App\Service; use App\Entity\User; use App\Git\CustomGitRepository; -use CzProject\GitPhp\GitRepository; class SnipService { @@ -42,7 +41,7 @@ class SnipService return file_get_contents($this->getSnipPath()); } - public function getRepo(): GitRepository + public function getRepo(): CustomGitRepository { return $this->repo; } diff --git a/templates/history/index.html.twig b/templates/history/index.html.twig new file mode 100644 index 0000000..28cee93 --- /dev/null +++ b/templates/history/index.html.twig @@ -0,0 +1,19 @@ +{% extends 'base/base.html.twig' %} + +{% block title %}Snip {{ snip }}{% endblock %} + +{% block body %} + + Back + + + Reset to latest + +