Skip to content

fix: add support for commit message file argument for pre-commit#366

Merged
shenxianpeng merged 6 commits intomainfrom
fix/add-commit-msg-file
Feb 3, 2026
Merged

fix: add support for commit message file argument for pre-commit#366
shenxianpeng merged 6 commits intomainfrom
fix/add-commit-msg-file

Conversation

@shenxianpeng
Copy link
Contributor

@shenxianpeng shenxianpeng commented Feb 3, 2026

fixes #363

Summary by CodeRabbit

  • New Features

    • Added "refactor" as a supported commit-message imperative.
    • Allow passing a commit message file path as a positional CLI argument.
  • Improvements

    • Simplified the message option to a boolean flag.
    • More predictable selection of commit message source (file vs stdin vs repository) during validation.
  • Tests

    • Added tests covering positional-argument workflows, branch interaction, invalid and missing-file cases.

@netlify
Copy link

netlify bot commented Feb 3, 2026

Deploy Preview for commit-check ready!

Name Link
🔨 Latest commit 8adb4db
🔍 Latest deploy log https://app.netlify.com/projects/commit-check/deploys/69814c4d0bc9ed0007c468b1
😎 Deploy Preview https://deploy-preview-366--commit-check.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 3, 2026

📝 Walkthrough

Walkthrough

Adds "refactor" to the allowed commit imperatives and changes CLI parsing to accept a positional commit message file path. The -m/--message option is now a boolean flag; commit message source selection uses the positional file, stdin, or git as available.

Changes

Cohort / File(s) Summary
Imperative Support
commit_check/imperatives.py
Added "refactor" to the IMPERATIVES set.
CLI Argument & Message Handling
commit_check/main.py
Added positional commit_msg_file argument; changed -m/--message from optional path to boolean flag; updated main flow to enable message validation and choose commit message source from positional file, stdin, or git accordingly.
Tests
tests/main_test.py
Added TestPositionalArgumentFeature with tests covering positional-file behavior with/without --message, branch flag interaction, invalid message, and nonexistent file fallback; uses tempfile and mocking.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant User
  participant CLI as commit-check main
  participant File as commit_msg_file
  participant Stdin
  participant Git
  participant Validator

  User->>CLI: invoke (pre-commit / git commit)
  CLI->>File: check positional `commit_msg_file` argument
  alt file provided
    File-->>CLI: commit_file_path
  else no file
    CLI->>Stdin: read stdin for message
    alt stdin has content
      Stdin-->>CLI: message content
    else no stdin
      CLI->>Git: request commit data
      Git-->>CLI: commit content
    end
  end
  CLI->>Validator: run validators with commit content (message validation enabled)
  Validator-->>CLI: results
  CLI-->>User: exit/status
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

developer

Poem

🐰 A nibble on verbs, a "refactor" cheer,
A path for messages both far and near.
Flags now flip, files, stdin, or git found,
Validators hop and circle round.
🥕✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The addition of 'refactor' to the IMPERATIVES set appears unrelated to the primary objective of supporting pre-commit's commit message file argument. Remove the 'refactor' imperative addition unless it is tied to a separate issue, or clarify why it is included in this PR focused on pre-commit compatibility.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding support for a commit message file positional argument to enable pre-commit compatibility.
Linked Issues check ✅ Passed The PR successfully addresses issue #363 by implementing a positional argument for the commit message file, adding 'refactor' to imperatives, and introducing comprehensive test coverage for the new functionality.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/add-commit-msg-file

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions bot added the bug Something isn't working label Feb 3, 2026
@codecov
Copy link

codecov bot commented Feb 3, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.65%. Comparing base (9f64593) to head (8adb4db).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #366      +/-   ##
==========================================
+ Coverage   94.37%   94.65%   +0.28%     
==========================================
  Files           9        9              
  Lines         800      805       +5     
==========================================
+ Hits          755      762       +7     
+ Misses         45       43       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed-hq
Copy link

codspeed-hq bot commented Feb 3, 2026

CodSpeed Performance Report

Merging this PR will degrade performance by 31.33%

Comparing fix/add-commit-msg-file (8adb4db) with main (450e718)1

Summary

⚡ 19 improved benchmarks
❌ 1 regressed benchmark
✅ 145 untouched benchmarks
🆕 5 new benchmarks
⏩ 108 skipped benchmarks2

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
test_version 2 ms 1.4 ms +43.14%
test_main_error_handling_subprocess_failure 3.5 ms 3 ms +17.95%
test_main_with_invalid_config_file 3.2 ms 2.7 ms +19.72%
test_main_with_message_empty_string_no_stdin_with_git 5.7 ms 8.2 ms -31.33%
test_main_with_dry_run_all_checks 3.8 ms 3.4 ms +14.04%
test_cli_allow_commit_types 5.1 ms 4.5 ms +11.31%
test_cli_subject_imperative_false 5.5 ms 5 ms +10.23%
test_nonexistent_config_file_error 2 ms 1.4 ms +40.86%
test_cli_allow_merge_commits_false 5.1 ms 4.5 ms +12.75%
test_env_allow_commit_types 5.1 ms 4.5 ms +11.73%
test_env_subject_max_length 5.1 ms 4.6 ms +10.53%
test_env_overrides_default 5.1 ms 4.6 ms +12.39%
test_cli_subject_imperative_true 5 ms 4.5 ms +11.62%
test_cli_subject_max_length 5.1 ms 4.6 ms +10.83%
test_env_subject_imperative 5 ms 4.5 ms +11.75%
test_author_email_validation 3.6 ms 3.1 ms +17.96%
test_dry_run_always_passes 3.2 ms 2.7 ms +18.4%
test_author_name_validation 3.6 ms 3.1 ms +17.24%
test_branch_validation 3.8 ms 3.2 ms +16.48%
test_message_validation_with_invalid_commit 5.1 ms 4.6 ms +10.8%
... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Footnotes

  1. No successful run was found on main (9f64593) during the generation of this report, so 450e718 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

  2. 108 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@shenxianpeng shenxianpeng marked this pull request as ready for review February 3, 2026 00:41
@shenxianpeng shenxianpeng requested a review from a team as a code owner February 3, 2026 00:41
@shenxianpeng shenxianpeng requested a review from Copilot February 3, 2026 00:41
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@commit_check/main.py`:
- Around line 319-327: The positional commit message file handling currently
sets args.message = True only when no other checks are present, causing message
validation to be skipped if a file is provided alongside --branch or --author
flags; change the logic in the commit_msg_file handling block so that when
args.commit_msg_file (commit_msg_file_path) is provided it always enables
message validation by setting args.message = True (or alternatively raise an
explicit error if combining a positional commit_msg_file with other checks is
unsupported); update the block that references commit_msg_file_path,
args.commit_msg_file, args.message, args.branch, args.author_name, and
args.author_email accordingly.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes issue #363 by adding support for pre-commit hooks to pass the commit message file as a positional argument. The pre-commit framework passes filenames as positional arguments when pass_filenames: true is set, causing the original implementation to fail with "unrecognized arguments" error.

Changes:

  • Modified argument parsing to accept an optional positional commit_msg_file argument for pre-commit compatibility
  • Changed --message flag from accepting an optional file path to a boolean flag
  • Added "refactor" to the list of imperative verbs (unrelated to the main fix)

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
commit_check/main.py Added positional commit_msg_file argument and modified --message to be a boolean flag; updated logic to handle commit message file from either positional argument or stdin
commit_check/imperatives.py Added "refactor" as an imperative verb (allows "refactor:" prefix in conventional commits)

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Contributor

Copilot AI commented Feb 3, 2026

@shenxianpeng I've opened a new pull request, #367, to work on those changes. Once the pull request is ready, I'll request review from you.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@commit_check/main.py`:
- Around line 65-66: Document that the semantics of the -m/--message flag
changed from accepting an optional file path to a boolean store_true flag:
update release notes and the migration guide to show the new usage (use the
positional argument for a message file, e.g., commit-check -m /path/to/file) and
add a clear note in the CLI help text for the --message/-m option in main.py so
users know to move prior invocations like `-m /path/to/file` to the positional
parameter.

Comment on lines +65 to +66
action="store_true",
help="validate commit message (file path can be provided as positional argument for pre-commit compatibility)",
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

# First, let's understand the current structure of the repository
git ls-files | head -20

Repository: commit-check/commit-check

Length of output: 561


🏁 Script executed:

# Check the git history of main.py to see if --message behavior changed
git log --oneline commit_check/main.py | head -10

Repository: commit-check/commit-check

Length of output: 120


🏁 Script executed:

# Look at the current main.py to understand the full context of lines 65-66 and the positional argument handling
wc -l commit_check/main.py

Repository: commit-check/commit-check

Length of output: 93


🏁 Script executed:

# Read the relevant section of main.py around lines 65-66 and surrounding code
sed -n '50,100p' commit_check/main.py

Repository: commit-check/commit-check

Length of output: 1332


🏁 Script executed:

# Check the recent commit that modified main.py
git show e593590 --stat

Repository: commit-check/commit-check

Length of output: 3037


🏁 Script executed:

# View the actual changes in that commit for main.py
git show e593590 -- commit_check/main.py | head -150

Repository: commit-check/commit-check

Length of output: 4457


🏁 Script executed:

# Check if there's an even earlier version to understand the full history
git log -p --follow -S "--message" -- commit_check/main.py | head -200

Repository: commit-check/commit-check

Length of output: 51


🏁 Script executed:

# Double-check if there are any earlier commits or branches with main.py
git log --all --follow --source -- commit_check/main.py

Repository: commit-check/commit-check

Length of output: 14151


🏁 Script executed:

# Verify the tests to see what the intended behavior is
grep -n "\-\-message\|commit_msg_file" tests/main_test.py | head -30

Repository: commit-check/commit-check

Length of output: 1331


🏁 Script executed:

# Check the previous version of main.py from the commit before e593590
git show bd1abc3:commit_check/main.py | head -100

Repository: commit-check/commit-check

Length of output: 2914


🏁 Script executed:

# Check if the old version had --message accepting a file path
git show bd1abc3:commit_check/main.py | grep -A 5 "\-\-message"

Repository: commit-check/commit-check

Length of output: 427


Document breaking change in --message flag behavior.

The -m/--message flag changed from accepting an optional file path argument to a boolean flag. Previous usage like -m /path/to/file must now use the positional argument: commit-check -m /path/to/file. Update release notes or migration guide to inform users of this change.

🤖 Prompt for AI Agents
In `@commit_check/main.py` around lines 65 - 66, Document that the semantics of
the -m/--message flag changed from accepting an optional file path to a boolean
store_true flag: update release notes and the migration guide to show the new
usage (use the positional argument for a message file, e.g., commit-check -m
/path/to/file) and add a clear note in the CLI help text for the --message/-m
option in main.py so users know to move prior invocations like `-m
/path/to/file` to the positional parameter.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Feb 3, 2026

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@tests/main_test.py`:
- Around line 536-549: The test_positional_arg_invalid_commit test can be flaky
because it does not mock commit_check.engine.get_commit_info; update the test to
patch commit_check.engine.get_commit_info (the same way
test_message_validation_with_invalid_commit does) to return a deterministic
commit payload (e.g., a dict with a known "author") before calling main(),
ensuring the validation outcome is independent of the repo state; use the
existing mocking style in other tests (mocker or monkeypatch) to set the return
value and then call main() and assert result == 1.

Comment on lines +536 to +549
@pytest.mark.benchmark
def test_positional_arg_invalid_commit(self):
"""Test that positional argument correctly rejects invalid commits."""
with tempfile.NamedTemporaryFile(mode="w", delete=False) as f:
f.write("invalid commit message without type")
f.flush()

try:
# Use positional argument with invalid message
sys.argv = ["commit-check", f.name]
result = main()
assert result == 1 # Should fail validation
finally:
os.unlink(f.name)
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Add mock for get_commit_info to ensure consistent test behavior.

This test is missing a mock that similar tests include. Looking at test_message_validation_with_invalid_commit (line 50), it mocks commit_check.engine.get_commit_info to return a known author. Without this mock, the test result could vary depending on the git state of the test environment or if the author happens to match an ignore list entry.

Proposed fix
     `@pytest.mark.benchmark`
     def test_positional_arg_invalid_commit(self):
         """Test that positional argument correctly rejects invalid commits."""
+        # Note: Consider adding mocker fixture and mocking get_commit_info
+        # similar to test_message_validation_with_invalid_commit
         with tempfile.NamedTemporaryFile(mode="w", delete=False) as f:

Or convert to use mocker:

     `@pytest.mark.benchmark`
-    def test_positional_arg_invalid_commit(self):
+    def test_positional_arg_invalid_commit(self, mocker):
         """Test that positional argument correctly rejects invalid commits."""
+        mocker.patch("commit_check.engine.get_commit_info", return_value="test-author")
         with tempfile.NamedTemporaryFile(mode="w", delete=False) as f:
🤖 Prompt for AI Agents
In `@tests/main_test.py` around lines 536 - 549, The
test_positional_arg_invalid_commit test can be flaky because it does not mock
commit_check.engine.get_commit_info; update the test to patch
commit_check.engine.get_commit_info (the same way
test_message_validation_with_invalid_commit does) to return a deterministic
commit payload (e.g., a dict with a known "author") before calling main(),
ensuring the validation outcome is independent of the repo state; use the
existing mocking style in other tests (mocker or monkeypatch) to set the return
value and then call main() and assert result == 1.

@shenxianpeng
Copy link
Contributor Author

shenxianpeng commented Feb 3, 2026

@copilot, code review the whole changes again.

Copy link
Contributor

Copilot AI commented Feb 3, 2026

@shenxianpeng I've opened a new pull request, #368, to work on those changes. Once the pull request is ready, I'll request review from you.

@shenxianpeng shenxianpeng merged commit 7237f50 into main Feb 3, 2026
35 of 36 checks passed
@shenxianpeng shenxianpeng deleted the fix/add-commit-msg-file branch February 3, 2026 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

commit-check: error: unrecognized arguments: .git/COMMIT_EDITMSG

2 participants