GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — add check on pre-push stage: exit if staged files not committed yet by aless10 · Pull Request #3113 · pre-commit/pre-commit

add check on pre-push stage: exit if staged files not committed yet #3113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
aless10 wants to merge 3 commits into pre-commit:main
base: main
Loading
Loading
from aless10:2486-prepush-staged-files

Uh oh!

There was an error while loading. Please reload this page.

Open

add check on pre-push stage: exit if staged files not committed yet #3113

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
3 commits
Select commit Hold shift + click to select a range
80e6149
add check on pre-push stage: exit if staged files not committed yet
aless10 Jan 18, 2024
e36cdc2
fix failing tests
aless10 Jan 18, 2024
f6c6b6c
improvements/fixes after code review
aless10 Feb 26, 2024
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

5 changes: 4 additions & 1 deletion 5 pre_commit/commands/run.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,10 @@ def run(
environ.get('_PRE_COMMIT_SKIP_POST_CHECKOUT')
):
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
# as environment variables for the hooks
if args.prepare_commit_message_source:
Expand Down
40 changes: 36 additions & 4 deletions 40 tests/commands/run_test.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def test_show_diff_on_failure(
{'hook': 'nope', 'hook_stage': 'pre-push'},
(b'No hook with id `nope` in stage `pre-push`',),
1,
True,
False,
),
(
{'all_files': True, 'verbose': True},
Expand Down Expand Up @@ -828,7 +828,7 @@ def test_stages(cap_out, store, repo_with_passing_hook):
'language': 'pygrep',
'stages': [stage],
}
for i, stage in enumerate(('pre-commit', 'pre-push', 'manual'), 1)
for i, stage in enumerate(('pre-commit', 'manual'), 1)
],
}
add_config_to_repo(repo_with_passing_hook, config)
Expand All @@ -844,8 +844,7 @@ def _run_for_stage(stage):
return printed

assert _run_for_stage('pre-commit').startswith(b'hook 1...')
assert _run_for_stage('pre-push').startswith(b'hook 2...')
assert _run_for_stage('manual').startswith(b'hook 3...')
assert _run_for_stage('manual').startswith(b'hook 2...')


def test_commit_msg_hook(cap_out, store, commit_msg_repo):
Expand Down Expand Up @@ -1246,3 +1245,36 @@ def test_pre_commit_env_variable_set(cap_out, store, repo_with_passing_hook):
cap_out, store, repo_with_passing_hook, args, environ,
)
assert environ['PRE_COMMIT'] == '1'


def test_pre_push_fails_if_staged_files(
cap_out, store, repo_with_passing_hook,
):
Comment thread
Comment on lines +1250 to +1252

@asottile asottile Feb 25, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test is probably sufficient -- then you don't need the additional one above

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

aless10 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
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'
)
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.