Node.js

huskyをインストールしたのに、pre-commitフックが動かない時にみる場所と対応方法

後からhuskyをプロジェクトに入れると、たまーにpre-commitが動かないことがあるので覚書。 現象 huskyをインストールして、.huskyrcに以下のようなpre-commitのタスクを書いても何もおきない […]

広告ここから
広告ここまで

後からhuskyをプロジェクトに入れると、たまーにpre-commitが動かないことがあるので覚書。

現象

huskyをインストールして、.huskyrcに以下のようなpre-commitのタスクを書いても何もおきない

{
  "hooks": {
      "pre-commit": "echo 'hello'"
  }
}

動いている様子が全くない。

$ git commit -m "some change"
[master 1f46900] some change
 1 file changed, 1 insertion(+), 1 deletion(-)

みるべき点

この場合、pre-commitのフックがそもそも作成されていない可能性が非常に高い。

.git/hooksを調べる

lsコマンドなどで調べてみるのが一番早い

$ ls -la .git/hooks/
total 88
drwxr-xr-x  12 development  staff   408  9  2 15:56 .
drwxr-xr-x  14 development  staff   476  9  2 19:01 ..
-rwxr-xr-x   1 development  staff   478  9  2 15:56 applypatch-msg.sample
-rwxr-xr-x   1 development  staff   896  9  2 15:56 commit-msg.sample
-rwxr-xr-x   1 development  staff   189  9  2 15:56 post-update.sample
-rwxr-xr-x   1 development  staff   424  9  2 15:56 pre-applypatch.sample
-rwxr-xr-x   1 development  staff  1642  9  2 15:56 pre-commit.sample
-rwxr-xr-x   1 development  staff  1348  9  2 15:56 pre-push.sample
-rwxr-xr-x   1 development  staff  4898  9  2 15:56 pre-rebase.sample
-rwxr-xr-x   1 development  staff   544  9  2 15:56 pre-receive.sample
-rwxr-xr-x   1 development  staff  1239  9  2 15:56 prepare-commit-msg.sample
-rwxr-xr-x   1 development  staff  3610  9  2 15:56 update.sample

.sampleしかないので、そもそもフックが作成できていない状態なのがわかる。

あるべき姿

もし生成に成功していれば、このように表示される。

$ ls -la .git/hooks/  | grep pre-commit
-rwxr-xr-x   1 development  staff  1405  9  2 19:04 pre-commit

対応方法

GitHubのIssueをみる限り、「入れ直す」が今の所ベターな対策の様子。

same here, this is a recurring issue with husky. previous version of husky you could fix this by doing

rm -rf .git/hooks

and then installing husky

npm install -D husky

this doesn’t work for me, but maybe it does for y’all.

https://github.com/typicode/husky/issues/445#issuecomment-458309598

[未検証] husky-upgradeでもいけるかもしれない

huskyを使っていると、たまにこのようなメッセージが表示されます。

Warning: Setting commit-msg script in package.json > scripts will be deprecated
Please move it to husky.hooks in package.json, a .huskyrc file, or a husky.config.js file
Or run ./node_modules/.bin/husky-upgrade for automatic update

See https://github.com/typicode/husky for usage

どうもhusky-upgradeコマンドでpre-commitのフックを作り直している様子です。

$ ./node_modules/.bin/husky-upgrade

husky > upgrading /Users/sandbox/test/practice-conventional-commit/package.json
moved scripts.commitmsg to husky.hooks.commit-msg
husky > done

もしかすると、こちらを使っても対処できるかもしれません。

ブックマークや限定記事(予定)など

WP Kyotoサポーター募集中

WordPressやフロントエンドアプリのホスティング、Algolia・AWSなどのサービス利用料を支援する「WP Kyotoサポーター」を募集しています。
月額または年額の有料プランを契約すると、ブックマーク機能などのサポーター限定機能がご利用いただけます。

14日間のトライアルも用意しておりますので、「このサイトよく見るな」という方はぜひご検討ください。

広告ここから
広告ここまで

Related Category posts