From 1f464220ee2be623af73f09b83a1d17c30c2fe76 Mon Sep 17 00:00:00 2001 From: tim Date: Wed, 5 Apr 2023 22:58:58 +0200 Subject: [PATCH] Show current branch in snip single Reset branch automatically if trying to commit detached --- src/Controller/SnipController.php | 5 +++-- src/Service/SnipService.php | 8 +++++++- templates/snip/single.html.twig | 3 +++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Controller/SnipController.php b/src/Controller/SnipController.php index 26a7a6b..ca7f238 100644 --- a/src/Controller/SnipController.php +++ b/src/Controller/SnipController.php @@ -35,10 +35,11 @@ class SnipController extends AbstractController public function single(Snip $snip): Response { $this->denyAccessUnlessGranted(SnipVoter::VIEW, $snip); - + $snipService = $this->snipServiceFactory->create($snip); return $this->render('snip/single.html.twig', [ 'snip' => $snip, - 'content' => $this->snipServiceFactory->create($snip)->get(), + 'content' => $snipService->get(), + 'branch' => $snipService->getRepo()->getCurrentBranchName(), ]); } diff --git a/src/Service/SnipService.php b/src/Service/SnipService.php index 93acf8f..a50809e 100644 --- a/src/Service/SnipService.php +++ b/src/Service/SnipService.php @@ -7,6 +7,9 @@ use App\Git\CustomGitRepository; class SnipService { + private const SNIP_FILE_NAME = 'snip.txt'; + private const MASTER_BRANCH_NAME = 'master'; + public function __construct( private readonly CustomGitRepository $repo, private readonly User $user, @@ -26,8 +29,11 @@ class SnipService public function update(string $snipContents): void { + if ($this->repo->getCurrentBranchName() !== self::MASTER_BRANCH_NAME) { + $this->repo->checkout(self::MASTER_BRANCH_NAME); + } file_put_contents($this->getSnipPath(), $snipContents); - $this->repo->addFile('snip.txt'); + $this->repo->addFile(self::SNIP_FILE_NAME); if ($this->repo->hasChanges()) { $this->repo->commit(sprintf('Updated snip at %s by %s', date('Y-m-d H:i:s'), $this->user)); } diff --git a/templates/snip/single.html.twig b/templates/snip/single.html.twig index 0ebefee..c707d43 100644 --- a/templates/snip/single.html.twig +++ b/templates/snip/single.html.twig @@ -23,6 +23,9 @@ {{ include('snip/badge.html.twig', {snip: snip}) }} {{ snip }} +
+

Current branch: {{ branch }}

+

{{ content|nl2br }}