Show current branch in snip single

Reset branch automatically if trying to commit detached
This commit is contained in:
Tim 2023-04-05 22:58:58 +02:00
parent 4e56fed76d
commit 1f464220ee
3 changed files with 13 additions and 3 deletions

View File

@ -35,10 +35,11 @@ class SnipController extends AbstractController
public function single(Snip $snip): Response public function single(Snip $snip): Response
{ {
$this->denyAccessUnlessGranted(SnipVoter::VIEW, $snip); $this->denyAccessUnlessGranted(SnipVoter::VIEW, $snip);
$snipService = $this->snipServiceFactory->create($snip);
return $this->render('snip/single.html.twig', [ return $this->render('snip/single.html.twig', [
'snip' => $snip, 'snip' => $snip,
'content' => $this->snipServiceFactory->create($snip)->get(), 'content' => $snipService->get(),
'branch' => $snipService->getRepo()->getCurrentBranchName(),
]); ]);
} }

View File

@ -7,6 +7,9 @@ use App\Git\CustomGitRepository;
class SnipService class SnipService
{ {
private const SNIP_FILE_NAME = 'snip.txt';
private const MASTER_BRANCH_NAME = 'master';
public function __construct( public function __construct(
private readonly CustomGitRepository $repo, private readonly CustomGitRepository $repo,
private readonly User $user, private readonly User $user,
@ -26,8 +29,11 @@ class SnipService
public function update(string $snipContents): void 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); file_put_contents($this->getSnipPath(), $snipContents);
$this->repo->addFile('snip.txt'); $this->repo->addFile(self::SNIP_FILE_NAME);
if ($this->repo->hasChanges()) { if ($this->repo->hasChanges()) {
$this->repo->commit(sprintf('Updated snip at %s by %s', date('Y-m-d H:i:s'), $this->user)); $this->repo->commit(sprintf('Updated snip at %s by %s', date('Y-m-d H:i:s'), $this->user));
} }

View File

@ -23,6 +23,9 @@
{{ include('snip/badge.html.twig', {snip: snip}) }} {{ include('snip/badge.html.twig', {snip: snip}) }}
{{ snip }} {{ snip }}
</h4> </h4>
<div class="card-header">
<p class="card-text">Current branch: {{ branch }}</p>
</div>
<div class="card-body"> <div class="card-body">
<p class="card-text">{{ content|nl2br }}</p> <p class="card-text">{{ content|nl2br }}</p>
</div> </div>