gitgithooksgit-huskyhusky

How to run husky pre-commit in child directory only


We have an enterprise application with a folder structure like the following:

/project
  .git
  /sub1
    ...Java project
  /sub2
    package.json
    ...Javascript Backbone project
  /sub3
    ...Java project
  /sub4
    ...Java project
  /sub5
    package.json
    ...Javascript React project

I currently have Husky set up both in sub2 and sub5 projects, which causes conflicts (requires an npm install whenever I switch projects). Also, the Java developers are reporting that when they commit code in projects sub1, sub3 and sub4, the Husky git hooks are being executed.

Is it possible to only have the hooks run if you are IN a certain folder when you commit?


Solution

  • Is it possible to only have the hooks run if you are IN a certain folder when you commit?

    No, but the hook itself can determine which directory it was in when it was run. This is not properly documented, but GIT_PREFIX is set in the environment before Git chdir-s to the $GIT_WORK_TREE or $GIT_DIR directory. (This has been the case since Git version 1.7.8.)