lefthook icon indicating copy to clipboard operation
lefthook copied to clipboard

Shared hooks

Open bbros-dev opened this issue 3 years ago • 7 comments

Thank you for the effort you have put into lefthook and for making it opensource.

It would be useful to be able to share hooks. since Pre-Commit already does this, consuming their convention would immediately provide a resource of hooks?

Or not, that doesn't matter.
The important idea is being able to share hooks - I suggest also looking at the git sub-repo as a way to manage shared repositories.

Pre-commit Example:

repos:
  - repo: git://github.com/antonbabenko/pre-commit-terraform
    rev: v1.30.0
    hooks:
      - id: terraform_fmt
      - id: terragrunt_fmt
      - id: terraform_docs
  - repo: git://github.com/pre-commit/pre-commit-hooks
    rev: v2.5.0
    hooks:
      - id: check-merge-conflict

bbros-dev avatar Oct 20 '20 12:10 bbros-dev

Wrting the code you wish you had...:

pre-commit:
  parallel: true
  commands:
    ...
  scripts:
    "terraform_tfsec.sh "
    ...
  repos:
     their-tfm:
       vendor: git
       repo: git://github.com/antonbabenko/pre-commit-terraform

This would populate a folder .lefthook-shared with the sub-repo / folder their-tfm The psuedo code:

mkdir .lefthook-shared
git sub-repo clone git://github.com/antonbabenko/pre-commit-terraform ./.lefthook-shared/their-tfm

Note that scripts references a script located here ./.lefthook-shared/their-tfm/terraform_tfsec.sh

Of course the above is PoC and the pre-commit repo scripts are not expected to work out the box.

bbros-dev avatar Oct 20 '20 12:10 bbros-dev

This would be nice. But I think we can also start porting pre_commit_hooks to Go and add to Lefthook. This would make using Lefthook nicer.

https://github.com/pre-commit/pre-commit-hooks/tree/master/pre_commit_hooks

aminya avatar Jun 03 '21 03:06 aminya

Wrting the code you wish you had...:

pre-commit:
  parallel: true
  commands:
    ...
  scripts:
    "terraform_tfsec.sh "
    ...
  repos:
     their-tfm:
       vendor: git
       repo: git://github.com/antonbabenko/pre-commit-terraform

This would populate a folder .lefthook-shared with the sub-repo / folder their-tfm The psuedo code:

mkdir .lefthook-shared
git sub-repo clone git://github.com/antonbabenko/pre-commit-terraform ./.lefthook-shared/their-tfm

Note that scripts references a script located here ./.lefthook-shared/their-tfm/terraform_tfsec.sh

Of course the above is PoC and the pre-commit repo scripts are not expected to work out the box.

Exactly why I found this issue. Don't really want to define our hook scripts in multiple repos and have to maintain that across repos. I couldn't find a way to do that with lefthook, am I missing something?

kunickiaj avatar Sep 13 '21 18:09 kunickiaj

Any idea if this is ever going to happen?

kunickiaj avatar Jun 21 '22 16:06 kunickiaj

Adding a repo of scripts for lefthook and referring it from the config might be helpful.

I would really appreciate if you could provide the way you see it. Any use case example would be helpful.

mrexox avatar Jun 22 '22 16:06 mrexox

I think the OP's example is pretty much exactly the same as my use case. I've used (and currently use pre-commit) to run hooks across many repos. Reference terraform pre-commit hooks in my config so they aren't defined per repo, just configured per repo.

kunickiaj avatar Jun 22 '22 16:06 kunickiaj

@mrexox I was looking into leveraging this functionality recently and came across this issue. I've opened up #323 with an implementation of this functionality. I've attempted to keep Lefthook as fast as possible when processing remote configs, so all git processing is done within a goroutine. I hope to contribute this feature to the project as I'm a big fan, and appreciate all feedback! 😄

oatovar avatar Aug 27 '22 08:08 oatovar

The proposed config solution for repos sound really cool.

In the meantime I ended up with a different solution for sharing hooks. I publish an npm package containing the shared hooks (e.g. lefthook-shared). Then I install lefthook-shared in all projects consuming the shared hooks and reference the node_modules location of the package.

Example lefthook.yml configuration of consuming project:

source_dir: "./node_modules/lefthook-shared/lefthook"
pre-commit:
  scripts:
    "script.sh":
      runner: bash

JuliusHenke avatar Oct 19 '22 08:10 JuliusHenke

Whoa! This is a really cool hack! :100:

BTW I hope to finish the PR with remote hooks support and release it with 1.2.0 in about 2 weeks

mrexox avatar Oct 19 '22 08:10 mrexox