captainhookphp / captainhook

CaptainHook is a very flexible git hook manager for software developers that makes sharing git hooks with your team a breeze.

Home Page:http://captainhook.info

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ability to ignore deleted files in `$STAGED_FILES`

Wirone opened this issue · comments

Currently we're ignoring deleted files on git diff level and then we're using xargs for passing to actual command. It's required because running tools (e.g. PHPStan) for non-existing files would cause errors.

{
    "pre-commit": {
        "enabled": true,
        "actions": [
            {
                "action": "git diff --diff-filter=ACMR --name-only --staged -- '*.php' | xargs -r vendor/bin/phpstan --ansi analyse --memory-limit=5G"
            }
        ]
    }
}

Usage of $STAGED_FILES probably would be cleaner BUT currently it's not possible to use it since its options does not allow ignoring removed files.

PS. CaptainHook\App\Runner\Action\Cli\Command\Placeholder\StagedFiles has invalid Class UpdatedFiles in phpDoc 😉

The IndexOperator should ignore deleted files by default.

$cmd         = new GetStagedFiles($this->repo->getRoot());
$formatter   = new FilterByStatus(['A', 'M']);
$result      = $this->runner->run($cmd, $formatter);
$this->files = $result->getFormattedOutput();

The FilterByStatus should remove all D prefixed lines. So no deleted files should be returned in the result.
In my tests no deleted files were returned.

@sebastianfeldmann Thanks for pointing to index operator, unfortunately I did not look there before - my bad. I've tested it and it works with deleted files (these are not listed in $STAGED_FILES). But at this point I won't migrate from explicit git diff because we're using --diff-filter=ACMR and since Index::resolveFiles() uses only AM it won't be compatible with our current approach. I think at least R should be handled (because moving files without modifying them could lead to static analysis errors too), maybe it's possible to make it configurable?

I totally agree that we should use ACMR by default. I will change that in the upcoming version and after that I will add an option to make it configurable.

I tagged a new version (3.8.1) for the git dependency.
From now on Index::getStagedFiles(array $diffFilter) uses the [A, C, M, R] as its default.

If you installed CaptainHook via Composer a simple composer update should do the trick.
If you installed the PHAR version I will tag a new version as soon as I added the configuration option.

I added the option to configure the placeholder for your needs

{$STAGED_FILES|diff-filter:ACMR}

But ACMR is the default for now anyways :)

Thanks for the suggestion and pointing out that I missed C and R.

Thank you very much! 🍻