Skip to content

Conversation

regit
Copy link

@regit regit commented Apr 23, 2025

The patch fixes command line when used as pre-commit plugin and change the hook.

The --edit flag was asking for a argument which was not supplied by pre-commit.

When using prepare-commit-msg hook, it was impossible to use an editor for the commit message.

Why

After "fixing" #428 by skipping compilation, I've found out that it was not working properly.

Summary by CodeRabbit

  • Chores
    • Updated commit message validation to run at the correct stage, ensuring better alignment with Git commit workflows.

The patch fixes command line when used as pre-commit plugin
and change the hook.

The --edit flag was asking for a argument which was not
supplied by pre-commit.

When using prepare-commit-msg hook, it was impossible to use
an editor for the commit message.
@regit regit requested a review from KeisukeYamashita as a code owner April 23, 2025 13:14
Copy link

coderabbitai bot commented Apr 23, 2025

Walkthrough

The update modifies the pre-commit hook configuration for commit message linting. Specifically, it changes the execution command for the commitlint tool to explicitly reference the commit message file and adjusts the hook's trigger stage from prepare-commit-msg to commit-msg. No source code or public API changes are involved; only the configuration file is updated to refine how commit message validation is performed.

Changes

File(s) Change Summary
.pre-commit-hooks.yaml Updated the commitlint hook: changed the entry command to specify the commit message file and switched the hook stage from prepare-commit-msg to commit-msg.

Poem

In the warren, we commit with care,
Now linting runs just where it's fair.
With hooks aligned and messages neat,
Our history’s tidy, our process complete.
🐇✨


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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@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

🧹 Nitpick comments (1)
.pre-commit-hooks.yaml (1)

9-9: Reconsider enabling verbose output
Setting verbose: true will echo the full command on every commit. If it isn’t needed for debugging, disabling verbose mode (verbose: false) can keep the hook output cleaner.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between efaf903 and 6a77ccd.

📒 Files selected for processing (1)
  • .pre-commit-hooks.yaml (1 hunks)
🔇 Additional comments (1)
.pre-commit-hooks.yaml (1)

6-6: Confirm the correct hook stage for message linting
Switching from prepare-commit-msg to commit-msg is the right lifecycle event for validating the final commit message. This ensures linting runs after the message is composed.

name: Assert Conventional Commit Messages
description: 'Asserts that Conventional Commits have been used for all commit messages according to the rules for this repo.'
entry: commitlint --edit
entry: commitlint --edit .git/COMMIT_EDITMSG
Copy link

@coderabbitai coderabbitai bot Apr 23, 2025

Choose a reason for hiding this comment

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

💡 Verification agent

❓ Verification inconclusive

Use a dynamic commit message argument instead of a hard-coded path
Hard‑coding .git/COMMIT_EDITMSG can break in environments with custom Git dirs or submodules. Let pre‑commit pass the correct commit-msg file via $1 and restore the default filename passing:

-entry: commitlint --edit .git/COMMIT_EDITMSG
+entry: commitlint --edit "$1"

Also update or remove pass_filenames: false so that the hook receives the filename as its first argument.


Update commitlint hook to use the dynamic commit‐message file
Hard‑coding .git/COMMIT_EDITMSG can break in repos with custom Git dirs or submodules. In your .pre-commit-hooks.yaml:

• File: .pre-commit-hooks.yaml
• Line: 4

Replace:

-entry: commitlint --edit .git/COMMIT_EDITMSG
+entry: commitlint --edit "$1"

And ensure you keep:

pass_filenames: false

so that pre‑commit passes only the commit‑message file ($1) and no other filenames.

Copy link
Author

Choose a reason for hiding this comment

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

It failed when I gave the proposal a try.

Copy link

Choose a reason for hiding this comment

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

🧩 Analysis chain

@regit I'll need a bit more information to help you solve this issue. Could you share what error you encountered when trying the "$1" approach?

Let me also check how the command-line arguments are processed in this project to better understand the requirements.


🏁 Script executed:

#!/bin/bash
# Find how the --edit flag is implemented
rg --type rust "fn.*edit" -A 10

Length of output: 33


@regit Let me try different approaches to understand how the --edit flag works in this project.


🏁 Script executed:

#!/bin/bash
# Look for the CLI argument definitions that include the edit flag
rg --type rust "\-\-edit" -A 5 -B 5

Length of output: 754


@regit Let me continue investigating to better understand how the --edit flag works.


🏁 Script executed:

#!/bin/bash
# Find more information about the CLI arguments and specifically how the edit flag is defined
rg --type rust "edit.*=.*arg" -A 5 -B 5

Length of output: 41


@regit Let me try a different approach to find how the CLI arguments are defined.


🏁 Script executed:

#!/bin/bash
# Find how the CLI arguments are defined for the commitlint command
rg --type rust "struct.*Args" -A 20

Length of output: 1526


@regit Let me check how the edit argument is processed, which will help understand why it's failing.


🏁 Script executed:

#!/bin/bash
# Look at the processing of the edit argument
rg --type rust "if let Some\(edit\) = self\.edit\.as_deref\(\)" -A 10 -B 5

Length of output: 1076


@regit I understand better now. Looking at the code, I can see that the --edit flag needs a filename parameter. When you tried "$1", it likely failed because of quoting issues in the YAML file or how pre-commit passes arguments.


🏁 Script executed:

#!/bin/bash
# Let's look at how pre-commit handles hooks with commit-msg stage
# First check if there's any documentation in the repo
rg --type md "pre-commit" -A 10 -B 10

# Then look at any tests or examples with pre-commit hooks
ls -la
find . -name "*.toml" -o -name "*.yaml" -o -name "*.yml" | grep -i pre-commit

Length of output: 1169


@regit I think I understand the issue now. Looking at the code, the --edit flag requires a filename argument to read the commit message from.

When you tried using "$1", it likely failed because the quotes in the YAML file were interpreted literally - pre-commit would try to run commitlint --edit "$1" where it's looking for a file literally named $1 rather than substituting the value.


🏁 Script executed:

#!/bin/bash
# Let's check if .git/COMMIT_EDITMSG actually exists in a typical git repo
# and see if there are any other pre-commit examples with commit-msg stage

# Output the pre-commit-hooks.yaml file content
cat .pre-commit-hooks.yaml

# Check if there are any examples of pre-commit commit-msg hooks in other projects
rg --type yaml "\s+stages:\s+\[commit-msg\]" -A 5 -B 10

Length of output: 971


@regit I understand the issue now. When you tried using "$1" in the YAML file, it was treated as a literal string "$1" rather than a shell variable substitution.

For pre-commit hooks in the commit-msg stage, pre-commit passes the commit message file as the first argument. To use this, you need a wrapper script approach.

Here's what you could do:

  1. Create a small shell script in your repo:
#!/bin/sh
# pre-commit-commitlint.sh
commitlint --edit "$1"
  1. Make it executable: chmod +x pre-commit-commitlint.sh

  2. Update your .pre-commit-hooks.yaml:

- id: commitlint
  name: Assert Conventional Commit Messages
  description: 'Asserts that Conventional Commits have been used for all commit messages according to the rules for this repo.'
  entry: pre-commit-commitlint.sh
  language: script
  stages: [commit-msg]
  pass_filenames: false
  require_serial: true
  verbose: true

This approach properly handles the commit message file passed by pre-commit to your script.

Alternatively, if you prefer not to add a script, keeping the hardcoded .git/COMMIT_EDITMSG path is a reasonable compromise that works in most cases, as you've implemented in your current PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant