How to bypass pre-commit hooks after fixing merge conflicts?
Asked Answered
A

2

12

TLDR: git rev-parse -q --verify MERGE_HEAD errors out when NOT in a merge state. How can I get a similar command that would error out when we're IN a merge state?


Problem: when I merge master into my branch and it has conflicts, I have to manually correct them, then commit. In some cases though, I have too many changes coming from master, so I'd rather just skip the pre-commit hooks.

Partial solution: I could just run git commit --no-verify and get things merged, but that's because I know about the flag. Other developers might not know about it and so (1) they spend more time than needed waiting for the pre-commit hooks to run and (2) as it could take too long, they tend to be less likely to remerge master (assumption).

Almost definitive solution: I found a way to detect if I am in a merge state by using git rev-parse -q --verify MERGE_HEAD. It returns a hash when in merge state, and errors out when NOT in merge state. But what I need is exactly the opposite. I need it to error out when I AM in a merge state, so I could add it as a pre-commit hook to all my hooks. That'd make them fail in the first line, then not execute the following command.

  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "*.js": [
      "git rev-parse -q --verify MERGE_HEAD",
      "eslint --max-warnings 0 --fix"
    ],
    "*.scss": [
      "git rev-parse -q --verify MERGE_HEAD",
      "stylelint 'app/**/*.scss'"
    ],
    "*.{js,json,yml,scss,md}": [
      "git rev-parse -q --verify MERGE_HEAD",
      "prettier --write"
    ],
    "*": [
      "bundle exec rubocop --auto-correct --force-exclusion"
    ]
  },

Note that the above mentioned does not work. In order for it to work, all we need is to reverse the effect from git rev-parse -q --verify MERGE_HEAD, i.e, it should error out when we are in a merge state. How can I do this in a simple command?

Attar answered 15/4, 2020 at 17:25 Comment(0)
B
6

If you dont want to run lint-staged script on pre-commit hook after fixing merge conflicts then you would do something like in the code snippet below. I have added --no-revs just in order not to show output that is not needed

"husky": {
  "hooks": {
    "pre-commit": "git rev-parse -q --no-revs --verify MERGE_HEAD || lint-staged"
  }
},
Blizzard answered 29/10, 2020 at 6:41 Comment(1)
I can verify, this solution works well, I have it in my ./husky/pre-commit: git rev-parse -q --no-revs --verify MERGE_HEAD || npx lint-stagedSelfinduction
R
4

Have you considered negating the outcome of your initial command? e.g.

git rev-parse -q --verify MERGE_HEAD && false

If you're in the middle of merge the right-hand side is true therefore false gets evaluated and the command exits with status code 1.

Rail answered 15/4, 2020 at 22:36 Comment(1)
Or, use the ! shell directive: ! git rev-parse -q --verify MERGE_HEADBlacktail

© 2022 - 2024 — McMap. All rights reserved.