igorsgm / laravel-git-hooks

🪝 • Efficiently manage Git hooks in Laravel projects. Enhance code quality, save time on reviews, and prevent bugs from entering your repository.
MIT License
17 stars 6 forks source link

pre-push hook error - no arguments expected #31

Open Rasmus-Bertell opened 4 months ago

Rasmus-Bertell commented 4 months ago

I have a pre-push hook which runs my tests and it works with php artisan git-hooks:pre-push command, but when actually pushing with the command git push I get the following error:

$ git push

  No arguments expected for "git-hooks:pre-push" command, got "origin".

error: failed to push some refs to 'https://************************************'
software version
PHP 8.3.6
git 2.45.0
Laravel Framework 11.5.0
laravel-git-hooks 1.3.0
Rasmus-Bertell commented 4 months ago

And my pre-push hook looks like this, it's a very simple implementation but works for now.

<?php

namespace App\Console\GitHooks;

use Closure;
use Igorsgm\GitHooks\Contracts\PrePushHook;
use Igorsgm\GitHooks\Exceptions\HookFailException;
use Igorsgm\GitHooks\Git\Log;
use Illuminate\Support\Facades\Artisan;

class PestPrePushHook implements PrePushHook
{
    /**
     * Get the name of the hook.
     */
    public function getName(): ?string
    {
        return 'Pest Pre Push Hook';
    }

    /**
     * Execute the Hook.
     *
     * @param  Log  $log
     * @param  Closure  $next The next hook in the chain to execute.
     * @return mixed|null
     */
    public function handle(Log $log, Closure $next)
    {
        $return = Artisan::call("test");

        if ($return !== 0) {
            throw new HookFailException("Tests failed");
        }

        // Run the next hook in the chain
        return $next($log);
    }
}

And .git/hooks/pre-push file contents

#!/bin/sh
# Detect whether /dev/tty is available & functional
if sh -c ": >/dev/tty" >/dev/null 2>/dev/null; then
    exec < /dev/tty
fi

php /home/rasmus/xxxxxxxx/xxxxxxxxxxxxxxxxxxxx/artisan git-hooks:pre-push $@ >&2
Rasmus-Bertell commented 4 months ago

Seems like this is the same error as in #24 sorry for opening a new issue about the same error. I thought I looked through the open issues but I must have been tired or something.

mauricekindermann commented 4 months ago

I have exactly the same issue. I thought I was going crazy. For whatever reason - pre-commit works just fine. But pre-push always runs into this issue no matter how I try circumvent it.

Rasmus-Bertell commented 4 months ago

Just a note for anyone encountering this issue, you can circumvent the hooks with --no-verify flag e.g. git push --no-verify, if you don't want to disable the hooks while waiting for a fix to this.

Rasmus-Bertell commented 4 months ago

Based on https://git-scm.com/docs/githooks#_pre_push.

The hook is called with two parameters which provide the name and location of the destination remote, if a named remote is not being used both values will be the same.

I think this could fixed with the following but I haven't had the chance to test it yet. If it works I'll create a PR, there might be some other hooks affected by the same bug also.

https://github.com/igorsgm/laravel-git-hooks/blob/c42045f67905e7f7de2e70e34d27e25e494585f1/src/Console/Commands/PrePush.php#L18

      *
      * @var string
      */
-    protected $signature = 'git-hooks:pre-push';
+    protected $signature = 'git-hooks:pre-push {remote?} {url?}';

     /**
      * The console command description.
brunogoossens commented 1 day ago

same for me

Rasmus-Bertell commented 21 hours ago

I completely forgot about this, but I'll create a PR this weekend to fix this issue.