Skip to content

Commit

Permalink
Use merge-base (three dot diff) instead of direct diff for git-diff-b…
Browse files Browse the repository at this point in the history
  • Loading branch information
bdsl committed Jan 25, 2022
1 parent 4e21783 commit c73935b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Logger/GitHub/GitDiffFileProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function provide(string $gitDiffFilter, string $gitDiffBase): string
{
$filter = $this->shellCommandLineExecutor->execute(sprintf(
'git diff %s --diff-filter=%s --name-only | grep src/ | paste -s -d "," -',
escapeshellarg($gitDiffBase),
escapeshellarg($gitDiffBase . '...HEAD'),
escapeshellarg($gitDiffFilter)
));

Expand All @@ -71,7 +71,7 @@ public function provideWithLines(string $gitDiffBase): string
{
return $this->shellCommandLineExecutor->execute(sprintf(
"git diff %s --unified=0 --diff-filter=AM | grep -v -e '^[+-]' -e '^index'",
escapeshellarg($gitDiffBase)
escapeshellarg($gitDiffBase . '...HEAD')
));
}
}
24 changes: 22 additions & 2 deletions tests/phpunit/Logger/GitHub/GitDiffFileProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public function test_it_throws_no_code_to_mutate_exception_when_diff_is_empty():

public function test_it_executes_diff_and_returns_filter_as_a_string(): void
{
$expectedCommandLine = 'git diff \'master\' --diff-filter=\'AM\' --name-only | grep src/ | paste -s -d "," -';
$expectedCommandLine = 'git diff \'master...HEAD\' --diff-filter=\'AM\' --name-only | grep src/ | paste -s -d "," -';

if (PHP_OS_FAMILY === 'Windows') {
$expectedCommandLine = 'git diff "master" --diff-filter="AM" --name-only | grep src/ | paste -s -d "," -';
$expectedCommandLine = 'git diff "master...HEAD" --diff-filter="AM" --name-only | grep src/ | paste -s -d "," -';
}

$shellCommandLineExecutor = $this->createMock(ShellCommandLineExecutor::class);
Expand All @@ -75,4 +75,24 @@ public function test_it_executes_diff_and_returns_filter_as_a_string(): void

$this->assertSame('src/A.php,src/B.php', $filter);
}

public function test_it_provides_lines_filter_as_a_string(): void
{
$expectedCommandLine = 'git diff \'master...HEAD\' --unified=0 --diff-filter=AM | grep -v -e \'^[+-]\' -e \'^index\'';

if (PHP_OS_FAMILY === 'Windows') {
$expectedCommandLine = 'git diff "master...HEAD" --unified=0 --diff-filter=AM | grep -v -e \'^[+-]\' -e \'^index\'';
}

$shellCommandLineExecutor = $this->createMock(ShellCommandLineExecutor::class);
$shellCommandLineExecutor->expects($this->once())
->method('execute')
->with($expectedCommandLine)
->willReturn('<LINE BY LINE GIT DIFF>');

$diffProvider = new GitDiffFileProvider($shellCommandLineExecutor);
$filter = $diffProvider->provideWithLines('master');

$this->assertSame('<LINE BY LINE GIT DIFF>', $filter);
}
}

0 comments on commit c73935b

Please sign in to comment.