Skip to content

Commit

Permalink
Allow --diff-filter usage
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianfeldmann committed Jan 23, 2024
1 parent 4b77bd2 commit 5cb1ea9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Command/Log/ChangedFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ protected function getGitCommand(): string
{
return 'log --format=' . escapeshellarg('')
. ' --name-only'
. $this->diffFilter
. $this->author
. $this->merges
. $this->date
Expand Down
20 changes: 20 additions & 0 deletions src/Command/Log/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ abstract class Log extends Base
*/
protected string $format = '%h -%d %s (%ci) <%an>';

/**
* Diff filter types
* --diff-filter
*
* @var string
*/
protected string $diffFilter = '';

/**
* Include or hide merge commits.
* --no-merges
Expand Down Expand Up @@ -87,6 +95,18 @@ public function prettyFormat(string $format): Log
return $this;
}

/**
* Set the diff filter
*
* @param array<string> $filter
* @return $this
*/
public function withDiffFilter(array $filter): Log
{
$this->diffFilter = empty($filter) ? '' : ' --diff-filter=' . implode('', $filter);
return $this;
}

/**
* Define merge commit behaviour.
*
Expand Down
7 changes: 4 additions & 3 deletions src/Operator/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ class Log extends Base
/**
* Get the list of files that changed since a given revision.
*
* @param string $revision
* @param string $revision
* @param array<string> $filter
* @return array<string>
*/
public function getChangedFilesSince(string $revision): array
public function getChangedFilesSince(string $revision, array $filter = []): array
{
$cmd = (new ChangedFiles($this->repo->getRoot()))->byRange($revision);
$cmd = (new ChangedFiles($this->repo->getRoot()))->byRange($revision)->withDiffFilter($filter);
$result = $this->runner->run($cmd);

return $result->getBufferedOutput();
Expand Down
17 changes: 16 additions & 1 deletion tests/git/Command/Log/ChangedFilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
class ChangedFilesTest extends TestCase
{
/**
* Tests Commits::getCommand
* Tests ChangedFiles::getCommand
*/
public function testDefault()
{
Expand All @@ -36,4 +36,19 @@ public function testDefault()
$exe
);
}

/**
* Tests ChangedFiles::getCommand
*/
public function testWithFilter()
{
$cmd = new ChangedFiles();
$cmd->withDiffFilter(['A']);
$exe = $cmd->getCommand();

$this->assertEquals(
'git log --format=\'\' --name-only --diff-filter=A --no-merges',
$exe
);
}
}

0 comments on commit 5cb1ea9

Please sign in to comment.