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

Nggw 94 tabs eol #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions src/Action/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,17 @@ private function isEnabled(ActionConfig $action): bool
{
return $action->getOptions()->get('enabled', true);
}

public function getChangedFiles(Config\Action $action, Repository $repository): array
{
$excludedFiles = $action->getOptions()->get('excluded_files');
$extensions = $action->getOptions()->get('extensions', ['php', 'twig']);

$changedFiles = [];
foreach ($extensions as $extension) {
$changedFiles = [...$changedFiles, ...$repository->getIndexOperator()->getStagedFilesOfType($extension)];
}

return array_diff($changedFiles, $excludedFiles);
}
}
43 changes: 43 additions & 0 deletions src/Action/CheckForTabs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Netgen\GitHooks\Action;

use CaptainHook\App\Config;
use CaptainHook\App\Console\IO;
use SebastianFeldmann\Git\Repository;
use Symfony\Component\Process\Process;
use function array_diff;
use function implode;

final class CheckForTabs extends Action
{
protected const ERROR_MESSAGE = 'You have tabs in your code!';

protected function doExecute(Config $config, IO $io, Repository $repository, Config\Action $action): void
{
$files = $this->getChangedFiles($action, $repository);
if (empty($files)) {
return;
}

$arguments = array_merge(
[
'grep',
'$\t',
],
$files
);

$process = new Process($arguments);

$process->run();
$process->wait();

if ($process->getOutput()) {
$io->writeError("<error>{$process->getOutput()}</error>");
$this->throwError($action, $io);
}
}
}
43 changes: 43 additions & 0 deletions src/Action/CheckForWhiteSpaceAtEOL.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Netgen\GitHooks\Action;

use CaptainHook\App\Config;
use CaptainHook\App\Console\IO;
use SebastianFeldmann\Git\Repository;
use Symfony\Component\Process\Process;
use function array_diff;
use function implode;

final class CheckForWhiteSpaceAtEOL extends Action
{
protected const ERROR_MESSAGE = 'You have white spaces at EOL!';

protected function doExecute(Config $config, IO $io, Repository $repository, Config\Action $action): void
{
$files = $this->getChangedFiles($action, $repository);
if (empty($files)) {
return;
}

$arguments = array_merge(
[
'grep',
'[[:blank:]]$',
],
$files
);

$process = new Process($arguments);

$process->run();
$process->wait();

if ($process->getOutput()) {
$io->writeError("<error>{$process->getOutput()}</error>");
$this->throwError($action, $io);
}
}
}