Skip to content

Commit 5a47e03

Browse files
Add optional entry type option for tree entries (#221)
* Add optional entry type option for tree entries * Fix cs problems * Update test for diff range counts * Make each entry type a different function
1 parent de32cc0 commit 5a47e03

File tree

3 files changed

+84
-7
lines changed

3 files changed

+84
-7
lines changed

src/Gitonomy/Git/Tree.php

+43-5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Tree
2424
protected $hash;
2525
protected $isInitialized = false;
2626
protected $entries;
27+
protected $entriesByType;
2728

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

4950
$this->entries = [];
51+
$this->entriesByType = [
52+
'blob' => [],
53+
'tree' => [],
54+
'commit' => [],
55+
];
5056

5157
foreach ($parser->entries as $entry) {
5258
list($mode, $type, $hash, $name) = $entry;
5359
if ($type == 'blob') {
54-
$this->entries[$name] = [$mode, $this->repository->getBlob($hash)];
60+
$treeEntry = [$mode, $this->repository->getBlob($hash)];
5561
} elseif ($type == 'tree') {
56-
$this->entries[$name] = [$mode, $this->repository->getTree($hash)];
62+
$treeEntry = [$mode, $this->repository->getTree($hash)];
5763
} else {
58-
$this->entries[$name] = [$mode, new CommitReference($hash)];
64+
$treeEntry = [$mode, new CommitReference($hash)];
5965
}
66+
$this->entries[$name] = $treeEntry;
67+
$this->entriesByType[$type][$name] = $treeEntry;
6068
}
6169

6270
$this->isInitialized = true;
6371
}
6472

6573
/**
66-
* @return array An associative array name => $object
74+
* @return array<string, array{string, CommitReference|Tree|Blob}> An associative array name => $object
6775
*/
68-
public function getEntries()
76+
public function getEntries(): array
6977
{
7078
$this->initialize();
7179

7280
return $this->entries;
7381
}
7482

83+
/**
84+
* @return array<string, array{string, CommitReference}> An associative array of name => [mode, commit reference]
85+
*/
86+
public function getCommitReferenceEntries(): array
87+
{
88+
$this->initialize();
89+
90+
return $this->entriesByType['commit'];
91+
}
92+
93+
/**
94+
* @return array<string, array{string, Tree}> An associative array of name => [mode, tree]
95+
*/
96+
public function getTreeEntries(): array
97+
{
98+
$this->initialize();
99+
100+
return $this->entriesByType['tree'];
101+
}
102+
103+
/**
104+
* @return array<string, array{string, Blob}> An associative array of name => [mode, blob]
105+
*/
106+
public function getBlobEntries(): array
107+
{
108+
$this->initialize();
109+
110+
return $this->entriesByType['blob'];
111+
}
112+
75113
public function getEntry($name)
76114
{
77115
$this->initialize();

tests/Gitonomy/Git/Tests/DiffTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public function testDiffRangeParse($repository)
139139
$this->assertSame(0, $changes[0]->getRangeOldCount());
140140

141141
$this->assertSame(1, $changes[0]->getRangeNewStart());
142-
$this->assertSame(0, $changes[0]->getRangeNewCount());
142+
$this->assertSame(1, $changes[0]->getRangeNewCount());
143143
}
144144

145145
/**

tests/Gitonomy/Git/Tests/TreeTest.php

+40-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace Gitonomy\Git\Tests;
1414

1515
use Gitonomy\Git\Blob;
16+
use Gitonomy\Git\CommitReference;
1617

1718
class TreeTest extends AbstractTest
1819
{
@@ -21,7 +22,7 @@ class TreeTest extends AbstractTest
2122
/**
2223
* @dataProvider provideFooBar
2324
*/
24-
public function testCase($repository)
25+
public function testGetEntries($repository)
2526
{
2627
$tree = $repository->getCommit(self::LONGFILE_COMMIT)->getTree();
2728

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

38+
/**
39+
* @dataProvider provideFooBar
40+
*/
41+
public function testGetCommitReferenceEntries($repository)
42+
{
43+
$tree = $repository->getCommit(self::NO_MESSAGE_COMMIT)->getTree();
44+
45+
$commits = $tree->getCommitReferenceEntries();
46+
47+
$this->assertNotEmpty($commits['barbaz'], 'barbaz is present');
48+
$this->assertTrue($commits['barbaz'][1] instanceof CommitReference, 'barbaz is a Commit');
49+
}
50+
51+
/**
52+
* @dataProvider provideFooBar
53+
*/
54+
public function testGetTreeEntries($repository)
55+
{
56+
$tree = $repository->getCommit(self::NO_MESSAGE_COMMIT)->getTree();
57+
58+
$trees = $tree->getTreeEntries();
59+
60+
$this->assertEmpty($trees);
61+
}
62+
63+
/**
64+
* @dataProvider provideFooBar
65+
*/
66+
public function testGetBlobEntries($repository)
67+
{
68+
$tree = $repository->getCommit(self::NO_MESSAGE_COMMIT)->getTree();
69+
70+
$blobs = $tree->getBlobEntries();
71+
72+
$this->assertNotEmpty($blobs['README.md'], 'README.md is present');
73+
$this->assertTrue($blobs['README.md'][1] instanceof Blob, 'README.md is a blob');
74+
}
75+
3776
/**
3877
* @dataProvider provideFooBar
3978
*/

0 commit comments

Comments
 (0)