From 4e56fed76df6fcdcf87e54cdf1cc77b9030ba723 Mon Sep 17 00:00:00 2001 From: tim Date: Wed, 5 Apr 2023 22:46:37 +0200 Subject: [PATCH] Allow going back to a previous commit --- src/Controller/HistoryController.php | 36 ++++++++++++++++++++++++++++ src/Git/CustomGitRepository.php | 12 ++++++---- src/Git/SimpleCommit.php | 26 ++++++++++++++++++++ src/Service/SnipService.php | 3 +-- templates/history/index.html.twig | 19 +++++++++++++++ templates/snip/single.html.twig | 3 +++ 6 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 src/Controller/HistoryController.php create mode 100644 src/Git/SimpleCommit.php create mode 100644 templates/history/index.html.twig 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 + +
+ {% for commit in commits %} + + {{ commit.date|date }} - {{ commit.hash }} + + {% endfor %} +
+{% endblock %} \ No newline at end of file diff --git a/templates/snip/single.html.twig b/templates/snip/single.html.twig index b843e77..0ebefee 100644 --- a/templates/snip/single.html.twig +++ b/templates/snip/single.html.twig @@ -10,6 +10,9 @@ Edit + + History + {% endif %} Raw