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

Commit is running twice when using commitizen with husky.

Open asjadanis opened this issue 4 years ago • 13 comments

I am trying to configure husky hooks with commitizen and lint-staged. This seems to trigger commit twice when I run yarn cz but when I run git commit it runs just once as expected. I have added githooks under the .husky folder and the configuration can be seen below. I have already seen the issue mentioned on readme and renamed my script to cz but it doesn't seem to be running as expected.

{
  "scripts": {
    "cz": "cz",
    "dev": "nodemon --watch '**/*.ts' --exec 'ts-node' src/server.ts",
    "lint": "eslint . --ext .ts",
    "lint-and-format": "eslint . --ext .ts --fix",
    "prettier-format": "prettier --config .prettierrc 'src/**/*.ts' --write",
    "test": "echo \"Error: no test specified\" && exit 1",
    "prepare": "husky install",
    "build": "tsc",
    "start": "NODE_ENV=production yarn build && node dist/server.js"
  },
  "lint-staged": {
    "src/**/*.ts": [
      "yarn prettier-format",
      "yarn lint-and-format"
    ],
    "*.ts": "eslint --cache --fix"
  },
  "config": {
    "commitizen": {
      "path": "cz-conventional-changelog"
    }
  },
  "dependencies": {
    "express": "^4.17.1"
  },
  "devDependencies": {
    "@types/express": "^4.17.13",
    "@typescript-eslint/eslint-plugin": "^4.29.1",
    "@typescript-eslint/parser": "^4.29.1",
    "commitizen": "^4.2.4",
    "cz-conventional-changelog": "^3.3.0",
    "dotenv": "^10.0.0",
    "eslint": "^7.32.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^3.4.0",
    "lint-staged": ">=10",
    "nodemon": "^2.0.12",
    "prettier": "^2.3.2",
    "ts-node": "^10.2.0",
    "typescript": "^4.3.5",
    "husky": ">=6"
  }
}

pre-commit

image

prepare-commit-msg

image

asjadanis avatar Aug 15 '21 19:08 asjadanis

Hey @asjadanis, I'm kinda having the same issue, have you found a workaround for this?

imchamarac avatar Oct 22 '21 09:10 imchamarac

I have the same problem i didn't find how to solve. Event if 'im not using husky and having a classic hook the command is run twice

yecine06 avatar Nov 09 '21 05:11 yecine06

@asjadanis did you find any solution to this?

oxodesign avatar Dec 09 '21 06:12 oxodesign

same issue, runs twice using regular git hook or husky

danielmahon avatar Dec 20 '21 15:12 danielmahon

This may or may not help people here, but we only run commitizen when no commit message is provided.

This means commitizen will not run with git commit -m, nor with git commit --amend, or for git merge commits etc... See https://git-scm.com/docs/githooks#_prepare_commit_msg for a full list of values the second arg can have.

  # Only run commitizen if no commit message was already provided.
  if [ -z "${2-}" ]; then
    export CZ_TYPE="${CZ_TYPE:-fix}"
    export CZ_MAX_HEADER_WIDTH=$COMMITLINT_MAX_WIDTH
    export CZ_MAX_LINE_WIDTH=$CZ_MAX_HEADER_WIDTH
    # By default git hooks are not interactive. exec < /dev/tty allows a users terminal to interact with commitizen.
    exec < /dev/tty && "$(dirname "$0")/git-cz.js" --hook
  fi

Not sure if this is the problem people here are facing, but it might help someone.

techmunk avatar Feb 11 '22 03:02 techmunk

This may or may not help people here, but we only run commitizen when no commit message is provided.

This means commitizen will not run with git commit -m, nor with git commit --amend, or for git merge commits etc... See https://git-scm.com/docs/githooks#_prepare_commit_msg for a full list of values the second arg can have.

  # Only run commitizen if no commit message was already provided.
  if [ -z "${2-}" ]; then
    export CZ_TYPE="${CZ_TYPE:-fix}"
    export CZ_MAX_HEADER_WIDTH=$COMMITLINT_MAX_WIDTH
    export CZ_MAX_LINE_WIDTH=$CZ_MAX_HEADER_WIDTH
    # By default git hooks are not interactive. exec < /dev/tty allows a users terminal to interact with commitizen.
    exec < /dev/tty && "$(dirname "$0")/git-cz.js" --hook
  fi

Not sure if this is the problem people here are facing, but it might help someone.

Thanks @techmunk. You are a life saver

vsanse avatar May 27 '22 05:05 vsanse

this worked for me to make them both work at the same time: prepare-commit-msg

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

if [ $2 == "template" ]; then
  exec < /dev/tty && npx cz --hook || true
fi

roman-supy-io avatar Aug 01 '22 13:08 roman-supy-io

Hi @roman-supy-io ,

Just curious if you mind clarifying how you achieved this? Where is "template" coming from in the CLI? Is it something like git commit -m "template" or is it a flag? Like git commit --template?

ontoneio avatar Aug 17 '22 22:08 ontoneio

Hi @roman-supy-io ,

Just curious if you mind clarifying how you achieved this? Where is "template" coming from in the CLI? Is it something like git commit -m "template" or is it a flag? Like git commit --template?

$2 is template when run git commit not like git commit -m

INT31302 avatar Dec 28 '22 16:12 INT31302

watching this

huantaoliu avatar Feb 24 '23 01:02 huantaoliu

@techmunk cz is running when I pass a commit message. Screenshot 2023-03-18 at 10 36 32 PM

thedath avatar Mar 18 '23 17:03 thedath

ref: https://github.com/commitizen/cz-cli/issues/934#issuecomment-1982811302

git config --local core.editor cat

image

Zhengqbbb avatar Mar 13 '24 15:03 Zhengqbbb