mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-04-16 10:31:46 +04:00
New option to allow modified files
This commit is contained in:
parent
7728162d7a
commit
858e89865e
3 changed files with 16 additions and 2 deletions
|
|
@ -129,15 +129,20 @@ def _run_single_hook(classifier, hook, args, skips, cols, use_color):
|
||||||
diff_after = cmd_output_b('git', 'diff', '--no-ext-diff', retcode=None)
|
diff_after = cmd_output_b('git', 'diff', '--no-ext-diff', retcode=None)
|
||||||
|
|
||||||
file_modifications = diff_before != diff_after
|
file_modifications = diff_before != diff_after
|
||||||
|
modified_ok = args.modified_files_ok
|
||||||
|
|
||||||
# If the hook makes changes, fail the commit
|
# If the hook makes changes, fail the commit
|
||||||
if file_modifications:
|
if not modified_ok and file_modifications:
|
||||||
retcode = 1
|
retcode = 1
|
||||||
|
|
||||||
if retcode:
|
if retcode and not modified_ok:
|
||||||
retcode = 1
|
retcode = 1
|
||||||
print_color = color.RED
|
print_color = color.RED
|
||||||
pass_fail = 'Failed'
|
pass_fail = 'Failed'
|
||||||
|
elif file_modifications and modified_ok:
|
||||||
|
retcode = 0
|
||||||
|
print_color = color.YELLOW
|
||||||
|
pass_fail = 'Edited'
|
||||||
else:
|
else:
|
||||||
retcode = 0
|
retcode = 0
|
||||||
print_color = color.GREEN
|
print_color = color.GREEN
|
||||||
|
|
|
||||||
|
|
@ -101,6 +101,13 @@ def _add_run_options(parser):
|
||||||
'--show-diff-on-failure', action='store_true',
|
'--show-diff-on-failure', action='store_true',
|
||||||
help='When hooks fail, run `git diff` directly afterward.',
|
help='When hooks fail, run `git diff` directly afterward.',
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--modified-files-ok', '-m', action='store_true',
|
||||||
|
default='PRE_COMMIT_MODIFIED_FILES_OK' in os.environ,
|
||||||
|
help="File modification by hooks won't be equated with failure. "
|
||||||
|
'Equivalent to setting the environment variable '
|
||||||
|
'PRE_COMMIT_MODIFIED_FILES_OK=true.',
|
||||||
|
)
|
||||||
mutex_group = parser.add_mutually_exclusive_group(required=False)
|
mutex_group = parser.add_mutually_exclusive_group(required=False)
|
||||||
mutex_group.add_argument(
|
mutex_group.add_argument(
|
||||||
'--all-files', '-a', action='store_true', default=False,
|
'--all-files', '-a', action='store_true', default=False,
|
||||||
|
|
|
||||||
|
|
@ -107,6 +107,7 @@ def run_opts(
|
||||||
hook_stage='commit',
|
hook_stage='commit',
|
||||||
show_diff_on_failure=False,
|
show_diff_on_failure=False,
|
||||||
commit_msg_filename='',
|
commit_msg_filename='',
|
||||||
|
modified_files_ok=False,
|
||||||
):
|
):
|
||||||
# These are mutually exclusive
|
# These are mutually exclusive
|
||||||
assert not (all_files and files)
|
assert not (all_files and files)
|
||||||
|
|
@ -121,6 +122,7 @@ def run_opts(
|
||||||
hook_stage=hook_stage,
|
hook_stage=hook_stage,
|
||||||
show_diff_on_failure=show_diff_on_failure,
|
show_diff_on_failure=show_diff_on_failure,
|
||||||
commit_msg_filename=commit_msg_filename,
|
commit_msg_filename=commit_msg_filename,
|
||||||
|
modified_files_ok=modified_files_ok,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue