Skip to content

Commit

Permalink
add --staged flag
Browse files Browse the repository at this point in the history
  • Loading branch information
utsavsomaiya committed Oct 30, 2024
1 parent 6ef7e3c commit 3797e51
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
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
8 changes: 8 additions & 0 deletions app/Contracts/PathsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@ 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');
});

0 comments on commit 3797e51

Please sign in to comment.