Skip to content
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

Wizard exits into vim/COMMIT_EDITMSG #934

Open
atomicrobokid opened this issue May 10, 2022 · 14 comments
Open

Wizard exits into vim/COMMIT_EDITMSG #934

atomicrobokid opened this issue May 10, 2022 · 14 comments

Comments

@atomicrobokid
Copy link

atomicrobokid commented May 10, 2022

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
Copy link
Contributor

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
Copy link

dwhoban commented Aug 23, 2022

+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
Copy link
Contributor

Zhengqbbb commented Aug 23, 2022

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
Copy link

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
Copy link

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

@Zhengqbbb
Copy link
Contributor

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

@mrkpatchaa
Copy link

mrkpatchaa commented Aug 5, 2023

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
Copy link

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
Copy link
Contributor

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
Copy link

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
Copy link

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
Copy link
Contributor

Zhengqbbb commented Mar 7, 2024

@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
Copy link

lawren commented May 14, 2024

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.

@shtev21
Copy link

shtev21 commented Sep 15, 2024

So what would happen when someone tries to run something like git rebase -i?

I don't think setting the default editor to cat is a sensible approach to resolving this issue.

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

No branches or pull requests