Merge pull request #1 from shahin/auto-commit

Add automatic addition of changes
This commit is contained in:
shahin 2022-08-18 11:01:38 -07:00 committed by GitHub
commit 0b6b7d5af1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 4 deletions

View file

@ -81,6 +81,7 @@ MANIFEST_HOOK_DICT = cfgv.Map(
cfgv.Optional('require_serial', cfgv.check_bool, False),
cfgv.Optional('stages', cfgv.check_array(cfgv.check_one_of(C.STAGES)), []),
cfgv.Optional('verbose', cfgv.check_bool, False),
cfgv.Optional('commit_changes', cfgv.check_bool, False),
)
MANIFEST_SCHEMA = cfgv.Array(MANIFEST_HOOK_DICT)

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:

View file

@ -215,6 +215,11 @@ def commit(repo: str = '.') -> None:
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:
_, out, _ = cmd_output('git', 'rev-parse', '--git-path', name, cwd=repo)
return os.path.join(repo, out.strip())

View file

@ -36,6 +36,7 @@ class Hook(NamedTuple):
require_serial: bool
stages: Sequence[str]
verbose: bool
commit_changes: bool
@property
def cmd(self) -> tuple[str, ...]:

View file

@ -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
# 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)
# 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}.')
else:
# There weren't any staged files so we don't need to do anything