Home > front end >  Can a pre-commit (pre-commit.com) hook add files to a commit?
Can a pre-commit (pre-commit.com) hook add files to a commit?

Time:05-23

Can pre-commit hooks (with pre-commit.com) add files to a commit?

My use case: I do work in jupyter notebooks. On commit, I want to generate and git-add an html version of any ipynb files in the commit.

Eg a commit hook like:

- repo: local
  hooks:
    - id: nb-as-html
      name: nb-as-html
      stages: [commit]
      language: system
      verbose: false
      pass_filenames: false
      always_run: true
      entry: find devtools \( -name \*.ipynb -not -name \*checkpoint.ipynb \) -type f -exec jupyter nbconvert --to HTML {} \; 

and add the file somehow to the commit.

How do I do this? Or is pre-commit not intended for this?

CodePudding user response:

It is possible to add files in a pre-commit hook, but it is not recommended to do so. The Git developers will usually fix issues that come up with this workflow, but they don't do so gladly and it's not something that is encouraged. In addition, the user will not have expected you to do this and may not be pleased that you have done so. Since hooks aren't cloned when you clone the repository (since that would allow executing arbitrary code), you also can't assume other contributors will run them.

In addition, you should avoid committing generated files to the repository altogether. That tends to bloat the repository and cause conflicts when merges or rebases happen down the line. You'd be better off generating this file in CI and uploading it somewhere else.

  • Related