Added two new options that allow for file modifications #1188
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces two new options to pre-commit that do not change the program's default behavior, but allow users to either specify command-line options or set environment variables to do two things:
--modified-files-ok
/PRE_COMMIT_MODIFIED_FILES_OK
: When specified/set, pre-commit no longer classifies it as a failure when hooks have a non-zero exit status or when hooks modify files (the former is required to accommodate the latter). What this means in practice is that when pre-commit is invoked from agit commit
command, it will not prevent the commit from proceeding if a hook modifies a file.--add-modified-files
/PRE_COMMIT_ADD_MODIFIED_FILES
: When specified/set, this first activates the--modified-files-ok
option and then if any files are modified by hooks, pre-commit stages those modifications. This means that when a user invokes pre-commit as part of agit commit
command, modifications made by hooks will be included in the commit and the commit command will be allowed to continue. The down side of activating this option is that if there are any un-staged changes (stashed by pre-commit) that conflict with the changes made by the hooks, then the stashed changes can be lost. I would welcome feedback or suggestions on how to prevent this.The objective of this work is to make using pre-commit a more seamless experience. I recently suggested to my co-workers that we start using pre-commit, and I had one particularly grumpy co-worker strongly object based on the fact that if the hooks made modifications, he would be required to manually stage those modifications and execute
git commit
a second time. I'm hoping that many others will see value in the simplified workflow enabled by these options just like I hope my co-worker doesn't have anything else to protest about... one can dream, right?I've added the necessary changes to the test cases to ensure these changes don't break any existing tests. I've also used these options and verified they function as expected.