Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --staged option #302

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/Commands/DefaultCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ protected function configure()
new InputOption('bail', '', InputOption::VALUE_NONE, 'Test for code style errors without fixing them and stop on first error'),
new InputOption('repair', '', InputOption::VALUE_NONE, 'Fix code style errors but exit with status 1 if there were any changes made'),
new InputOption('dirty', '', InputOption::VALUE_NONE, 'Only fix files that have uncommitted changes'),
new InputOption('staged', 's', InputOption::VALUE_NONE, 'Only fix files that have staged changes'),
new InputOption('format', '', InputOption::VALUE_REQUIRED, 'The output format that should be used'),
new InputOption('cache-file', '', InputArgument::OPTIONAL, 'The path to the cache file'),
]
Expand Down
7 changes: 7 additions & 0 deletions app/Contracts/PathsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@ interface PathsRepository
* @return array<int, string>
*/
public function dirty();

/**
* Determine the "staged" files.
*
* @return array<int, string>
*/
public function staged();
}
20 changes: 20 additions & 0 deletions app/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public static function paths($input)
return static::resolveDirtyPaths();
}

if ($input->getOption('staged')) {
return static::resolveStagedPaths();
}

return $input->getArgument('path');
}

Expand Down Expand Up @@ -46,4 +50,20 @@ public static function resolveDirtyPaths()

return $files;
}

/**
* Resolves the staged paths, if any.
*
* @return array<int, string>
*/
public static function resolveStagedPaths()
{
$files = app(PathsRepository::class)->staged();

if (empty($files)) {
abort(0, 'No staged files found.');
}

return $files;
}
}
32 changes: 32 additions & 0 deletions app/Repositories/GitPathsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,36 @@ public function dirty()

return array_values(array_intersect($files, $dirtyFiles));
}

/**
* {@inheritDoc}
*/
public function staged()
{
$process = tap(new Process(['git', 'diff', '--cached', '--name-only', '--diff-filter=ACM', '--', '**.php']))->run();

if (! $process->isSuccessful()) {
abort(1, 'The [--staged] option is only available when using Git.');
}

$stagedFiles = collect(preg_split('/\R+/', $process->getOutput(), flags: PREG_SPLIT_NO_EMPTY))
->map(function ($file) {
if (PHP_OS_FAMILY === 'Windows') {
$file = str_replace('/', DIRECTORY_SEPARATOR, $file);
}

return $this->path.DIRECTORY_SEPARATOR.$file;
})
->values()
->all();

$files = array_values(array_map(function ($splFile) {
return $splFile->getPathname();
}, iterator_to_array(ConfigurationFactory::finder()
->in($this->path)
->files()
)));

return array_values(array_intersect($files, $stagedFiles));
}
}
63 changes: 63 additions & 0 deletions tests/Feature/StagedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

use App\Contracts\PathsRepository;

it('determines staged files', function () {
$paths = Mockery::mock(PathsRepository::class);

$paths
->shouldReceive('staged')
->once()
->andReturn([
base_path('tests/Fixtures/without-issues-laravel/file.php'),
]);

$this->swap(PathsRepository::class, $paths);

[$statusCode, $output] = run('default', ['--staged' => true]);

expect($statusCode)->toBe(0)
->and($output)
->toContain('── Laravel', ' 1 file');
});

it('ignores the path argument', function () {
$paths = Mockery::mock(PathsRepository::class);

$paths
->shouldReceive('staged')
->once()
->andReturn([
base_path('tests/Fixtures/without-issues-laravel/file.php'),
]);

$this->swap(PathsRepository::class, $paths);

[$statusCode, $output] = run('default', [
'-s' => true,
'path' => base_path(),
]);

expect($statusCode)->toBe(0)
->and($output)
->toContain('── Laravel', ' 1 file');
});

it('does not abort when there are no staged files', function () {
$paths = Mockery::mock(PathsRepository::class);

$paths
->shouldReceive('staged')
->once()
->andReturn([]);

$this->swap(PathsRepository::class, $paths);

[$statusCode, $output] = run('default', [
'--staged' => true,
]);

expect($statusCode)->toBe(0)
->and($output)
->toContain('── Laravel', ' 0 files');
});