duanba7498 2017-07-24 12:31
浏览 72

Gt preCommit(可能有帮助PhpStorm)和php-cs-fixer

I want automatic process, when I commit file my php-cs-fixer trn and fix all files which add to commit, maybe PhpStorm have some opportunity, running external tools before commit or push. I found only after commit, but I don't need after, need before. And I try tune my git, using pre-commit event I create in my project file, like example, just without extension

sudo gedit .git/hooks/pre-commit

then add code

#!/usr/bin/env bash

ROOT="/home/ivan/host/master"

echo "php-cs-fixer pre commit hook start"

PHP_CS_FIXER="php-cs-fixer"
HAS_PHP_CS_FIXER=false

if [ -x php-cs-fixer ]; then
    HAS_PHP_CS_FIXER=true
fi

if $HAS_PHP_CS_FIXER; then
    git status --porcelain | grep -e '^[AM]\(.*\).php$' | cut -c 3- | while read line; do
        $PHP_CS_FIXER fix --config-file=$ROOT/.php_cs --verbose "$line";
        git add "$line";
    done
else
    echo ""
    echo "Please install php-cs-fixer, e.g.:"
    echo ""
    echo "  composer require --dev fabpot/php-cs-fixer:dev-master"
    echo ""
fi

echo "php-cs-fixer pre commit hook finish"

then compare when I running php-cs-fixer for some file,

php-cs-fixer fix src/AppBundle/Services/OutboundInvoiceService.php

then checking file and have many fixer part for this file, then checkout changes and just add empty line for this file and commit it, pre-commit not working, only what I have in my repo commit with empty line, without php-cs-fixer working. What am I doing wrong ?

Example where I add unused namespaces, php-cs-fixer must remove it and add some another fix, but after add file I have only unused namespace :(

enter image description hereenter image description here enter image description here enter image description here enter image description here

Only working if I create php file

#!/usr/bin/php
<?php
/**
 * .git/hooks/pre-commit
 *
 */

 /**
  * collect all files which have been added, copied or
  * modified and store them in an array called output
  */
 exec('git diff --cached --name-status --diff-filter=ACM', $output);
foreach ($output as $file) {

$fileName = trim(substr($file, 1) );
/**
 * Only PHP file
 */
if (pathinfo($fileName,PATHINFO_EXTENSION) == "php") {
    /**
     * Check for error
     */
    $lint_output = array();
    exec("php -l " . escapeshellarg($fileName), $lint_output, $return);
    if ($return == 0) {

        /**
         * PHP-CS-Fixer && add it back
         */
        exec("php-cs-fixer fix {$fileName}; git add {$fileName}");
    } else {

        echo implode("
", $lint_output), "
";

        exit(1);

    }
    }
}
exit(0);

and after git add file before pushed, run this file.

But this is complicated approach, because after commit file, I need run this script, BUT IT'S WORKING.

And my question how to use git hooks pre-commit for automatic this process ?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)