Wizard exits into vim/COMMIT_EDITMSG
atomicrobokid opened this issue · comments
Hi,
Hoping for a bit of guidance,
I have setup this package following the instructions and have the wizard running. This uses husky and the prepare-commit-message
hook runs exec < /dev/tty && npx --no-install git-cz --hook || true
.
Then the commit-msg
hook runs npx --no -- commitlint --edit "\${1}"
to lint the commit message.
Relevant package versions:
"@commitlint/cli": "^16.2.4",
"@commitlint/config-conventional": "^16.2.4",
"@commitlint/cz-commitlint": "^16.2.4",
"commitizen": "^4.2.4",
"cz-customizable": "^6.3.0",
The trouble i'm facing is that every time i complete the wizard i'm dumped back into the vim in what looks like the COMMIT_EDITMSG
git hook asking me to enter a commit message, and have to :q
to exit which is annoying. Linting then runs after this as expected.
Steps to reproduce:
- Run
git commit -a
in terminal - Proceed through wizard and choose
Yes
to theconfirmCommit
message. - Vim starts,
:q
to exit - Linting runs.
If i comment out the linting stage i do not enter the COMMIT_EDITMSG
hook, so from what i can tell, this opens because the --edit
flag of Commitlint says:
read last commit message from the specified file or fallbacks to ./.git/COMMIT_EDITMSG
so looking at my lint command npx --no -- commitlint --edit "\${1}"
- is the path/file not being passed correctly from cz to commitlint? Or is the git process thinking it also needs to launch?
Thanks
In fact, it is very not recommended to add commit-msg
hook~ It will change the default git commit command. E.g, you can't quickly perform "git commit -m ..."
+1 on this. Currently the existing husky integration with a prepare-commit-msg hook is broken and exits to an editor screen.
Not sure what you mean @Zhengqbbb, the point of using husky with commitizen is to enforce commitizen usage on all commits.
Not sure what you mean @Zhengqbbb, the point of using husky with commitizen is to enforce commitizen usage on all commits.
Yep~ when u use prepare-commit-msg
hook. it mean will enforce open editor
(the behavior is git commit
not git commit -m
Is there any solution for this problem? It's ridiculous to try to edit it every time. Otherwise, the package has no meaning.
@atomicrobokid were you able to fix this?
same here, looking for a fix on this annoying behaviour.
Hi guys, I am the author of cz-git and this is the only solution to alias your git command.
- copy code to your
.zshrc
or.bashrc
and restart your terminal
git() {
local _cmd="$*"
if [ "$1" = "commit" ] && [ "${_cmd%%"--help"}" = "$_cmd" ]; then
shift 1; command git commit -m ''
else
command git "$@"
fi
}
Maybe next month I'll write a post explaining why this is a problem and how to resolve, until then can follow this thread
Zhengqbbb/cz-git#87
I fixed the issue on my side by using the solution provided here
https://gist.github.com/webbertakken/c2b457d39224baf701c8de1589b61555#file-pre-commit-sh-L7
if sh -c ": >/dev/tty" >/dev/null 2>/dev/null; then exec >/dev/tty 2>&1; fi
Now the wizard will exit and my commitlint will run immediately instead of entering COMMIT_EDITMSG.
EDIT: August 9.
Just to complete my last answer. It works only if you run like this
git commit -m ""
Maybe next month I'll write a post explaining why this is a problem and how to resolve, until then can follow this thread Zhengqbbb/cz-git#87
Hi @Zhengqbbb, thanks for the above workaround. Have you written the mentioned post yet?
Kind regards,
Maybe next month I'll write a post explaining why this is a problem and how to resolve, until then can follow this thread Zhengqbbb/cz-git#87
Hi @Zhengqbbb, thanks for the above workaround. Have you written the mentioned post yet?
Kind regards,
🤩 I have been busy in the last half year, even weekends, but you reminded me that see my sharing, I will prepare it, and I will reply to you after posting the article.
Got a dirty solution here (using husky) by stubbing the editor with echo
which runs and quits immediately.
- Added to my
package.json
:
"prepare": "husky install && git config --local include.path ../.gitconfig"
- Created
.gitconfig
in project root:
[core]
editor=echo
This will still produce the following message:
hint: Waiting for your editor to close the file... /Users/arturmoczulski/.../.git/COMMIT_EDITMSG
However, it does achieve the goal of not opening a Vim or VS Code when using prepare-commit-msg
hook
This will still produce the following message:
hint: Waiting for your editor to close the file... /Users/arturmoczulski/.../.git/COMMIT_EDITMSG
@ArturMoczulski FYI, if you configure the core.editor
to be cat
instead of echo
, that will avoid the hint:
.
@Zhengqbbb There must be a better way? Maybe making use of the GIT_EDITOR
environment variable somewhere in the commit
lifecycle?
@ArturMoczulski FYI, if you configure the core.editor to be cat instead of echo, that will avoid the hint:.
@danielbayley
!!! Awesome !!! It work well~ 🤩
git config core.editor cat
There must be a better way? Maybe making use of the GIT_EDITOR environment variable somewhere in the commit lifecycle?
- Set the environment variable is different ways like windows (F...
SET
) and linux ... - Each hook file or command is a subshell, can not transfer to outside to affect git choose editor
Can add project scripts (like pnpm's postinstall) to help other project collaborator
// package.json
"scripts": {
"postinstall": "git config --local core.editor cat",
}
Just to clarify what I think @danielbayley was getting at:
-
Create
.gitconfig
in project root:[core] editor=cat
-
Add to package.json:
"prepare": "husky install && git config --local include.path ../.gitconfig"
-
Reinstall your packages with
npm i
,bun i
, etc.
This should achieve the desired effect of commitizen fully taking over the commit
command without exiting into vim or logging any superfluous messages.
You can also place the .gitconfig
somewhere else and update the prepare
script accordingly. I placed mine in the .husky
folder to keep things tidy.