Git hook没有运行2个连续命令

时间:2015-02-13 08:07:17

标签: git bash

我尝试创建一个pre-push挂钩,以便在每次推送之前运行php-cs-fixer

这是我当前的pre-push钩子:

#!/bin/sh

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

第一个命令没有问题就会触发。 只有git commit -am 'PSR-2没有。更准确地说,php-cs-fixer运行后跟此错误error: failed to push some refs to ..

我没有运气也试过以下内容:

#!/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')

根据这个stackoverflow问题,它应该只在cmd1成功时运行。

1 个答案:

答案 0 :(得分:3)

exec builtin命令用给定的程序替换shell。它 NOT 分叉一个新进程来执行php-cs-fixer

由于shell被php-cs-fixer程序取代,&& git commit ...永远不会被执行。

查看manpage of exec

  

如果指定了command,它将替换shell。没有创建新流程。

php-cs-fixer的第一行应该如下所示

#!/usr/bin/env php

php-cs-fixer应具有执行权限chmod +x php-cs-fixer

比你可以使用它

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