简体   繁体   English

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

[英]Git hook not running 2 consecutive commands

I'm trying to create a pre-push hook to run php-cs-fixer before every push. 我正在尝试创建一个pre-push钩,以便在每次推送之前运行php-cs-fixer

This is the current pre-push hook I have: 这是我目前的pre-push钩:

#!/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. 只有git commit -am 'PSR-2没有。 To be more precise, the php-cs-fixer runs followed by this error error: failed to push some refs to .. 更确切地说, php-cs-fixer运行后出现此错误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. 根据这个stackoverflow问题,它应该只在cmd1成功时运行。

The exec builtin command replaces the shell with the given program. exec builtin命令用给定的程序替换shell。 It does NOT fork a new process to execute php-cs-fixer . 没有一个新的进程来执行php-cs-fixer

Since the shell is replaced by the php-cs-fixer program the && git commit ... is never executed. 由于shell被php-cs-fixer程序取代,因此永远不会执行&& git commit ...

Take a look at the manpage of exec 看看exec联机帮助页

If command is specified, it replaces the shell. 如果指定了command,则替换shell。 No new process is created. 没有创建新进程。

The first line of php-cs-fixer should look like this php-cs-fixer的第一行应该是这样的

#!/usr/bin/env php

and the php-cs-fixer should have execute permissions chmod +x php-cs-fixer . 并且php-cs-fixer应该具有执行权限chmod +x php-cs-fixer

Than you can just use it 比你可以使用它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM