cz-cli icon indicating copy to clipboard operation
cz-cli copied to clipboard

Wizard exits into vim/COMMIT_EDITMSG

Open atomicrobokid opened this issue 3 years ago • 14 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:

  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

atomicrobokid avatar May 10 '22 17:05 atomicrobokid

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

Zhengqbbb avatar Jun 17 '22 02:06 Zhengqbbb

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

dwhoban avatar Aug 23 '22 02:08 dwhoban

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

Zhengqbbb avatar Aug 23 '22 03:08 Zhengqbbb

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?

ahmetkuslular avatar Jan 03 '23 18:01 ahmetkuslular

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

ghacosta avatar Feb 27 '23 17:02 ghacosta

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 https://github.com/Zhengqbbb/cz-git/issues/87

Zhengqbbb avatar Feb 28 '23 01:02 Zhengqbbb

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

mrkpatchaa avatar Aug 05 '23 20:08 mrkpatchaa

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,

aaccioly avatar Sep 21 '23 07:09 aaccioly

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.

Zhengqbbb avatar Sep 21 '23 13:09 Zhengqbbb

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

ArturMoczulski avatar Nov 04 '23 01:11 ArturMoczulski

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?

danielbayley avatar Mar 06 '24 17:03 danielbayley

@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

Zhengqbbb avatar Mar 07 '24 07:03 Zhengqbbb