lefthook icon indicating copy to clipboard operation
lefthook copied to clipboard

Skip hook/command when commit message matches a pattern

Open reitzig opened this issue 1 year ago • 2 comments

:zap: Summary

Allow to skip a hook (or individual command) if the commit message matches a certain pattern.

Value

I like using cog verify (cf. cocogitto/cocogitto) as part of a commit-msg hook. I also like creating fixup commits (in IDEA, if that matters) which end up vanishing after rebasing with autosquash.

Sadly, the two do not mix well: cocogitto forbids the ephemeral commit messages of such commits, as they don't conform to conventional commits.

What I would like is to configure lefthook to ignore fixup commits:

commit-msg:
  skip:
    - ref: "wip/*"
    - msg: "^fixup! "
  commands:
    cog:
      run: cog verify --file {1}

Behavior and configuration changes

The change seems additive to me.

reitzig avatar Jul 22 '24 15:07 reitzig

FWIW: cocogitto/cocogitto#403

Either feature fixes my use case, but I do think that this proposal has merit in its own right for lefthook. Users may use different tools to enforce conventional commits!

reitzig avatar Jul 22 '24 15:07 reitzig

Workaround:

commit-msg:
  skip:
    - ref: "wip/*"
  commands:
    cog:
      run: sh -c "if grep -qv '^fixup!' {1}; then cog verify --file {1}; fi"

reitzig avatar Aug 02 '24 09:08 reitzig

There is also another workaround:

commit-msg:
  skip:
    - run: cat .git/COMMIT_EDITMSG | grep "^fixup! "
  jobs:
    - run: cog verify --file {1}

I will close this issue since there are workarounds and msg: '' matcher only makes sense for commit-msg hook.

mrexox avatar Apr 02 '25 07:04 mrexox