Skip to content

Commit

Permalink
Add optional entry type option for tree entries (#221)
Browse files Browse the repository at this point in the history
* Add optional entry type option for tree entries

* Fix cs problems

* Update test for diff range counts

* Make each entry type a different function
  • Loading branch information
Patrick-Beuks committed May 6, 2024
1 parent de32cc0 commit 5a47e03
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 7 deletions.
48 changes: 43 additions & 5 deletions src/Gitonomy/Git/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Tree
protected $hash;
protected $isInitialized = false;
protected $entries;
protected $entriesByType;

public function __construct(Repository $repository, $hash)
{
Expand All @@ -47,31 +48,68 @@ protected function initialize()
$parser->parse($output);

$this->entries = [];
$this->entriesByType = [
'blob' => [],
'tree' => [],
'commit' => [],
];

foreach ($parser->entries as $entry) {
list($mode, $type, $hash, $name) = $entry;
if ($type == 'blob') {
$this->entries[$name] = [$mode, $this->repository->getBlob($hash)];
$treeEntry = [$mode, $this->repository->getBlob($hash)];
} elseif ($type == 'tree') {
$this->entries[$name] = [$mode, $this->repository->getTree($hash)];
$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;
}

/**
* @return array An associative array name => $object
* @return array<string, array{string, CommitReference|Tree|Blob}> An associative array name => $object
*/
public function getEntries()
public function getEntries(): array
{
$this->initialize();

return $this->entries;
}

/**
* @return array<string, array{string, CommitReference}> An associative array of name => [mode, commit reference]
*/
public function getCommitReferenceEntries(): array
{
$this->initialize();

return $this->entriesByType['commit'];
}

/**
* @return array<string, array{string, Tree}> An associative array of name => [mode, tree]
*/
public function getTreeEntries(): array
{
$this->initialize();

return $this->entriesByType['tree'];
}

/**
* @return array<string, array{string, Blob}> An associative array of name => [mode, blob]
*/
public function getBlobEntries(): array
{
$this->initialize();

return $this->entriesByType['blob'];
}

public function getEntry($name)
{
$this->initialize();
Expand Down
2 changes: 1 addition & 1 deletion tests/Gitonomy/Git/Tests/DiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down
41 changes: 40 additions & 1 deletion tests/Gitonomy/Git/Tests/TreeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Gitonomy\Git\Tests;

use Gitonomy\Git\Blob;
use Gitonomy\Git\CommitReference;

class TreeTest extends AbstractTest
{
Expand All @@ -21,7 +22,7 @@ class TreeTest extends AbstractTest
/**
* @dataProvider provideFooBar
*/
public function testCase($repository)
public function testGetEntries($repository)
{
$tree = $repository->getCommit(self::LONGFILE_COMMIT)->getTree();

Expand All @@ -34,6 +35,44 @@ public function testCase($repository)
$this->assertTrue($entries['README.md'][1] instanceof Blob, 'README.md is a Blob');
}

/**
* @dataProvider provideFooBar
*/
public function testGetCommitReferenceEntries($repository)
{
$tree = $repository->getCommit(self::NO_MESSAGE_COMMIT)->getTree();

$commits = $tree->getCommitReferenceEntries();

$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();

$this->assertEmpty($trees);
}

/**
* @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');
}

/**
* @dataProvider provideFooBar
*/
Expand Down

0 comments on commit 5a47e03

Please sign in to comment.