phpro / grumphp

A PHP code-quality tool
MIT License
4.15k stars 431 forks source link

No run task with commit with phpstorm #254

Closed cacahouwete closed 7 years ago

cacahouwete commented 7 years ago
Q A
Branch v0.11.0
Bug? yes
New feature? no
Question? no
Documentation? no

Hi!

I'm new with this tool and I already love it! But I have a problem when I use it with phpstorm with Windows. It seems that grumphp doesn't catch the event to run all tasks (phpcs only for my tests).

I made some test and when I run a commit like phpstorm made it with the following command: git -c core.quotepath=false commit --signoff --only -m "test" -- src/AppBundle/Entity/Platform.php

The commit works without any test :s . If I git add the file before and I run the same command, grumphp runs the task and display the expected error for the test.

I'm not very good in English, (I m French) if you don't understand something I can try to reexplain ^^,

My configuration PHP7.1.0 Windows 10 (Vesion 1607) PHPStorm2016.3.2 Symfony2.8.15 Git/GitBashForWindows 2.11.0

# grumphp.yml
parameters:
    git_dir: .
    bin_dir: bin
    tasks:
        phpcs:
            standard: "vendor/leaphub/phpcs-symfony2-standard/leaphub/phpcs/Symfony2/"

Steps to reproduce with PHPStorm:

Result with PHPStorm:

[master 044a0bf] test
 1 file changed, 3 insertions(+)

Steps to reproduce with GitBashForWindows:

veewee commented 7 years ago

Hello @cacahouete78,

Thanks for reporting! I can't reproduce this on my mac, so I'll need to investigate it on a Windows machine. In the meantime, can you try to add the '-vvv' parameter to the grumphp command in the .git/hooks/pre-commit file and try again? This will give us some more information about what is going wrong.

Thanks!

cacahouwete commented 7 years ago
$ git -c core.quotepath=false commit --only -m "up" -- src/AppBundle/AppBundle.php

I have the following output :

[2017-01-03 11:29:37] GrumPHP.INFO: run command: diff "-r -p -m -M --full-index --staged"  [] []
[2017-01-03 11:29:37] GrumPHP.DEBUG: last command (diff) duration: 203.02ms [] []
[2017-01-03 11:29:37] GrumPHP.DEBUG: last command (diff) return code: 0 [] []
[2017-01-03 11:29:37] GrumPHP.DEBUG: last command (diff) output:  [] []

If I git add before I have the following output

[2017-01-03 11:31:37] GrumPHP.INFO: run command: diff "-r -p -m -M --full-index --staged"  [] []
[2017-01-03 11:31:37] GrumPHP.DEBUG: last command (diff) duration: 202.49ms [] []
[2017-01-03 11:31:37] GrumPHP.DEBUG: last command (diff) return code: 0 [] []
[2017-01-03 11:31:37] GrumPHP.DEBUG: last command (diff) output: diff --git a/src/AppBundle/AppBundle.php b/src/AppBundle/AppBundle.php index 3e15bc56c939fe70070d8e108e980b78087fa0c7..250422a03ea62d8a0f95daaf92372e0ac8844e3c 100644 --- a/src/AppBundle/AppBundle.php +++ b/src/AppBundle/AppBundle.php @@ -13,6 +13,8 @@ class AppBundle extends Bundle       +     +        [] []
veewee commented 7 years ago

Isn't this behaviour normal? If you don't stage your changes to git and commit, you'll have an empty staged area. The changes wont be send to the server. You first need to tell git what changes you want to stage.

For example:

What do you get if you run git status and git diff before and after the commit in phpstorm?

cacahouwete commented 7 years ago

First test without git add

In the file I add some return lines to raise an error with phpcs

1) Bofore commit:

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   src/AppBundle/AppBundle.php

no changes added to commit (use "git add" and/or "git commit -a")
$ git diff
diff --git a/src/AppBundle/AppBundle.php b/src/AppBundle/AppBundle.php
index 129cbf3..b18869d 100644
--- a/src/AppBundle/AppBundle.php
+++ b/src/AppBundle/AppBundle.php
@@ -10,4 +10,7 @@ use Symfony\Component\HttpKernel\Bundle\Bundle;

 class AppBundle extends Bundle
 {
+
+
+
 }

2) Run commit :

$ git -c core.quotepath=false commit --only -m "up" -- src/AppBundle/AppBundle.php
[2017-01-03 13:55:19] GrumPHP.INFO: run command: diff "-r -p -m -M --full-index --staged"  [] []
[2017-01-03 13:55:20] GrumPHP.DEBUG: last command (diff) duration: 203.87ms [] []
[2017-01-03 13:55:20] GrumPHP.DEBUG: last command (diff) return code: 0 [] []
[2017-01-03 13:55:20] GrumPHP.DEBUG: last command (diff) output:  [] []
GrumPHP detected a pre-commit command.
GrumPHP is sniffing your code!
Running task 1/7: Composer
Running task 2/7: Blacklist
Running task 3/7: Phpcs
Running task 4/7: PhpMd
Running task 5/7: PhpParser
Running task 6/7: Phpunit
Running task 7/7: SecurityChecker
GrumPHP detected a commit-msg command.
GrumPHP is sniffing your code!
                \\\ ,
                 \ `|
                  ) (   .-""-.
                  | |  /_  {  '.
                  | | (/ `\   } )
                  | |  ^/ ^`}   {
                  \  \ \=  ( {   )
                   \  \ '-, {   {{
                    \  \_.'  ) }  )
                     \.-'   (     (
                     /'-.'_. ) (  }
                     \_(    {   _/\
                      ) '--' `-;\  \
                  _.-'       /  / /
           <\/>_.'         .'  / /
       <\/></\>/.  '      /<\// /
       </\>  _ |\`- _ . -/|<// (
    <\/>    - _- `  _.-'`_/- |  \
    </\>        -  - -  -     \\\
     }`<\/>                <\/>`{
     { </\>-<\/>_<\/>_<\/>-</\> }
     }      </\> </\> </\>      {
  <\/>.                         <\/>
  </\>                          </\>
   {`<\/>                     <\/>`}
   } </\>-<\/>_<\/>_<\/>_<\/>-</\> {
   {      </\> </\> </\> </\>      }
   }                               }
   {            N I C E            {
<\/>            W O R K            <\/>
</\>                               </\>
  `<\/>                          <\/>'
   </\>-<\/>_<\/>_<\/>_<\/>_<\/>-</\>
        </\> </\> </\> </\> </\>
[master 5474ebf] up
 1 file changed, 3 insertions(+)

After commit:

$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean
$ git diff

(command display nothing)

Second test with git add

I git push first. In the same file I add some other return lines to have git change and to raise an error with phpcs

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   src/AppBundle/AppBundle.php

no changes added to commit (use "git add" and/or "git commit -a")
$ git diff
diff --git a/src/AppBundle/AppBundle.php b/src/AppBundle/AppBundle.php
index b18869d..514883d 100644
--- a/src/AppBundle/AppBundle.php
+++ b/src/AppBundle/AppBundle.php
@@ -12,5 +12,8 @@ class AppBundle extends Bundle
 {

+
+

+
 }
$ git add src/AppBundle/AppBundle.php

(command display nothing)

$ git -c core.quotepath=false commit --only -m "up" -- src/AppBundle/AppBundle.php
[2017-01-03 14:21:36] GrumPHP.INFO: run command: diff "-r -p -m -M --full-index --staged"  [] []
[2017-01-03 14:21:36] GrumPHP.DEBUG: last command (diff) duration: 202.46ms [] []
[2017-01-03 14:21:36] GrumPHP.DEBUG: last command (diff) return code: 0 [] []
[2017-01-03 14:21:36] GrumPHP.DEBUG: last command (diff) output: diff --git a/src/AppBundle/AppBundle.php b/src/AppBundle/AppBundle.php index b18869dde3042c4293a6f9567f977fb97a52e50e..514883db0389046e047ce1bd1c8b8f5c561e1c26 100644 --- a/src/AppBundle/AppBundle.php +++ b/src/AppBundle/AppBundle.php @@ -12,5 +12,8 @@ class AppBundle extends Bundle  {     + +       +  }  [] []
GrumPHP detected a pre-commit command.
GrumPHP is sniffing your code!
Running task 1/7: Composer
Running task 2/7: Blacklist
Running task 3/7: Phpcs
Command: "C:\Scripts\bin\phpcs.BAT" "--standard=vendor/endouble/symfony3-custom-coding-standard/Symfony3Custom/" "--report-full" "--report-json" "src/AppBundle/AppBundle.php"

Command: "bin\phpcbf.BAT" "--standard=vendor/endouble/symfony3-custom-coding-standard/Symfony3Custom/" "D:\Projects\website\src\AppBundle\AppBundle.php"

Running task 4/7: PhpMd
Command: "bin\phpmd.BAT" "src/AppBundle/AppBundle.php" "text" "phpmd.xml" "--exclude" "Tests,tests"

Running task 5/7: PhpParser
Running task 6/7: Phpunit
Command: "bin\phpunit.BAT" "--configuration=grumphp-phpunit.xml"

Running task 7/7: SecurityChecker
                                       .....'',;;::cccllllllllllllcccc:::;;,,,''...'',,'..
                            ..';cldkO00KXNNNNXXXKK000OOkkkkkxxxxxddoooddddddxxxxkkkkOO0XXKx:.
                      .':ok0KXXXNXK0kxolc:;;,,,,,,,,,,,;;,,,''''''',,''..              .'lOXKd'
                 .,lx00Oxl:,'............''''''...................    ...,;;'.             .oKXd.
              .ckKKkc'...'',:::;,'.........'',;;::::;,'..........'',;;;,'.. .';;'.           'kNKc.
           .:kXXk:.    ..       ..................          .............,:c:'...;:'.         .dNNx.
          :0NKd,          .....''',,,,''..               ',...........',,,'',,::,...,,.        .dNNx.
         .xXd.         .:;'..         ..,'             .;,.               ...,,'';;'. ...       .oNNo
         .0K.         .;.              ;'              ';                      .'...'.           .oXX:
        .oNO.         .                 ,.              .     ..',::ccc:;,..     ..                lXX:
       .dNX:               ......       ;.                'cxOKK0OXWWWWWWWNX0kc.                    :KXd.
     .l0N0;             ;d0KKKKKXK0ko:...              .l0X0xc,...lXWWWWWWWWKO0Kx'                   ,ONKo.
   .lKNKl...'......'. .dXWN0kkk0NWWWWWN0o.            :KN0;.  .,cokXWWNNNNWNKkxONK: .,:c:.      .';;;;:lk0XXx;
  :KN0l';ll:'.         .,:lodxxkO00KXNWWWX000k.       oXNx;:okKX0kdl:::;'',;coxkkd, ...'. ...'''.......',:lxKO:.
 oNNk,;c,'',.                      ...;xNNOc,.         ,d0X0xc,.     .dOd,           ..;dOKXK00000Ox:.   ..''dKO,
'KW0,:,.,:..,oxkkkdl;'.                'KK'              ..           .dXX0o:'....,:oOXNN0d;.'. ..,lOKd.   .. ;KXl.
;XNd,;  ;. l00kxoooxKXKx:..ld:         ;KK'                             .:dkO000000Okxl;.   c0;      :KK;   .  ;XXc
'XXdc.  :. ..    '' 'kNNNKKKk,      .,dKNO.                                   ....       .'c0NO'      :X0.  ,.  xN0.
.kNOc'  ,.      .00. ..''...      .l0X0d;.             'dOkxo;...                    .;okKXK0KNXx;.   .0X:  ,.  lNX'
 ,KKdl  .c,    .dNK,            .;xXWKc.                .;:coOXO,,'.......       .,lx0XXOo;...oNWNXKk:.'KX;  '   dNX.
  :XXkc'....  .dNWXl        .';l0NXNKl.          ,lxkkkxo' .cK0.          ..;lx0XNX0xc.     ,0Nx'.','.kXo  .,  ,KNx.
   cXXd,,;:, .oXWNNKo'    .'..  .'.'dKk;        .cooollox;.xXXl     ..,cdOKXXX00NXc.      'oKWK'     ;k:  .l. ,0Nk.
    cXNx.  . ,KWX0NNNXOl'.           .o0Ooldk;            .:c;.':lxOKKK0xo:,.. ;XX:   .,lOXWWXd.      . .':,.lKXd.
     lXNo    cXWWWXooNWNXKko;'..       .lk0x;       ...,:ldk0KXNNOo:,..       ,OWNOxO0KXXNWNO,        ....'l0Xk,
     .dNK.   oNWWNo.cXK;;oOXNNXK0kxdolllllooooddxk00KKKK0kdoc:c0No        .'ckXWWWNXkc,;kNKl.          .,kXXk,
      'KXc  .dNWWX;.xNk.  .kNO::lodxkOXWN0OkxdlcxNKl,..        oN0'..,:ox0XNWWNNWXo.  ,ONO'           .o0Xk;
      .ONo    oNWWN0xXWK, .oNKc       .ONx.      ;X0.          .:XNKKNNWWWWNKkl;kNk. .cKXo.           .ON0;
      .xNd   cNWWWWWWWWKOkKNXxl:,'...;0Xo'.....'lXK;...',:lxk0KNWWWWNNKOd:..   lXKclON0:            .xNk.
      .dXd   ;XWWWWWWWWWWWWWWWWWWNNNNNWWNNNNNNNNNWWNNNNNNWWWWWNXKNNk;..        .dNWWXd.             cXO.
      .xXo   .ONWNWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWNNK0ko:'..OXo          'l0NXx,              :KK,
      .OXc    :XNk0NWXKNWWWWWWWWWWWWWWWWWWWWWNNNX00NNx:'..       lXKc.     'lONN0l.              .oXK:
      .KX;    .dNKoON0;lXNkcld0NXo::cd0NNO:;,,'.. .0Xc            lXXo..'l0NNKd,.              .c0Nk,
      :XK.     .xNX0NKc.cXXl  ;KXl    .dN0.       .0No            .xNXOKNXOo,.               .l0Xk;.
     .dXk.      .lKWN0d::OWK;  lXXc    .OX:       .ONx.     . .,cdk0XNXOd;.   .'''....;c:'..;xKXx,
     .0No         .:dOKNNNWNKOxkXWXo:,,;ONk;,,,,,;c0NXOxxkO0XXNXKOdc,.  ..;::,...;lol;..:xKXOl.
     ,XX:             ..';cldxkOO0KKKXXXXXXXXXXKKKKK00Okxdol:;'..   .';::,..':llc,..'lkKXkc.
     :NX'    .     ''            ..................             .,;:;,',;ccc;'..'lkKX0d;.
     lNK.   .;      ,lc,.         ................        ..,,;;;;;;:::,....,lkKX0d:.
    .oN0.    .'.      .;ccc;,'....              ....'',;;;;;;;;;;'..   .;oOXX0d:.
    .dN0.      .;;,..       ....                ..''''''''....     .:dOKKko;.
     lNK'         ..,;::;;,'.........................           .;d0X0kc'.
     .xXO'                                                 .;oOK0x:.
      .cKKo.                                    .,:oxkkkxk0K0xc'.
        .oKKkc,.                         .';cok0XNNNX0Oxoc,.
          .;d0XX0kdlc:;,,,',,,;;:clodkO0KK0Okdl:,'..
              .,coxO0KXXXXXXXKK0OOxdoc:,..
                        ...
            \    \   |    |     /    /
         \ _____  _____ _   _  _____
          |  __ \|_   _| \ | |/ ____| /
          | |__) | | | |  \| | |  __
          |  _  /  | | | . ` | | |_ |     /
          | | \ \ _| |_| |\  | |__| |
          |_|  \_\_____|_| \_|\_____|       /
        \
                  _____  _____ _   _  _____
           \     |  __ \|_   _| \ | |/ ____|  /
          \      | |__) | | | |  \| | |  __
                 |  _  /  | | | . ` | | |_ |   /
            \    | | \ \ _| |_| |\  | |__| |
            /    |_|  \_\_____|_| \_|\_____| \

                   _____  _____ _   _  _____   /
            /     |  __ \|_   _| \ | |/ ____|  /
           \      | |__) | | | |  \| | |  __
                  |  _  /  | | | . ` | | |_ |
             \    | | \ \ _| |_| |\  | |__| |  \
              /   |_|  \_\_____|_| \_|\_____| \
                /      /     |    |     \     \

FILE: ...\website\src\AppBundle\AppBundle.php
----------------------------------------------------------------------
FOUND 1 ERROR AND 1 WARNING AFFECTING 1 LINE
----------------------------------------------------------------------
 17 | WARNING | [ ] Please trim any trailing whitespace
 17 | ERROR   | [x] Whitespace found at end of line
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

Time: 101ms; Memory: 4Mb

You can fix some errors by running following command:
"bin\phpcbf.BAT" "--standard=vendor/endouble/symfony3-custom-coding-standard/Symfony3Custom/" "D:\Projects\website\src\AppBundle\AppBundle.php"
To skip commit checks, add -n or --no-verify flag to commit command
veewee commented 7 years ago

Great feedback, Thanks! :)

This seems related to #144. Normally the git hook is in charge of running the diff so that you can commit with the options -p, -a, -i, filenames, ...

DIFF=$(git diff -r -p -m -M --full-index --staged | cat)

Only when the diff is empty, GrumPHP will run a git diff. You can see that it is doing that based on:

[2017-01-03 13:55:19] GrumPHP.INFO: run command: diff "-r -p -m -M --full-index --staged"  [] []
[2017-01-03 13:55:20] GrumPHP.DEBUG: last command (diff) duration: 203.87ms [] []
[2017-01-03 13:55:20] GrumPHP.DEBUG: last command (diff) return code: 0 [] []
[2017-01-03 13:55:20] GrumPHP.DEBUG: last command (diff) output:  [] []

This means that something goes wrong with the DIFF in the git hook. Can you change your git hook:

# Fetch the GIT diff and format it as command input:
DIFF=$(git diff -r -p -m -M --full-index --staged | cat)

echo $DIFF;
exit 1;

This will just output the DIFF and stop your commit. I guess that your DIFF is empty. The $DIFF on my machine looks like this:

git -c core.quotepath=false commit --only -m "up" src/test.php
diff --git a/src/test.php b/src/test.php index 372bf10b74013301cfb4bf0e8007d208bb813363..d95f50da4a02d3d203bda1f3cb94e29d4f0ef481 100644 --- a/src/test.php +++ b/src/test.php @@ -2,3 +2,4 @@ 'something'; +'ok';
cacahouwete commented 7 years ago

so , next round :D

.git/hooks/pre-commit with echo exit added:

#!/bin/sh

#
# Run the hook command.
# Note: this will be replaced by the real command during copy.
#

# Fetch the GIT diff and format it as command input:
DIFF=$(git diff -r -p -m -M --full-index --staged | cat)
echo $DIFF;
exit 1;

# Run GrumPHP
(cd "./" && printf "%s\n" "${DIFF}" | exec "bin/grumphp" "-vvv" "git:pre-commit" '--skip-success-output')

# Validate exit code of above command
RC=$?
if [ "$RC" != 0 ]; then
  exit $RC;
fi

# Clean exit:
exit 0;
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   src/AppBundle/AppBundle.php

no changes added to commit (use "git add" and/or "git commit -a")
$ git -c core.quotepath=false commit --only -m "up" -- src/AppBundle/AppBundle.php
diff --git a/src/AppBundle/AppBundle.php b/src/AppBundle/AppBundle.php index 129cbf32df238bcf0a9938e166ee76c3078099ba..70e7539597e2d1f3cc3094061a474b2767e41f17 100644 --- a/src/AppBundle/AppBundle.php +++ b/src/AppBundle/AppBundle.php @@ -10,4 +10,6 @@ use Symfony\Component\HttpKernel\Bundle\Bundle; class AppBundle extends Bundle { + + }

when I run

cd "./" && printf "%s\n" "diff --git a/src/AppBundle/AppBundle.php b/src/AppBundle/AppBundle.php index 129cbf32df238bcf0a9938e166ee76c3078099ba..70e7539597e2d1f3cc3094061a474b2767e41f17 100644 --- a/src/AppBundle/AppBundle.php +++ b/src/AppBundle/AppBundle.php @@ -10,4 +10,6 @@ use Symfony\Component\HttpKernel\Bundle\Bundle; class AppBundle extends Bundle { + + }" | exec "bin/grumphp" "-vvv" "git:pre-commit" '--skip-success-output'
GrumPHP detected a pre-commit command.
GrumPHP is sniffing your code!
Running task 1/7: Composer
Running task 2/7: Blacklist
Running task 3/7: Phpcs
Running task 4/7: PhpMd
Running task 5/7: PhpParser
Running task 6/7: Phpunit
Running task 7/7: SecurityChecker

I have no task runing :s (I tried on GitBash for Windows and on the new Bash for Windows who run an Ubuntu Xenial on Windows)

cacahouwete commented 7 years ago

I just tried with my colleague's computer (on Ubuntu Xenial) , and there are the same issue. there are no check when we directly commit and push without add.

veewee commented 7 years ago

Great feedback! It looks like the diff from the hook does get parsed correctly. I've found an error while reading from the CLI input in GrumPHP. Can you validate if #255 Fixes your problem?

BTW: I also like your ASCII art :)

veewee commented 7 years ago

@cacahouete78: This one got merged in. Thanks for reporting!