google / addlicense

A program which ensures source code files have copyright license headers by scanning directory patterns recursively
Apache License 2.0
724 stars 169 forks source link

feature: add pre-commit support #138

Open Thomy90 opened 1 year ago

Thomy90 commented 1 year ago

Hi guys,

great tool. I have looked around and this tool is exactly what i am looking for.

I would like to use your tool in combination with pre-commit. The cool thing is then to run your tool during git hooks.

I have seen that for example the project golangci-lint also provide this pre-commit integration. https://github.com/golangci/golangci-lint/blob/master/.pre-commit-hooks.yaml

If you have questions, feel free to ask me.

Kind Regards, Thomy90

google-cla[bot] commented 1 year ago

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

willnorris commented 1 year ago

seems reasonable to me on the surface.

Some missing context here is that "pre-commit" seems to refer to https://pre-commit.com/ (and https://github.com/pre-commit). So it looks like we would just add this .pre-commit-hooks.yaml file, and then anyone that wanted to use addlicense would add an entry to their .pre-commit-config.yaml file to the effect of:

repos:
- repo: https://github.com/google/addlicense
  rev: v1.1.1
  hooks:
  - id: addlicense
    args: [ "-c", "Company, Inc", "*.go" ]

We don't need a .pre-commit-config.yaml file in addlicense unless we actually wanted to use pre-commit to run hooks ourselves.

Maybe add a comment at the top of the file that indicates that this is for use with pre-commit.com ?

Thomy90 commented 1 year ago

thanks a lot for your advice.

Yes, exactly. Your code snippet is the way i meant how addlicense can be used as a pre-commit hook.

Good point with the comment.

How about a extension in the readme with a code snippet to makes it clear how running addlicense with pre-commit?

For example:

Running with pre-commit

This is the official pre-commit hook for addlicense.

Activate by adding it to your .pre-commit-config.yaml:

repos:
- repo: https://github.com/google/addlicense
  rev: ''  # Use the sha / tag you want to point at
  hooks:
  - id: addlicense
    args: [ "-c", "Company, Inc", "*.go" ]
rickeylev commented 1 year ago

@mco-gh @willnorris Would one of you be willing to review/merge this PR? I, too, would like pre-commit to Just Work with addlicense