简体   繁体   中英

In a pre-commit-hook is there a way to validate the "state after commit" of the checkout folder?

I'd like to run static checks (eg mypy , pylint ) in a pre-commit hook. My current naive approach (just running those checks in the pre-commit hook) gives me false negatives and false positives because of non-staged changes and untracked files.

So what I actually want to do is to readily create the commit, clean the workspace by stashing away unstaged and untracked local changes, and then run the test.

Of course I could make an alias for this in order to make things easier, but then I would not be allowed to git commit directly any more (also I'd have to share the git config file).

I also could use the pre-commit hook, but I don't see a way to allow git commit -m"foo" file1 file2 (ie providing file names instead of staging all changes before).

Is there an intended (or even built in) way to do check "the state after the commit excluding local modifications (untracked and unstaged)" that works with

git commit -m"foo" file1 file2

It's the responsibility of the commit hook writer to query the index rather than the files in the sandbox. It's all doable, but somewhat tricky. Fortunately, it's all been done before.

The Python module pre-commit will stash unstaged changes for you, run all the hooks you request in your configuration file, and then pop the stash back afterwards.

Ref: https://pre-commit.com/

If you're working with Node, an excellent alternative written in JS is Husky. It implements many Git hooks and integrates very easily in any Node project.

Ref: https://typicode.github.io/husky

You can use either of these tools with projects in any language, but it's easiest to match languages because pre-commit can be in your requirements.dev.txt and husky in your package.json so they get installed with the rest of your dev environment with minimal additional dependencies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM