Nicolas Widart
Nicolas Widart

Reputation: 1287

Git hook not running 2 consecutive commands

I'm trying to create a pre-push hook to run php-cs-fixer before every push.

This is the current pre-push hook I have:

#!/bin/sh

exec php-cs-fixer fix . --config-file=".php_cs" && git commit -am 'PSR-2'

The first command gets trigger without a problem. Only the git commit -am 'PSR-2 doesn't. To be more precise, the php-cs-fixer runs followed by this error error: failed to push some refs to ..

I also tried the following without luck:

#!/bin/sh

php-cs-fixer fix . --config-file=".php_cs" && git commit -am 'PSR-2'

-

#!/bin/sh

(php-cs-fixer fix . --config-file=".php_cs" && git commit -am 'PSR-2')

According to this stackoverflow question, it should run only if cmd1 has succeeded.

Upvotes: 1

Views: 150

Answers (1)

René Link
René Link

Reputation: 51423

The exec builtin command replaces the shell with the given program. It does NOT fork a new process to execute php-cs-fixer.

Since the shell is replaced by the php-cs-fixer program the && git commit ... is never executed.

Take a look at the manpage of exec

If command is specified, it replaces the shell. No new process is created.

The first line of php-cs-fixer should look like this

#!/usr/bin/env php

and the php-cs-fixer should have execute permissions chmod +x php-cs-fixer.

Than you can just use it

php-cs-fixer fix . --config-file=".php_cs" && git commit -am 'PSR-2'

Upvotes: 3

Related Questions