Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

pre-push hook error - no arguments expected #31

Open
Rasmus-Bertell opened this issue May 2, 2024 · 7 comments · May be fixed by #34
Open

pre-push hook error - no arguments expected #31

Rasmus-Bertell opened this issue May 2, 2024 · 7 comments · May be fixed by #34

Comments

@Rasmus-Bertell
Copy link

Rasmus-Bertell commented May 2, 2024

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
Copy link
Author

Rasmus-Bertell commented May 2, 2024

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
Copy link
Author

Rasmus-Bertell commented May 3, 2024

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
Copy link

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
Copy link
Author

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
Copy link
Author

Rasmus-Bertell commented May 12, 2024

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.

protected $signature = 'git-hooks:pre-push';

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

@brunogoossens
Copy link

same for me

@Rasmus-Bertell
Copy link
Author

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

Rasmus-Bertell added a commit to Rasmus-Bertell/laravel-git-hooks that referenced this issue Sep 15, 2024
If the commands are run with extra arguments without them being defined
the command fails. This fix allows calling the `pre-push` and
`prepare-commit-msg` hooks with all their arguments while still
retaining the old behavior since the arguments are optional.

Reference: https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks

closes igorsgm#31
@Rasmus-Bertell Rasmus-Bertell linked a pull request Sep 15, 2024 that will close this issue
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants