Skip to content

2x ConsoleCommands

Wtyd edited this page Jun 6, 2024 · 1 revision

GitHooks is based on Laravel Zero Framework which is a micro-framework for console applications. So the way to launch commands in GitHooks is like Laravel's Artisan commands. You can run githooks list to see all available commands.

1. Conf

1.1. githooks conf:init

Creates the configuration file (githooks.yml) in the root of the project path. You can move to the qa directory. GitHooks will search these two paths for the file githooks.yml.

1.2. githooks conf:check

Check if githooks.yml is in root or qa path. Also check the file format. As the tools can have many options, although the command indicates that the format is correct, there may be errors. Despite this, it can help you correct some errors.

From 2.5.0 or higher, also, shows a summary of options and tools settings:

Conf:check

2. Hook

2.1. githooks hook [$hook=pre-commit] [$script]

Enable the execution of GitHooks in the precommit hook. Basically, copies the hooks/default.php script into the .git/hooks directory and rename it as pre-commit.

The hook command has two optional arguments:

  1. Hook (default pre-commit): the git hook where we want GitHooks or another script to run.
  2. CustomScript: your own script.

This way you can run any script when any git event occurs.

2.2. Set Your Own Script

This command copies the hooks/default.php. The content of this file is:

#!/bin/php
<?php

$backFiles = shell_exec('git diff --cached --name-only --diff-filter=ACM | grep ".php$\\|^composer.json$\\|^composer.lock$"');

if (!empty($backFiles)) {
    passthru('php vendor/bin/githooks tool all', $exit);

    exit($exit);
}

If, for example, you wanted to run phpunit after GitHooks you could create CustomPrecommit.php:

#!/bin/php
<?php

$backFiles = shell_exec('git diff --cached --name-only --diff-filter=ACM | grep ".php$\\|^composer.json$\\|^composer.lock$"');

if (!empty($backFiles)) {
    passthru('php vendor/bin/githooks tool all', $gitHooksExit);

    if (0 !== $gitHooksExit) {
        exit(1);
    }

    passthru('vendor/bin/phpunit tests/Unit/MyClassTest.php ', $phpunitExit);

    if (0 !== $phpunitExit) {
        echo "\n\e[41m\e[30mSome tests failed\033[0m\n";
        exit(1);
    }
}

Or something like this. Now I set like pre-commit with the command: githooks hook pre-commit MyCustomPrecommit.php.

To ensure that it is configured automatically, you can configure the command in the post-update-cmd and post-install-cmd events of the composer.json file (scripts section):

"scripts": {
    "post-update-cmd": [
      "Wtyd\\GitHooks\\Utils\\ComposerUpdater::php72orMinorUpdate",
      "vendor/bin/githooks hook pre-commit MyCustomPrecommit.php"
    ],
    "post-install-cmd": [
      "vendor/bin/githooks hook pre-commit MyCustomPrecommit.php"
    ]
}

2.3. githooks hook:clean [$hook=pre-commit]

Deletes the hook passed as argument (default pre-commit).

3. Tool

3.1. githooks tool $tool

Runs each tool separately in the way it is configured in githooks.yml:

githooks tool security-checker
githooks tool parallel-lint
githooks tool phpcpd
githooks tool phpcs
githooks tool phpcbf
githooks tool phpmd
githooks tool phpstan

This way GitHooks prints the command for run the tool under the hood and then runs it:

Imagen de una herramienta

Is posible run all the tools setted in githooks.yml at same time:

githooks tool all

3.1.1. Options

This command can be runned with a series of options that are used to override the values in the configuration file. These options are:

--ignoreErrorsOnExit[=IGNOREERRORSONEXIT]  Avoids exit error even if the tool finds some trobule. When tool is 'all' applies for all tools
--otherArguments[=OTHERARGUMENTS]          Other tool options not supported by GitHooks
--executablePath[=EXECUTABLEPATH]          Path to executable
--paths[=PATHS]                            Paths or files against the tool will be executed
--processes[=PROCESSES]                    Number of parallel processes in which the tools will be executed