mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-04-15 18:11:48 +04:00
Merge pull request #1 from shahin/auto-commit
Add automatic addition of changes
This commit is contained in:
commit
0b6b7d5af1
5 changed files with 52 additions and 4 deletions
|
|
@ -81,6 +81,7 @@ MANIFEST_HOOK_DICT = cfgv.Map(
|
||||||
cfgv.Optional('require_serial', cfgv.check_bool, False),
|
cfgv.Optional('require_serial', cfgv.check_bool, False),
|
||||||
cfgv.Optional('stages', cfgv.check_array(cfgv.check_one_of(C.STAGES)), []),
|
cfgv.Optional('stages', cfgv.check_array(cfgv.check_one_of(C.STAGES)), []),
|
||||||
cfgv.Optional('verbose', cfgv.check_bool, False),
|
cfgv.Optional('verbose', cfgv.check_bool, False),
|
||||||
|
cfgv.Optional('commit_changes', cfgv.check_bool, False),
|
||||||
)
|
)
|
||||||
MANIFEST_SCHEMA = cfgv.Array(MANIFEST_HOOK_DICT)
|
MANIFEST_SCHEMA = cfgv.Array(MANIFEST_HOOK_DICT)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -164,6 +164,7 @@ def _run_single_hook(
|
||||||
retcode = 0
|
retcode = 0
|
||||||
diff_after = diff_before
|
diff_after = diff_before
|
||||||
files_modified = False
|
files_modified = False
|
||||||
|
hook_failed = False
|
||||||
out = b''
|
out = b''
|
||||||
elif not filenames and not hook.always_run:
|
elif not filenames and not hook.always_run:
|
||||||
output.write(
|
output.write(
|
||||||
|
|
@ -180,6 +181,7 @@ def _run_single_hook(
|
||||||
retcode = 0
|
retcode = 0
|
||||||
diff_after = diff_before
|
diff_after = diff_before
|
||||||
files_modified = False
|
files_modified = False
|
||||||
|
hook_failed = False
|
||||||
out = b''
|
out = b''
|
||||||
else:
|
else:
|
||||||
# print hook and dots first in case the hook takes a while to run
|
# 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
|
duration = round(time.time() - time_before, 2) or 0
|
||||||
diff_after = _get_diff()
|
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
|
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
|
print_color = color.RED
|
||||||
status = 'Failed'
|
status = 'Failed'
|
||||||
else:
|
else:
|
||||||
|
|
@ -223,7 +238,7 @@ def _run_single_hook(
|
||||||
output.write_line_b(out.strip(), logfile_name=hook.log_file)
|
output.write_line_b(out.strip(), logfile_name=hook.log_file)
|
||||||
output.write_line()
|
output.write_line()
|
||||||
|
|
||||||
return files_modified or bool(retcode), diff_after
|
return hook_failed, diff_after
|
||||||
|
|
||||||
|
|
||||||
def _compute_cols(hooks: Sequence[Hook]) -> int:
|
def _compute_cols(hooks: Sequence[Hook]) -> int:
|
||||||
|
|
|
||||||
|
|
@ -215,6 +215,11 @@ def commit(repo: str = '.') -> None:
|
||||||
cmd_output_b(*cmd, cwd=repo, env=env)
|
cmd_output_b(*cmd, cwd=repo, env=env)
|
||||||
|
|
||||||
|
|
||||||
|
def update_changes(repo: str = '.') -> None:
|
||||||
|
cmd = ('git', 'add', '--update')
|
||||||
|
cmd_output_b(*cmd, cwd=repo)
|
||||||
|
|
||||||
|
|
||||||
def git_path(name: str, repo: str = '.') -> str:
|
def git_path(name: str, repo: str = '.') -> str:
|
||||||
_, out, _ = cmd_output('git', 'rev-parse', '--git-path', name, cwd=repo)
|
_, out, _ = cmd_output('git', 'rev-parse', '--git-path', name, cwd=repo)
|
||||||
return os.path.join(repo, out.strip())
|
return os.path.join(repo, out.strip())
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@ class Hook(NamedTuple):
|
||||||
require_serial: bool
|
require_serial: bool
|
||||||
stages: Sequence[str]
|
stages: Sequence[str]
|
||||||
verbose: bool
|
verbose: bool
|
||||||
|
commit_changes: bool
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def cmd(self) -> tuple[str, ...]:
|
def cmd(self) -> tuple[str, ...]:
|
||||||
|
|
|
||||||
|
|
@ -82,9 +82,35 @@ def _unstaged_changes_cleared(patch_dir: str) -> Generator[None, None, None]:
|
||||||
# We failed to apply the patch, presumably due to fixes made
|
# We failed to apply the patch, presumably due to fixes made
|
||||||
# by hooks.
|
# by hooks.
|
||||||
# Roll back the changes made by hooks.
|
# Roll back the changes made by hooks.
|
||||||
cmd_output_b(*_CHECKOUT_CMD, env=no_checkout_env)
|
cmd_output_b(
|
||||||
|
'git',
|
||||||
|
'restore',
|
||||||
|
'--source',
|
||||||
|
tree,
|
||||||
|
'--worktree',
|
||||||
|
'.',
|
||||||
|
env=no_checkout_env,
|
||||||
|
)
|
||||||
_git_apply(patch_filename)
|
_git_apply(patch_filename)
|
||||||
|
|
||||||
|
# Save the current satte of the index which may include
|
||||||
|
# partially staged files, a new commit, etc.
|
||||||
|
new_tree = cmd_output('git', 'write-tree')[1].strip()
|
||||||
|
# Restore worktree to the patch base (which updates the index
|
||||||
|
# as a side-effect).
|
||||||
|
cmd_output_b(
|
||||||
|
'git',
|
||||||
|
'checkout',
|
||||||
|
tree,
|
||||||
|
'--',
|
||||||
|
'.',
|
||||||
|
env=no_checkout_env,
|
||||||
|
)
|
||||||
|
# Apply patch.
|
||||||
|
_git_apply(patch_filename)
|
||||||
|
# Restore new saved index.
|
||||||
|
cmd_output_b('git', 'read-tree', new_tree, env=no_checkout_env)
|
||||||
|
|
||||||
logger.info(f'Restored changes from {patch_filename}.')
|
logger.info(f'Restored changes from {patch_filename}.')
|
||||||
else:
|
else:
|
||||||
# There weren't any staged files so we don't need to do anything
|
# There weren't any staged files so we don't need to do anything
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue