Skip to content
This repository has been archived by the owner on Jul 17, 2021. It is now read-only.

Commit

Permalink
create diff option to only check the currently staged files
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-feek committed Sep 15, 2017
1 parent 2ff336c commit 62818f6
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/Commands/CommitHooks/CommitHookCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@ abstract class CommitHookCommand extends Command
public function handle()
{
foreach (config($this->getConfigKey()) as $command) {
$this->info('git hook invoking: ' . $command);
$statusCode = $this->call($command);
// these commands in the config might be the command name + options.
$parts = explode(' ', $command, 2);
$commandName = $parts[0];
$arguments = $parts[1] ? explode(' ', $parts[1]) : [];

$this->line('git hook invoking: ' . $commandName . ' ' . implode($arguments, ' '));

$statusCode = $this->call($commandName, $arguments);

if ($statusCode !== 0) {
$this->error('git hook check failed');
return $statusCode;
}
}
Expand Down
26 changes: 24 additions & 2 deletions src/Commands/PHPCodeSnifferCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Feek\LaravelGitHooks\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;

abstract class PHPCodeSnifferCommand extends Command
{
Expand All @@ -29,19 +30,40 @@ abstract function getErrorMessage();
*/
abstract function getSuccessMessage();

/**
* @return array
*/
public function getOptions()
{
return [
['diff', null, InputOption::VALUE_OPTIONAL, 'only pass the currently staged files']
];
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$dirToCheckStyle = app_path();
$filesToCheck = app_path();

if ($this->option('diff')) {
// only check the current files that are staged
exec(
'git diff --cached --name-only',
$filesToCheck
);

$filesToCheck = implode($filesToCheck, ' ');
}

$executable = $this->getCodeSnifferExecutable();
$standard = $this->getCodingStandard();

exec(
"$executable -p --standard=$standard $dirToCheckStyle",
"$executable -p --standard=$standard $filesToCheck",
$output,
$statusCode
);
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Phpcbf.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Phpcbf extends PHPCodeSnifferCommand
*
* @var string
*/
protected $signature = 'hooks:phpcbf';
protected $signature = 'hooks:phpcbf {--diff}';

/**
* The console command description.
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Phpcs.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Phpcs extends PHPCodeSnifferCommand
*
* @var string
*/
protected $signature = 'hooks:phpcs';
protected $signature = 'hooks:phpcs {--diff}';

/**
* The console command description.
Expand Down
2 changes: 1 addition & 1 deletion src/Config/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
| commit will fail. Add as many or few as you want.
*/
'pre-commit' => [
'hooks:phpcs',
'hooks:phpcs --diff',
],

/*
Expand Down

0 comments on commit 62818f6

Please sign in to comment.