Skip to content

Commit eefc52c

Browse files
committed
Add optional entry type option for tree entries
1 parent 640068c commit eefc52c

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

src/Gitonomy/Git/Tree.php

Lines changed: 15 additions & 7 deletions
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,16 +48,23 @@ protected function initialize()
4748
$parser->parse($output);
4849

4950
$this->entries = [];
51+
$this->entriesByType = [
52+
TreeType::BLOB->value => [],
53+
TreeType::TREE->value => [],
54+
TreeType::COMMIT->value => [],
55+
];
5056

5157
foreach ($parser->entries as $entry) {
5258
list($mode, $type, $hash, $name) = $entry;
53-
if ($type == 'blob') {
54-
$this->entries[$name] = [$mode, $this->repository->getBlob($hash)];
55-
} elseif ($type == 'tree') {
56-
$this->entries[$name] = [$mode, $this->repository->getTree($hash)];
59+
if ($type == TreeType::BLOB->value) {
60+
$treeEntry = [$mode, $this->repository->getBlob($hash)];
61+
} elseif ($type == TreeType::TREE->value) {
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;
@@ -65,11 +73,11 @@ protected function initialize()
6573
/**
6674
* @return array An associative array name => $object
6775
*/
68-
public function getEntries()
76+
public function getEntries(?TreeType $type = null)
6977
{
7078
$this->initialize();
7179

72-
return $this->entries;
80+
return $type ? $this->entriesByType[$type->value] : $this->entries;
7381
}
7482

7583
public function getEntry($name)

src/Gitonomy/Git/TreeType.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Gitonomy.
5+
*
6+
* (c) Alexandre Salomé <[email protected]>
7+
* (c) Julien DIDIER <[email protected]>
8+
*
9+
* This source file is subject to the MIT license that is bundled
10+
* with this source code in the file LICENSE.
11+
*/
12+
13+
namespace Gitonomy\Git;
14+
15+
enum TreeType: string
16+
{
17+
case BLOB = 'blob';
18+
case TREE = 'tree';
19+
case COMMIT = 'commit';
20+
}

tests/Gitonomy/Git/Tests/TreeTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
namespace Gitonomy\Git\Tests;
1414

1515
use Gitonomy\Git\Blob;
16+
use Gitonomy\Git\CommitReference;
17+
use Gitonomy\Git\TreeType;
1618

1719
class TreeTest extends AbstractTest
1820
{
@@ -21,7 +23,7 @@ class TreeTest extends AbstractTest
2123
/**
2224
* @dataProvider provideFooBar
2325
*/
24-
public function testCase($repository)
26+
public function testGetEntries($repository)
2527
{
2628
$tree = $repository->getCommit(self::LONGFILE_COMMIT)->getTree();
2729

@@ -34,6 +36,26 @@ public function testCase($repository)
3436
$this->assertTrue($entries['README.md'][1] instanceof Blob, 'README.md is a Blob');
3537
}
3638

39+
/**
40+
* @dataProvider provideFooBar
41+
*/
42+
public function testGetEntriesByType($repository)
43+
{
44+
$tree = $repository->getCommit(self::NO_MESSAGE_COMMIT)->getTree();
45+
46+
$blobs = $tree->getEntries(TreeType::BLOB);
47+
48+
$this->assertNotEmpty($blobs['README.md'], 'README.md is present');
49+
$this->assertTrue($blobs['README.md'][1] instanceof Blob, 'README.md is a blob');
50+
51+
$trees = $tree->getEntries(TreeType::TREE);
52+
$this->assertEmpty($trees);
53+
54+
$commits = $tree->getEntries(TreeType::COMMIT);
55+
$this->assertNotEmpty($commits['barbaz'], 'barbaz is present');
56+
$this->assertTrue($commits['barbaz'][1] instanceof CommitReference, 'barbaz is a Commit');
57+
}
58+
3759
/**
3860
* @dataProvider provideFooBar
3961
*/

0 commit comments

Comments
 (0)