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 ?

doudizhu2222
doudizhu2222 是的,我在全球范围内安装此套餐。我更新了我的答案,添加屏幕,请看
3 年多之前 回复
doumowu7371
doumowu7371 在一个不相关的说明中,包fabfab/php-cs-fixer已被移动并重命名为friendsofphp/php-cs-fixer,请参阅packagist.org/packages/fabpot/php-cs-fixer。
3 年多之前 回复
douxi3233
douxi3233 我有非常相似的代码,但可能在某些变量中出现问题,我更改了ROOT=“/home/ivan/host/master/src/”或PHP_CS_FIXER=“php-cs-fixer”我有全局php-cs-fixer命令,你怎么看?并且没有米我如何提交文件,使用git-gui或PhpStorm或控制台命令?
3 年多之前 回复
dongmeng1875
dongmeng1875 看看github.com/refinery29/php-cs-fixer-config#pre-commit-hook
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐