4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Laravel] GithookをGit管理し、コミット時に対象ファイルをフォーマットする

4
Posted at

なにがおきたか

この記事を書きましたが、unstagedのものまでコミットされたり不便でした

[Laravel] コードをコミット時に自動整形する
https://qiita.com/ntm718/items/0f8feedc4e14c068c455

やりかた

大筋は上記の記事通りです。
下記コマンドを実行した際に、`.git/hooks/pre-commit に記載した内容がそのままコピーされているようでした

composer.json
"hooks": {
            "pre-commit": [
                "./vendor/bin/php-cs-fixer fix .",
                "git add ."
            ]
        }
./vendor/bin/cghooks update

下記のように、対象のhooksに転記されるだけ

.git/hooks/pre-commit
# !/bin/sh                                                                                                                 

./vendor/bin/php-cs-fixer fix .
git add .

つまるところ、普通にshellを書けば、そのまま転記される

composer.json
"hooks": {
            "pre-commit": [
                "for FILE in `git diff --staged --name-only | grep .php`; do",
                "if [ -e $FILE ]; then",
                "./vendor/bin/php-cs-fixer fix $FILE",
                "git add $FILE",
                "fi",
                "done"
            ]
        }
./vendor/bin/cghooks update

これを反映させた場合

pre-commitは下記のようになっています

# !/bin/sh

for FILE in `git diff --staged --name-only | grep .php`; do
if [ -e $FILE ]; then
./vendor/bin/php-cs-fixer fix .
git add $FILE
fi
done

上記のように、stage済みのものかつphpファイルで、削除されていないものであればフォーマットされます。

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?