Closed as not planned
Description
When writing a custom hook, it would be nice to know that certain preconditions are already met when executing the hook. These preconditions could have been verified by another hook, so having the ability to run a hook depending on the result of another would be useful.
For example, if we're going to check the contents of some .json
file for certain keys, knowing that the file in question is valid JSON would be useful before we attempt further inspection.
Usage
Users can specify hook dependencies in the hook configuration:
PreCommit:
CheckForKeyInJson:
depend: JsonSyntax
...
Note that depend
can be a single hook or an array of hooks.
Benefits
- This could potentially speed up hook runs in the case of failures, since we short circuit the running of other hooks if their dependencies didn't pass
- This allows hooks to incrementally build on each other without duplicating code (i.e. hooks don't need to check that a file is valid; they can rely on their dependencies to verify certain preconditions)
Costs
- Requires the introduction of a dependency resolution system (which would need to warn about cyclical dependencies)
- Execution time would increase depending on complexity of dependency graph (always need to do a traversal up front to ensure there are no cycles)
- Would limit our ability to parallelize hook runs, since some dependency graphs would force serial execution
- Requires a topological sort of hooks based on their dependencies so we execute them in the correct order. Hooks are no longer run in the order they are declared in
.overcommit.yml
Questions
- What happens if you
SKIP
a dependency of another hook?- If the dependent hook is required, don't allow the skip (and display message like when trying to skip a required hook)
- Otherwise skip all dependent hooks (make it clear to the user they were skipped due to a dependency being skipped)
- What happens if a user disables a hook that is a dependency of another hook?
Fail loudly with a helpful error message before running any hooks - What happens if a hook dependency returns a warning?
Warnings are just that: warnings, not show stoppers. Thus a warning would not cause all dependent hooks to fail, only errors would. - What happens if the user specifies a cyclical dependency?
Detect cycles before any hooks are run and fail fast with clear error message explaining cycle(s)
Feel free to leave comments or ask questions in this issue.