From e461a7ad359595dc1840e712c8b84d08b9a0b1ec Mon Sep 17 00:00:00 2001 From: tim Date: Wed, 5 Apr 2023 08:26:36 +0200 Subject: [PATCH] Start on snip history manipulation --- src/Git/CustomGit.php | 13 +++++++++++++ src/Git/CustomGitRepository.php | 29 +++++++++++++++++++++++++++++ src/Service/SnipService.php | 22 ++++++++-------------- src/Service/SnipServiceFactory.php | 10 +++++++++- 4 files changed, 59 insertions(+), 15 deletions(-) create mode 100644 src/Git/CustomGit.php create mode 100644 src/Git/CustomGitRepository.php diff --git a/src/Git/CustomGit.php b/src/Git/CustomGit.php new file mode 100644 index 0000000..8666df0 --- /dev/null +++ b/src/Git/CustomGit.php @@ -0,0 +1,13 @@ +runner); + } +} \ No newline at end of file diff --git a/src/Git/CustomGitRepository.php b/src/Git/CustomGitRepository.php new file mode 100644 index 0000000..14acfef --- /dev/null +++ b/src/Git/CustomGitRepository.php @@ -0,0 +1,29 @@ +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; + } +} \ No newline at end of file diff --git a/src/Service/SnipService.php b/src/Service/SnipService.php index 9250d09..24d841d 100644 --- a/src/Service/SnipService.php +++ b/src/Service/SnipService.php @@ -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; + } } \ No newline at end of file diff --git a/src/Service/SnipServiceFactory.php b/src/Service/SnipServiceFactory.php index 04c2f86..52c5e1e 100644 --- a/src/Service/SnipServiceFactory.php +++ b/src/Service/SnipServiceFactory.php @@ -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()); } } \ No newline at end of file