Skip to content

Is there any way to use this in a prepare-commit-msg hook? #689

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.

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

Closed
ArgentumCation opened this issue Mar 17, 2023 · 5 comments · Fixed by #731
Closed

Is there any way to use this in a prepare-commit-msg hook? #689

ArgentumCation opened this issue Mar 17, 2023 · 5 comments · Fixed by #731
Labels
scope: pre-commit type: feature A new enhacement proposal

Comments

@ArgentumCation
Copy link

ArgentumCation commented Mar 17, 2023

Description

The Node version of commitizen provides a --hook flag that allows users to generate a commit message, without actually performing a commit, is there a way to replicate this behavior in the Python version?

The example prepare-commit-hook in the cz-cli documentation is something like this:

#!/bin/bash
exec < /dev/tty && node_modules/.bin/cz --hook || true

Possible Solution

A new command or flag such as cz message or cz commit --message-only that could be embedded into a git hook that would generate a commit message without performing a full commit.

Additional context

Since the commit message in cz --dry-run is output to stdout (along with the questions) there's no way to just pipe the output of that directly into .git/COMMIT_MSG

Additional context

No response

@ArgentumCation ArgentumCation added the type: feature A new enhacement proposal label Mar 17, 2023
@woile
Copy link
Member

woile commented Mar 18, 2023

I'm ok with this feature. The question is, should we refactor --dry-run or should we introduce another flag?
In case we go for --dry-run how could we refactor it? Sending the questionary to the stderr?

@crai0
Copy link
Contributor

crai0 commented Apr 27, 2023

I think keeping --dry-run the way it is makes more sense because users probably use it to test their customizations. I would like to see something similar to what @ArgentumCation proposed:

The commit sub-command has a new --write-message-to-file option that takes a file path. When the flag is passed, commitizen runs the usual interactive dialog for preparing the commit message and then writes the final message to the specified file. This can be combined with --dry-run to also stop commitizen from performing the commit.

This way people can use it for more than just prepare-commit-msg hooks but it also flows well with how pre-commit handles these kinds of hooks, so going with this proposal would also make adding a new prepare-commit-msg pre-commit hook straightforward.

I could try whipping up an implementation of this if that helps.

@woile
Copy link
Member

woile commented Apr 28, 2023

Sounds good, PR is welcome.

@crai0
Copy link
Contributor

crai0 commented Apr 28, 2023

I did some research and interactive pre-commit hooks are not a thing pre-commit/pre-commit#1857.

A working prepare-commit-msg hook (not using pre-commit) could look like this:

#!/bin/sh
COMMIT_MSG_FILE=$1
exec < /dev/tty && cz commit --dry-run --write-message-to-file $COMMIT_MSG_FILE || true

I'll add a tutorial to the docs as well.

@crai0
Copy link
Contributor

crai0 commented Apr 28, 2023

I opened a pull request. Would be awesome to see this getting released soon.

@woile woile closed this as completed in #731 May 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope: pre-commit type: feature A new enhacement proposal
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants