Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5258dce

Browse files
particledecayasottile
authored andcommitted
fix: catch missing arg if using {prepare-}commit-msg stage
If using the prepare-commit-msg and commit-msg stages specifically (such as with the try-repo command), the `--commit-msg-filename` arg must be provided. [fixes #1336] chore: improve error message for hook stage check
1 parent 1c641b1 commit 5258dce

2 files changed

Lines changed: 20 additions & 8 deletions

File tree

pre_commit/commands/run.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,15 @@ def run(
306306
f'`git add {config_file}` to fix this.',
307307
)
308308
return 1
309+
if (
310+
args.hook_stage in {'prepare-commit-msg', 'commit-msg'} and
311+
not args.commit_msg_filename
312+
):
313+
logger.error(
314+
f'`--commit-msg-filename` is required for '
315+
f'`--hook-stage {args.hook_stage}`',
316+
)
317+
return 1
309318

310319
# Expose origin / source as environment variables for hooks to consume
311320
if args.origin and args.source:

tests/commands/run_test.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -663,12 +663,7 @@ def test_stages(cap_out, store, repo_with_passing_hook):
663663
'language': 'pygrep',
664664
'stages': [stage],
665665
}
666-
for i, stage in enumerate(
667-
(
668-
'commit', 'push', 'manual', 'prepare-commit-msg',
669-
'commit-msg',
670-
), 1,
671-
)
666+
for i, stage in enumerate(('commit', 'push', 'manual'), 1)
672667
],
673668
}
674669
add_config_to_repo(repo_with_passing_hook, config)
@@ -686,8 +681,6 @@ def _run_for_stage(stage):
686681
assert _run_for_stage('commit').startswith(b'hook 1...')
687682
assert _run_for_stage('push').startswith(b'hook 2...')
688683
assert _run_for_stage('manual').startswith(b'hook 3...')
689-
assert _run_for_stage('prepare-commit-msg').startswith(b'hook 4...')
690-
assert _run_for_stage('commit-msg').startswith(b'hook 5...')
691684

692685

693686
def test_commit_msg_hook(cap_out, store, commit_msg_repo):
@@ -819,6 +812,16 @@ def test_error_with_unstaged_config(cap_out, store, modified_config_repo):
819812
assert ret == 1
820813

821814

815+
def test_commit_msg_missing_filename(cap_out, store, repo_with_passing_hook):
816+
args = run_opts(hook_stage='commit-msg')
817+
ret, printed = _do_run(cap_out, store, repo_with_passing_hook, args)
818+
assert ret == 1
819+
assert printed == (
820+
b'[ERROR] `--commit-msg-filename` is required for '
821+
b'`--hook-stage commit-msg`\n'
822+
)
823+
824+
822825
@pytest.mark.parametrize(
823826
'opts', (run_opts(all_files=True), run_opts(files=[C.CONFIG_FILE])),
824827
)

0 commit comments

Comments
 (0)