mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-04-15 18:11:48 +04:00
Add automatic addition of changes from pre-commit to the commit
This commit is contained in:
parent
3fe38dff05
commit
0d19b4b7fd
5 changed files with 52 additions and 4 deletions
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue