Start on snip history manipulation
This commit is contained in:
parent
ccab49dd3c
commit
e461a7ad35
13
src/Git/CustomGit.php
Normal file
13
src/Git/CustomGit.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Git;
|
||||
|
||||
use CzProject\GitPhp\Git;
|
||||
|
||||
class CustomGit extends Git
|
||||
{
|
||||
public function open($directory)
|
||||
{
|
||||
return new CustomGitRepository($directory, $this->runner);
|
||||
}
|
||||
}
|
29
src/Git/CustomGitRepository.php
Normal file
29
src/Git/CustomGitRepository.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Git;
|
||||
|
||||
use CzProject\GitPhp\GitRepository;
|
||||
use DateTime;
|
||||
|
||||
class CustomGitRepository extends GitRepository
|
||||
{
|
||||
public function getAllCommits(): array
|
||||
{
|
||||
$result = $this->run('log', '--pretty=%H,%cI');
|
||||
|
||||
if (empty($result->getOutput())) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$commits = [];
|
||||
foreach ($result->getOutput() as $line) {
|
||||
$parts = explode(',', $line);
|
||||
$commits[] = [
|
||||
'hash' => $parts[0],
|
||||
'date' => new DateTime($parts[1]),
|
||||
];
|
||||
}
|
||||
|
||||
return $commits;
|
||||
}
|
||||
}
|
@ -2,28 +2,17 @@
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\Snip;
|
||||
use App\Entity\User;
|
||||
use CzProject\GitPhp\Git;
|
||||
use App\Git\CustomGitRepository;
|
||||
use CzProject\GitPhp\GitRepository;
|
||||
|
||||
class SnipService
|
||||
{
|
||||
private GitRepository $repo;
|
||||
|
||||
public function __construct(
|
||||
Snip $snip,
|
||||
string $snipBasePath,
|
||||
private readonly User $user,
|
||||
private readonly CustomGitRepository $repo,
|
||||
private readonly User $user,
|
||||
)
|
||||
{
|
||||
$git = new Git();
|
||||
$repoPath = sprintf('%s/%s', $snipBasePath, $snip->getId());
|
||||
if (!is_dir($repoPath)) {
|
||||
$this->repo = $git->init($repoPath);
|
||||
} else {
|
||||
$this->repo = $git->open($repoPath);
|
||||
}
|
||||
}
|
||||
|
||||
private function snipExists(): bool
|
||||
@ -52,4 +41,9 @@ class SnipService
|
||||
}
|
||||
return file_get_contents($this->getSnipPath());
|
||||
}
|
||||
|
||||
public function getRepo(): GitRepository
|
||||
{
|
||||
return $this->repo;
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\Snip;
|
||||
use App\Git\CustomGit;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
|
||||
class SnipServiceFactory
|
||||
@ -17,6 +18,13 @@ class SnipServiceFactory
|
||||
|
||||
public function create(Snip $snip): SnipService
|
||||
{
|
||||
return new SnipService($snip, $this->snipBasePath, $this->security->getUser());
|
||||
$git = new CustomGit();
|
||||
$repoPath = sprintf('%s/%s', $this->snipBasePath, $snip->getId());
|
||||
if (!is_dir($repoPath)) {
|
||||
$repo = $git->init($repoPath);
|
||||
} else {
|
||||
$repo = $git->open($repoPath);
|
||||
}
|
||||
return new SnipService($repo, $this->security->getUser());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user