Add automatic addition of changes from pre-commit to the commit

This commit is contained in:
shahin 2022-08-18 10:57:09 -07:00
parent 3fe38dff05
commit 0d19b4b7fd
5 changed files with 52 additions and 4 deletions

View file

@ -164,6 +164,7 @@ def _run_single_hook(
retcode = 0
diff_after = diff_before
files_modified = False
hook_failed = False
out = b''
elif not filenames and not hook.always_run:
output.write(
@ -180,6 +181,7 @@ def _run_single_hook(
retcode = 0
diff_after = diff_before
files_modified = False
hook_failed = False
out = b''
else:
# print hook and dots first in case the hook takes a while to run
@ -193,10 +195,23 @@ def _run_single_hook(
duration = round(time.time() - time_before, 2) or 0
diff_after = _get_diff()
# if the hook makes changes, fail the commit
# if the hook makes changes, fail the commit or add the changes
# automatically
files_modified = diff_before != diff_after
# We can't commit changes if another hook has made uncommitted
# modifications.
# In that case, fail the hook instead.
can_commit_changes = hook.commit_changes and not diff_before
hook_failed = bool(retcode) or (
files_modified and not can_commit_changes
)
if retcode or files_modified:
if files_modified and can_commit_changes:
git.update_changes()
diff_after = _get_diff()
assert not diff_after, 'Found unstaged diff'
if hook_failed:
print_color = color.RED
status = 'Failed'
else:
@ -223,7 +238,7 @@ def _run_single_hook(
output.write_line_b(out.strip(), logfile_name=hook.log_file)
output.write_line()
return files_modified or bool(retcode), diff_after
return hook_failed, diff_after
def _compute_cols(hooks: Sequence[Hook]) -> int: