Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional entry type option for tree entries #221

Merged
merged 5 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading