commitizen / cz-cli

The commitizen command line utility. #BlackLivesMatter
http://commitizen.github.io/cz-cli/
MIT License
16.58k stars 552 forks source link

Wizard exits into vim/COMMIT_EDITMSG #934

Open atomicrobokid opened 2 years ago

atomicrobokid commented 2 years ago

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:

  1. Run git commit -a in terminal
  2. Proceed through wizard and choose Yes to the confirmCommit message.
  3. Vim starts, :q to exit
  4. 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

Zhengqbbb commented 2 years ago

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 ..."

dwhoban commented 1 year ago

+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.

Zhengqbbb commented 1 year ago

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

ahmetkuslular commented 1 year ago

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?

ghacosta commented 1 year ago

same here, looking for a fix on this annoying behaviour.

Zhengqbbb commented 1 year ago

Hi guys, I am the author of cz-git and this is the only solution to alias your git command.

Maybe next month I'll write a post explaining why this is a problem and how to resolve, until then can follow this thread https://github.com/Zhengqbbb/cz-git/issues/87

mrkpatchaa commented 11 months ago

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 ""

aaccioly commented 9 months ago

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,

Zhengqbbb commented 9 months ago

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.

ArturMoczulski commented 8 months ago

Got a dirty solution here (using husky) by stubbing the editor with echo which runs and quits immediately.

  1. Added to my package.json:

    "prepare": "husky install && git config --local include.path ../.gitconfig"
    1. 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

danielbayley commented 4 months ago

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?

Zhengqbbb commented 4 months ago

@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?

  1. Set the environment variable is different ways like windows (F... SET) and linux ...
  2. 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",
}

image

lawren commented 1 month ago

Just to clarify what I think @danielbayley was getting at:

  1. Create .gitconfig in project root:

    [core]
      editor=cat
  2. Add to package.json:

    "prepare": "husky install && git config --local include.path ../.gitconfig"
  3. 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.