Skip to content

Commit

Permalink
Fix Tree command. (#69)
Browse files Browse the repository at this point in the history
The code to invoke the `git ls-tree` command was wrong, and resulted in
the nonexisting command `git tree ls-tree` being invoked instead. This
patch fixes the issue so the tree command can be used again.
  • Loading branch information
snake66 authored Oct 9, 2024
1 parent 1b9c2c7 commit dea89a6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Command/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __invoke(string $branch = 'master', string $path = ''): array
{
$objects = [];
$builder = $this->getCommandBuilder();
$builder->add('ls-tree')->add($branch . ':' . $path);
$builder->add($branch . ':' . $path);

$output = $this->run($builder);

Expand All @@ -73,4 +73,9 @@ public function __invoke(string $branch = 'master', string $path = ''): array

return $objects;
}

public function getCommandName(): string
{
return 'ls-tree';
}
}
54 changes: 54 additions & 0 deletions test/Command/TreeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php declare(strict_types=1);
/**
* phpGit - A Git wrapper for PHP
*
* @author https://github.com/inhere
* @link https://github.com/phppkg/phpgit
* @license MIT
*/

namespace PhpGitTest\Command;

use PhpGit\Exception\GitException;
use PhpGit\Git;
use PhpGitTest\BasePhpGitTestCase;
use Symfony\Component\Filesystem\Filesystem;


class TreeTest extends BasePhpGitTestCase
{
public function testListBranch(): void
{
$filesystem = new Filesystem();

$git = new Git();
$git->init($this->directory);
$git->setRepository($this->directory);
$filesystem->dumpFile($this->directory . '/README.md', 'hello');
$git->add('.');
$git->commit('Initial commit');

$result = $git->tree('master');

$this->assertIsArray($result);
$this->assertCount(1, $result);

$inner = array_shift($result);

$this->assertIsArray($inner);
$this->assertArrayHasKey('sort', $inner);
$this->assertEquals('2:README.md', $inner['sort']);

$this->assertArrayHasKey('type', $inner);
$this->assertEquals('blob', $inner['type']);

$this->assertArrayHasKey('mode', $inner);
$this->assertEquals(100644, $inner['mode']);

$this->assertArrayHasKey('hash', $inner);
$this->assertEquals('b6fc4c620b67d95f953a5c1c1230aaab5db5a1b0', $inner['hash']);

$this->assertArrayHasKey('file', $inner);
$this->assertEquals('README.md', $inner['file']);
}
}

0 comments on commit dea89a6

Please sign in to comment.