From eefc52c3a3e21f17b5cffd20f627ff7ebf808ced Mon Sep 17 00:00:00 2001 From: Patrick Beuks Date: Fri, 26 Apr 2024 21:10:02 +0200 Subject: [PATCH 1/4] Add optional entry type option for tree entries --- src/Gitonomy/Git/Tree.php | 22 +++++++++++++++------- src/Gitonomy/Git/TreeType.php | 20 ++++++++++++++++++++ tests/Gitonomy/Git/Tests/TreeTest.php | 24 +++++++++++++++++++++++- 3 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 src/Gitonomy/Git/TreeType.php diff --git a/src/Gitonomy/Git/Tree.php b/src/Gitonomy/Git/Tree.php index 570c8ff8..b2f5d95f 100644 --- a/src/Gitonomy/Git/Tree.php +++ b/src/Gitonomy/Git/Tree.php @@ -24,6 +24,7 @@ class Tree protected $hash; protected $isInitialized = false; protected $entries; + protected $entriesByType; public function __construct(Repository $repository, $hash) { @@ -47,16 +48,23 @@ protected function initialize() $parser->parse($output); $this->entries = []; + $this->entriesByType = [ + TreeType::BLOB->value => [], + TreeType::TREE->value => [], + TreeType::COMMIT->value => [], + ]; foreach ($parser->entries as $entry) { list($mode, $type, $hash, $name) = $entry; - if ($type == 'blob') { - $this->entries[$name] = [$mode, $this->repository->getBlob($hash)]; - } elseif ($type == 'tree') { - $this->entries[$name] = [$mode, $this->repository->getTree($hash)]; + if ($type == TreeType::BLOB->value) { + $treeEntry = [$mode, $this->repository->getBlob($hash)]; + } elseif ($type == TreeType::TREE->value) { + $treeEntry = [$mode, $this->repository->getTree($hash)]; } else { - $this->entries[$name] = [$mode, new CommitReference($hash)]; + $treeEntry = [$mode, new CommitReference($hash)]; } + $this->entries[$name] = $treeEntry; + $this->entriesByType[$type][$name] = $treeEntry; } $this->isInitialized = true; @@ -65,11 +73,11 @@ protected function initialize() /** * @return array An associative array name => $object */ - public function getEntries() + public function getEntries(?TreeType $type = null) { $this->initialize(); - return $this->entries; + return $type ? $this->entriesByType[$type->value] : $this->entries; } public function getEntry($name) diff --git a/src/Gitonomy/Git/TreeType.php b/src/Gitonomy/Git/TreeType.php new file mode 100644 index 00000000..d9b9f5c3 --- /dev/null +++ b/src/Gitonomy/Git/TreeType.php @@ -0,0 +1,20 @@ + + * (c) Julien DIDIER + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace Gitonomy\Git; + +enum TreeType: string +{ + case BLOB = 'blob'; + case TREE = 'tree'; + case COMMIT = 'commit'; +} diff --git a/tests/Gitonomy/Git/Tests/TreeTest.php b/tests/Gitonomy/Git/Tests/TreeTest.php index 4c0476de..0358ffd7 100644 --- a/tests/Gitonomy/Git/Tests/TreeTest.php +++ b/tests/Gitonomy/Git/Tests/TreeTest.php @@ -13,6 +13,8 @@ namespace Gitonomy\Git\Tests; use Gitonomy\Git\Blob; +use Gitonomy\Git\CommitReference; +use Gitonomy\Git\TreeType; class TreeTest extends AbstractTest { @@ -21,7 +23,7 @@ class TreeTest extends AbstractTest /** * @dataProvider provideFooBar */ - public function testCase($repository) + public function testGetEntries($repository) { $tree = $repository->getCommit(self::LONGFILE_COMMIT)->getTree(); @@ -34,6 +36,26 @@ public function testCase($repository) $this->assertTrue($entries['README.md'][1] instanceof Blob, 'README.md is a Blob'); } + /** + * @dataProvider provideFooBar + */ + public function testGetEntriesByType($repository) + { + $tree = $repository->getCommit(self::NO_MESSAGE_COMMIT)->getTree(); + + $blobs = $tree->getEntries(TreeType::BLOB); + + $this->assertNotEmpty($blobs['README.md'], 'README.md is present'); + $this->assertTrue($blobs['README.md'][1] instanceof Blob, 'README.md is a blob'); + + $trees = $tree->getEntries(TreeType::TREE); + $this->assertEmpty($trees); + + $commits = $tree->getEntries(TreeType::COMMIT); + $this->assertNotEmpty($commits['barbaz'], 'barbaz is present'); + $this->assertTrue($commits['barbaz'][1] instanceof CommitReference, 'barbaz is a Commit'); + } + /** * @dataProvider provideFooBar */ From 4937935c4671dd1bee7528d8f9b82f7ff1bf8069 Mon Sep 17 00:00:00 2001 From: Patrick Beuks Date: Fri, 26 Apr 2024 21:23:52 +0200 Subject: [PATCH 2/4] Fix cs problems --- src/Gitonomy/Git/Tree.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Gitonomy/Git/Tree.php b/src/Gitonomy/Git/Tree.php index b2f5d95f..1496aa05 100644 --- a/src/Gitonomy/Git/Tree.php +++ b/src/Gitonomy/Git/Tree.php @@ -49,8 +49,8 @@ protected function initialize() $this->entries = []; $this->entriesByType = [ - TreeType::BLOB->value => [], - TreeType::TREE->value => [], + TreeType::BLOB->value => [], + TreeType::TREE->value => [], TreeType::COMMIT->value => [], ]; From 343dc13e806f2cb8f63d158a24afbef3d813efde Mon Sep 17 00:00:00 2001 From: Patrick Beuks Date: Sun, 5 May 2024 11:42:49 +0200 Subject: [PATCH 3/4] Update test for diff range counts --- tests/Gitonomy/Git/Tests/DiffTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Gitonomy/Git/Tests/DiffTest.php b/tests/Gitonomy/Git/Tests/DiffTest.php index e251d72e..2232345f 100644 --- a/tests/Gitonomy/Git/Tests/DiffTest.php +++ b/tests/Gitonomy/Git/Tests/DiffTest.php @@ -139,7 +139,7 @@ public function testDiffRangeParse($repository) $this->assertSame(0, $changes[0]->getRangeOldCount()); $this->assertSame(1, $changes[0]->getRangeNewStart()); - $this->assertSame(0, $changes[0]->getRangeNewCount()); + $this->assertSame(1, $changes[0]->getRangeNewCount()); } /** From 574ad0c5cc99c9f8b5bcd497d98f8e734266bed1 Mon Sep 17 00:00:00 2001 From: Patrick Beuks Date: Sun, 5 May 2024 11:57:34 +0200 Subject: [PATCH 4/4] Make each entry type a different function --- src/Gitonomy/Git/Tree.php | 46 ++++++++++++++++++++++----- src/Gitonomy/Git/TreeType.php | 20 ------------ tests/Gitonomy/Git/Tests/TreeTest.php | 35 ++++++++++++++------ 3 files changed, 64 insertions(+), 37 deletions(-) delete mode 100644 src/Gitonomy/Git/TreeType.php diff --git a/src/Gitonomy/Git/Tree.php b/src/Gitonomy/Git/Tree.php index 1496aa05..7830cfa4 100644 --- a/src/Gitonomy/Git/Tree.php +++ b/src/Gitonomy/Git/Tree.php @@ -49,16 +49,16 @@ protected function initialize() $this->entries = []; $this->entriesByType = [ - TreeType::BLOB->value => [], - TreeType::TREE->value => [], - TreeType::COMMIT->value => [], + 'blob' => [], + 'tree' => [], + 'commit' => [], ]; foreach ($parser->entries as $entry) { list($mode, $type, $hash, $name) = $entry; - if ($type == TreeType::BLOB->value) { + if ($type == 'blob') { $treeEntry = [$mode, $this->repository->getBlob($hash)]; - } elseif ($type == TreeType::TREE->value) { + } elseif ($type == 'tree') { $treeEntry = [$mode, $this->repository->getTree($hash)]; } else { $treeEntry = [$mode, new CommitReference($hash)]; @@ -71,13 +71,43 @@ protected function initialize() } /** - * @return array An associative array name => $object + * @return array An associative array name => $object */ - public function getEntries(?TreeType $type = null) + public function getEntries(): array { $this->initialize(); - return $type ? $this->entriesByType[$type->value] : $this->entries; + return $this->entries; + } + + /** + * @return array An associative array of name => [mode, commit reference] + */ + public function getCommitReferenceEntries(): array + { + $this->initialize(); + + return $this->entriesByType['commit']; + } + + /** + * @return array An associative array of name => [mode, tree] + */ + public function getTreeEntries(): array + { + $this->initialize(); + + return $this->entriesByType['tree']; + } + + /** + * @return array An associative array of name => [mode, blob] + */ + public function getBlobEntries(): array + { + $this->initialize(); + + return $this->entriesByType['blob']; } public function getEntry($name) diff --git a/src/Gitonomy/Git/TreeType.php b/src/Gitonomy/Git/TreeType.php deleted file mode 100644 index d9b9f5c3..00000000 --- a/src/Gitonomy/Git/TreeType.php +++ /dev/null @@ -1,20 +0,0 @@ - - * (c) Julien DIDIER - * - * This source file is subject to the MIT license that is bundled - * with this source code in the file LICENSE. - */ - -namespace Gitonomy\Git; - -enum TreeType: string -{ - case BLOB = 'blob'; - case TREE = 'tree'; - case COMMIT = 'commit'; -} diff --git a/tests/Gitonomy/Git/Tests/TreeTest.php b/tests/Gitonomy/Git/Tests/TreeTest.php index 0358ffd7..b7ba7d32 100644 --- a/tests/Gitonomy/Git/Tests/TreeTest.php +++ b/tests/Gitonomy/Git/Tests/TreeTest.php @@ -14,7 +14,6 @@ use Gitonomy\Git\Blob; use Gitonomy\Git\CommitReference; -use Gitonomy\Git\TreeType; class TreeTest extends AbstractTest { @@ -39,21 +38,39 @@ public function testGetEntries($repository) /** * @dataProvider provideFooBar */ - public function testGetEntriesByType($repository) + public function testGetCommitReferenceEntries($repository) { $tree = $repository->getCommit(self::NO_MESSAGE_COMMIT)->getTree(); - $blobs = $tree->getEntries(TreeType::BLOB); + $commits = $tree->getCommitReferenceEntries(); - $this->assertNotEmpty($blobs['README.md'], 'README.md is present'); - $this->assertTrue($blobs['README.md'][1] instanceof Blob, 'README.md is a blob'); + $this->assertNotEmpty($commits['barbaz'], 'barbaz is present'); + $this->assertTrue($commits['barbaz'][1] instanceof CommitReference, 'barbaz is a Commit'); + } + + /** + * @dataProvider provideFooBar + */ + public function testGetTreeEntries($repository) + { + $tree = $repository->getCommit(self::NO_MESSAGE_COMMIT)->getTree(); + + $trees = $tree->getTreeEntries(); - $trees = $tree->getEntries(TreeType::TREE); $this->assertEmpty($trees); + } - $commits = $tree->getEntries(TreeType::COMMIT); - $this->assertNotEmpty($commits['barbaz'], 'barbaz is present'); - $this->assertTrue($commits['barbaz'][1] instanceof CommitReference, 'barbaz is a Commit'); + /** + * @dataProvider provideFooBar + */ + public function testGetBlobEntries($repository) + { + $tree = $repository->getCommit(self::NO_MESSAGE_COMMIT)->getTree(); + + $blobs = $tree->getBlobEntries(); + + $this->assertNotEmpty($blobs['README.md'], 'README.md is present'); + $this->assertTrue($blobs['README.md'][1] instanceof Blob, 'README.md is a blob'); } /**