question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Commit is running twice when using commitizen with husky.

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:15
  • Comments:9

github_iconTop GitHub Comments

7reactions
techmunkcommented, Feb 11, 2022

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.

0reactions
INT31302commented, Dec 28, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Commitizen and husky seem to be triggering the pre- ...
I am trying to configure commitizen with husky and when I commit using a custom script from package.json commit gets triggered twice.
Read more >
Commitizen - GitHub Pages
In essence, npm and husky will run "precommit" scripts twice if you name the script "commit" , and the workaround is to prevent...
Read more >
should husky be running precommit hooks twice
should husky be running precommit hooks twice: once before commitizen wizards and again after?
Read more >
How To Setup Husky, Lint Staged, And Commitizen On A Next ...
Husky improves your commits and more woof! As I said before about Git Hooks, we can use these hooks to run custom scripts...
Read more >
Set up ESLint, Prettier, Commitizen, Husky and friends in 2022
Finally, Commitizen is a tool that is useful in helping to ensure that git ... The next section will show how to use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found