Skip to content

Commit

Permalink
Merge pull request #30 from ramsey
Browse files Browse the repository at this point in the history
Implement command to checkout/restore working tree
  • Loading branch information
sebastianfeldmann authored Apr 10, 2021
2 parents c9d9e00 + 2a8d7c3 commit 43bdd92
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/Command/Checkout/RestoreWorkingTree.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* This file is part of SebastianFeldmann\Git.
*
* (c) Sebastian Feldmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace SebastianFeldmann\Git\Command\Checkout;

use SebastianFeldmann\Git\Command\Base;

/**
* Class RestoreWorkingTree
*
* @package SebastianFeldmann\Git
* @author Sebastian Feldmann <[email protected]>
* @link https://github.com/sebastianfeldmann/git
* @since Class available since Release 3.7.0
*/
class RestoreWorkingTree extends Base
{
/**
* Files and directories to restore.
*
* @var string[]
*/
private $files = ['.'];

/**
* Limits the paths affected by the operation to those specified here.
*
* @param array $files
*
* @return \SebastianFeldmann\Git\Command\Checkout\RestoreWorkingTree
*/
public function files(array $files): RestoreWorkingTree
{
$this->files = $files;
return $this;
}

/**
* Return the command to execute.
*
* @return string
*/
protected function getGitCommand(): string
{
return 'checkout --quiet'
. ' -- '
. implode(' ', array_map('escapeshellarg', $this->files));
}
}
17 changes: 17 additions & 0 deletions src/Operator/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace SebastianFeldmann\Git\Operator;

use SebastianFeldmann\Git\Command\Checkout\RestoreWorkingTree;
use SebastianFeldmann\Git\Command\Status\WorkingTreeStatus;
use SebastianFeldmann\Git\Command\Status\Porcelain\PathList;

Expand All @@ -37,4 +38,20 @@ public function getWorkingTreeStatus(): iterable

return $result->getFormattedOutput();
}

/**
* Performs a checkout (restore) operation on the given paths
* (or the entire repo, by default).
*
* @param string[] $limitToPaths
* @return bool
*/
public function restoreWorkingTree(array $limitToPaths = ['.']): bool
{
$cmd = (new RestoreWorkingTree($this->repo->getRoot()))->files($limitToPaths);

$result = $this->runner->run($cmd);

return $result->isSuccessful();
}
}
48 changes: 48 additions & 0 deletions tests/git/Command/Checkout/RestoreWorkingTreeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* This file is part of SebastianFeldmann\Git.
*
* (c) Sebastian Feldmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace SebastianFeldmann\Git\Command\Checkout;

use PHPUnit\Framework\TestCase;

/**
* Class RestoreWorkingTreeTest
*
* @package SebastianFeldmann\Git
* @author Sebastian Feldmann <[email protected]>
* @link https://github.com/sebastianfeldmann/git
* @since Class available since Release 3.7.0
*/
class RestoreWorkingTreeTest extends TestCase
{
public function testDefault(): void
{
$command = new RestoreWorkingTree();

$this->assertSame('git checkout --quiet -- \'.\'', $command->getCommand());
}

public function testFiles(): void
{
$command = new RestoreWorkingTree();
$command = $command->files([
"foo/*",
"foo bar.txt",
"foo' 'bar.txt",
"fooBar.txt",
]);

$this->assertSame(
"git checkout --quiet -- 'foo/*' 'foo bar.txt' 'foo'\'' '\''bar.txt' 'fooBar.txt'",
$command->getCommand()
);
}
}
43 changes: 43 additions & 0 deletions tests/git/Operator/StatusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use SebastianFeldmann\Cli\Command\Result as CommandResult;
use SebastianFeldmann\Cli\Command\Runner\Result as RunnerResult;
use SebastianFeldmann\Git\Command\Checkout\RestoreWorkingTree;
use SebastianFeldmann\Git\Command\Status\Porcelain\PathList;
use SebastianFeldmann\Git\Command\Status\WorkingTreeStatus;
use SebastianFeldmann\Git\Status\Path;
Expand Down Expand Up @@ -57,4 +58,46 @@ public function testGetWorkingTreeStatus(): void
$this->assertCount(2, $paths);
$this->assertContainsOnlyInstancesOf(Path::class, $paths);
}

public function testRestoreWorkingTreeWithDefaultPathsParameter(): void
{
$root = (string) realpath(__FILE__ . '/../../..');

$repo = $this->getRepoMock();
$runner = $this->getRunnerMock();
$cmdRes = new CommandResult('git ...', 0);
$runRes = new RunnerResult($cmdRes, ['foobar']);
$gitCmd = new RestoreWorkingTree($root);

$repo->method('getRoot')->willReturn($root);
$runner->expects($this->once())
->method('run')
->with($this->equalTo($gitCmd))
->willReturn($runRes);

$status = new Status($runner, $repo);

$this->assertTrue($status->restoreWorkingTree());
}

public function testRestoreWorkingTreeWithPassedPathsAndErrorResponse(): void
{
$root = (string) realpath(__FILE__ . '/../../..');

$repo = $this->getRepoMock();
$runner = $this->getRunnerMock();
$cmdRes = new CommandResult('git ...', 1);
$runRes = new RunnerResult($cmdRes, ['foobar']);
$gitCmd = (new RestoreWorkingTree($root))->files(['foo', 'bar']);

$repo->method('getRoot')->willReturn($root);
$runner->expects($this->once())
->method('run')
->with($this->equalTo($gitCmd))
->willReturn($runRes);

$status = new Status($runner, $repo);

$this->assertFalse($status->restoreWorkingTree(['foo', 'bar']));
}
}

0 comments on commit 43bdd92

Please sign in to comment.