Allow going back to a previous commit
This commit is contained in:
parent
e461a7ad35
commit
4e56fed76d
36
src/Controller/HistoryController.php
Normal file
36
src/Controller/HistoryController.php
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Controller;
|
||||||
|
|
||||||
|
use App\Entity\Snip;
|
||||||
|
use App\Service\SnipServiceFactory;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\Routing\Annotation\Route;
|
||||||
|
|
||||||
|
#[Route('/history/{snip}', name: 'history')]
|
||||||
|
class HistoryController extends AbstractController
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly SnipServiceFactory $snipServiceFactory,
|
||||||
|
)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route('/', name: '_index')]
|
||||||
|
public function index(Snip $snip): Response
|
||||||
|
{
|
||||||
|
return $this->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()]);
|
||||||
|
}
|
||||||
|
}
|
@ -7,6 +7,10 @@ use DateTime;
|
|||||||
|
|
||||||
class CustomGitRepository extends GitRepository
|
class CustomGitRepository extends GitRepository
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @return SimpleCommit[]
|
||||||
|
* @throws \CzProject\GitPhp\GitException
|
||||||
|
*/
|
||||||
public function getAllCommits(): array
|
public function getAllCommits(): array
|
||||||
{
|
{
|
||||||
$result = $this->run('log', '--pretty=%H,%cI');
|
$result = $this->run('log', '--pretty=%H,%cI');
|
||||||
@ -18,10 +22,10 @@ class CustomGitRepository extends GitRepository
|
|||||||
$commits = [];
|
$commits = [];
|
||||||
foreach ($result->getOutput() as $line) {
|
foreach ($result->getOutput() as $line) {
|
||||||
$parts = explode(',', $line);
|
$parts = explode(',', $line);
|
||||||
$commits[] = [
|
$commits[] = new SimpleCommit(
|
||||||
'hash' => $parts[0],
|
$parts[0],
|
||||||
'date' => new DateTime($parts[1]),
|
new DateTime($parts[1])
|
||||||
];
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $commits;
|
return $commits;
|
||||||
|
26
src/Git/SimpleCommit.php
Normal file
26
src/Git/SimpleCommit.php
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Git;
|
||||||
|
|
||||||
|
use DateTime;
|
||||||
|
|
||||||
|
class SimpleCommit
|
||||||
|
{
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private readonly string $hash,
|
||||||
|
private readonly DateTime $date,
|
||||||
|
)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getHash(): string
|
||||||
|
{
|
||||||
|
return $this->hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDate(): DateTime
|
||||||
|
{
|
||||||
|
return $this->date;
|
||||||
|
}
|
||||||
|
}
|
@ -4,7 +4,6 @@ namespace App\Service;
|
|||||||
|
|
||||||
use App\Entity\User;
|
use App\Entity\User;
|
||||||
use App\Git\CustomGitRepository;
|
use App\Git\CustomGitRepository;
|
||||||
use CzProject\GitPhp\GitRepository;
|
|
||||||
|
|
||||||
class SnipService
|
class SnipService
|
||||||
{
|
{
|
||||||
@ -42,7 +41,7 @@ class SnipService
|
|||||||
return file_get_contents($this->getSnipPath());
|
return file_get_contents($this->getSnipPath());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getRepo(): GitRepository
|
public function getRepo(): CustomGitRepository
|
||||||
{
|
{
|
||||||
return $this->repo;
|
return $this->repo;
|
||||||
}
|
}
|
||||||
|
19
templates/history/index.html.twig
Normal file
19
templates/history/index.html.twig
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
{% extends 'base/base.html.twig' %}
|
||||||
|
|
||||||
|
{% block title %}Snip {{ snip }}{% endblock %}
|
||||||
|
|
||||||
|
{% block body %}
|
||||||
|
<a href="{{ path('snip_single', {snip: snip.id}) }}" class="btn btn-primary">
|
||||||
|
<i class="fa fa-arrow-left"></i> Back
|
||||||
|
</a>
|
||||||
|
<a href="{{ path('history_set', {commit: 'master', snip: snip.id}) }}" class="btn btn-primary">
|
||||||
|
<i class="fa fa-reset"></i> Reset to latest
|
||||||
|
</a>
|
||||||
|
<div class="list-group">
|
||||||
|
{% for commit in commits %}
|
||||||
|
<a class="list-group-item" href="{{ path('history_set', {commit: commit.hash, snip: snip.id}) }}">
|
||||||
|
{{ commit.date|date }} - {{ commit.hash }}
|
||||||
|
</a>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
@ -10,6 +10,9 @@
|
|||||||
<a class="btn btn-warning" href="{{ path('snip_edit', {snip: snip.id}) }}">
|
<a class="btn btn-warning" href="{{ path('snip_edit', {snip: snip.id}) }}">
|
||||||
<i class="fa fa-pencil" aria-hidden="true"></i> Edit
|
<i class="fa fa-pencil" aria-hidden="true"></i> Edit
|
||||||
</a>
|
</a>
|
||||||
|
<a class="btn btn-info" href="{{ path('history_index', {snip: snip.id}) }}">
|
||||||
|
<i class="fa fa-history" aria-hidden="true"></i> History
|
||||||
|
</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<a href="{{ path('snip_raw', {snip: snip.id}) }}" class="btn btn-danger">
|
<a href="{{ path('snip_raw', {snip: snip.id}) }}" class="btn btn-danger">
|
||||||
<i class="fa fa-eye"></i> Raw
|
<i class="fa fa-eye"></i> Raw
|
||||||
|
Loading…
Reference in New Issue
Block a user