mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-20 01:24:42 +04:00
add check on pre-push stage: exit if staged files not committed yet
This commit is contained in:
parent
924680e974
commit
80e6149cf2
3 changed files with 42 additions and 1 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -4,3 +4,4 @@
|
||||||
/.tox
|
/.tox
|
||||||
/dist
|
/dist
|
||||||
.vscode/
|
.vscode/
|
||||||
|
.idea
|
||||||
|
|
|
||||||
|
|
@ -371,7 +371,14 @@ def run(
|
||||||
environ.get('_PRE_COMMIT_SKIP_POST_CHECKOUT')
|
environ.get('_PRE_COMMIT_SKIP_POST_CHECKOUT')
|
||||||
):
|
):
|
||||||
return 0
|
return 0
|
||||||
|
# prevent pushing staged files not committed (#2486)
|
||||||
|
if (
|
||||||
|
args.hook_stage == 'pre-push' and git.get_staged_files()
|
||||||
|
):
|
||||||
|
logger.error(
|
||||||
|
'Staged files found. Please commit before pushing',
|
||||||
|
)
|
||||||
|
return 1
|
||||||
# Expose prepare_commit_message_source / commit_object_name
|
# Expose prepare_commit_message_source / commit_object_name
|
||||||
# as environment variables for the hooks
|
# as environment variables for the hooks
|
||||||
if args.prepare_commit_message_source:
|
if args.prepare_commit_message_source:
|
||||||
|
|
|
||||||
|
|
@ -1246,3 +1246,36 @@ def test_pre_commit_env_variable_set(cap_out, store, repo_with_passing_hook):
|
||||||
cap_out, store, repo_with_passing_hook, args, environ,
|
cap_out, store, repo_with_passing_hook, args, environ,
|
||||||
)
|
)
|
||||||
assert environ['PRE_COMMIT'] == '1'
|
assert environ['PRE_COMMIT'] == '1'
|
||||||
|
|
||||||
|
|
||||||
|
def test_pre_push_fails_if_staged_files(
|
||||||
|
cap_out, store, repo_with_passing_hook,
|
||||||
|
):
|
||||||
|
config = {
|
||||||
|
'repo': 'local',
|
||||||
|
'hooks': [{
|
||||||
|
'id': 'no-todo',
|
||||||
|
'name': 'No TODO',
|
||||||
|
'entry': 'sh -c "! grep -iI todo $@" --',
|
||||||
|
'language': 'system',
|
||||||
|
'stage': 'pre-push',
|
||||||
|
}],
|
||||||
|
}
|
||||||
|
add_config_to_repo(repo_with_passing_hook, config)
|
||||||
|
|
||||||
|
with open('placeholder.py', 'w') as staged_file:
|
||||||
|
staged_file.write('"""TODO: something"""\n')
|
||||||
|
cmd_output('git', 'add', 'placeholder.py')
|
||||||
|
git_commit()
|
||||||
|
with open('placeholder.py', 'w') as staged_file:
|
||||||
|
staged_file.write('"""Ok"""\n')
|
||||||
|
|
||||||
|
cmd_output('git', 'add', 'placeholder.py')
|
||||||
|
|
||||||
|
args = run_opts(hook_stage='pre-push')
|
||||||
|
ret, printed = _do_run(cap_out, store, repo_with_passing_hook, args)
|
||||||
|
assert ret == 1
|
||||||
|
assert printed == (
|
||||||
|
b'[ERROR] Staged files found.'
|
||||||
|
b' Please commit before pushing\n'
|
||||||
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue