0

I'm trying to set up a custom repo: local hook with pre-commit and running into an error message that I don't understand

My steps:

  1. From C:\Python\monorepo\project1\project1_app\ (in PyCharm terminal):
pre-commit install 
  1. Create the following file: C:\Python\monorepo\.pre-commit-config.yaml:
-   repo: local
    hooks:
    -   id: pre-commit
  1. Edited C:\Python\monorepo\.git\hooks\pre-commit:

pip freeze > requirements_check.txt
cmp --silent requirements_check.txt requirements.txt

if [ $? -gt 0 ]; 
then
  echo "There are packages in the env not in requirements.txt"
  echo "Aborting commit"
  rm requirements_check.txt
  exit 1
fi

rm requirements_check.txt
exit 0
  1. From C:\Python\monorepo\project1\project1_app\ ran
pre-commit run --all-files

I want to get this running without an error (once I know the basic config setup is correct I can tweak the script for my needs), but I am currently getting an error as follows:

An error has occurred: InvalidConfigError: 
==> File .pre-commit-config.yaml
==> At Config()
==> At key: repos
==> At Repository(repo='local')
==> At key: hooks
==> At Hook(id='pre-commit')
=====> Missing required key: name
Check the log at C:\Users\myusername\.cache\pre-commit\pre-commit.log

I also tried adding the "name" argument to the .pre-commit.config.yaml file, running "pre-commit clean" and retrying "pre-commit run -all-files", but I still get the same error:

repos:
-   repo: local
    hooks:
    -   id: pre-commit
    -   name: pre-commit

Can someone help with my pre-commit setup/config so I can avoid this error?

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
alexei7
  • 182
  • 1
  • 3
  • 11

1 Answers1

2

the error message is telling you that your yaml:

repos:
-   repo: local
    hooks:
    -   id: pre-commit
    -   name: pre-commit

makes a list of two dictionaries under hooks when you want to add the name key to the first dictionary

you'll also need entry and language and you will probably want pass_filenames: false too, but I'll let you figure that part out!

repos:
-   repo: local
    hooks:
    -   id: pre-commit
        name: pre-commit
        entry: path/to/script.sh
        language: script
        pass_filenames: false

disclaimer: I wrote pre-commit

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • 1
    Thank you so much for the very helpful answer - it worked. When I wrote this, I was using a "how to make bug report" video on Youtube - I've just realised it was yours - thank you for that too!! – alexei7 Mar 14 '23 at 12:48
  • I'm want to use python not bash to run "pip freeze > requirements.txt" before committing to git? Is it possible? – alexei7 Mar 14 '23 at 12:50
  • I've tried a lot of variations of settings in the ".pre-commit-config.yaml" and 'pre-commit" but I've not been able to get requirements.txt to update. – alexei7 Mar 14 '23 at 12:50