Skip to content

adehad/pre-commit-hooks

Repository files navigation

pre_commit_hooks


Table of Contents

Usage

check-header-footer

Some example usages of this are to check for the License or Copyright header. For the footer the use case presented in the example is for a specific doxygen tag,

Arguments:

  • --lines: How many lines to search from the top of the file.
  • --footer-lines: How many lines to search from the bottom of the file.
  • --header-config: <config_filename>:comma,separated,rules to apply to the header check.
  • --footer-config: <config_filename>:comma,separated,rules to apply to the footer check.

The config file is a json of {rule: expected lines}. The lines are separated using an array. Internally we use a regex comparison to find the exact strings.

// config.json Example
{
    "license": [
        "# SPDX-FileCopyrightText: 2022-present adehad <[email protected]>",
        "#",
        "# SPDX-License-Identifier: GPL-3.0-or-later"
    ],
    "c_license": [
        "/*",
        " * Copyright (c) 2023 COMPANY_NAME",
        " * All rights reserved",
        " */"
    ],
    "doxygen": [
        "/// @}"
    ]
}

With pre-commit we can specify the types_or for specific file types, and restrict to a subset of files. Running header and footer checks at the same time.

repos:
  - repo: https://github.com/adehad/pre-commit-hooks
    rev: main
    hooks:
      - id: check-header-footer
        files: ^Source/(?!Generated).*
        types_or: [c, c++]
        args:
          - --header-config=config.json:c_license
          - --footer-config=config.json:doxygen

or for verbosity the header and footer stages can be isolated, this can be used for checking the header and footer separated (see below) or can be used to select different rules for different languages.

repos:
  - repo: https://github.com/adehad/pre-commit-hooks
    rev: main
    hooks:
      - id: check-header-footer
        name: Checking for License
        files: ^Source/(?!Generated).*
        types_or: [c, c++]
        args:
          - --header-config=config.json:c_license
  - repo: https://github.com/adehad/pre-commit-hooks
    rev: main
    hooks:
      - id: check-header-footer
        name: Checking for Doxygen file group
        files: ^Source/(?!Generated).*
        types_or: [c, c++]
        args:
          - --footer-config=config.json:doxygen

Future work:

  1. Support a year parameter that can be used to apply fixes.

Local Installation

pipx install git+https://github.com/adehad/pre-commit-hooks.git@main

Development

hatch shell
hatch run lint
hatch run docs
hatch run cov

License

pre_commit_hooks is distributed under the terms of the GPL-3.0-or-later license.