add-license-headers setup#

The add-license-headers hook adds and updates license headers in specified directories and file types using REUSE. Here is a sample license header for a Python file generated by the template with a start year of 2023:

Picture of Python file's license header in light mode Picture of Python file's license header in dark mode

To get started, add the hook to your .pre-commit-config.yaml file:

- repo: https://github.com/ansys/pre-commit-hooks
  rev: v0.5.2
  hooks:
  - id: add-license-headers

The hook runs on protocol buffer (PROTO) files in any directory, as well as Python files within directories named src, examples, and tests.

If your repository was created before the current year, add the --start_year argument with the year that your first file was committed. The ansys-pre-commit-hooks repository started in 2023, so the .pre-commit-config.yaml file looks like this:

- repo: https://github.com/ansys/pre-commit-hooks
  rev: v0.5.2
  hooks:
  - id: add-license-headers
    args:
    - --start_year=2023

By default, the ansys.jinja2 template is used to generate the license headers for files.

The template contains the following variables:

  • {{ copyright_line }}: Contains the current year and the phrase “ANSYS, Inc. and/or its affiliates.” by default.

  • {{ expression }}: Contains the SPDX-License-Identifier expression, which is “MIT” by default.

add-license-headers hook arguments#

Argument

Default value

Description

--custom_copyright

ANSYS, Inc. and/or its affiliates.

Copyright line to include in the license header.

--custom_template

ansys

Name of the .jinja2 template file located in the .reuse/templates/ directory.

--custom_license

MIT

SPDX-License-Identifier expression to include in the license header. To view the list of valid SPDX license expressions, see the SPDX License List.

--start_year

2023

Year that the first file was committed to the repository.

--ignore_license_check

False

Whether to check for the license in the header.

Frequently asked questions#

How do you specify additional directories and files to run the hook on?

To specify additional files and/or directories the hook should run on, add the necessary regex to the files line in your .pre-commit-config.yaml file:

- repo: https://github.com/ansys/pre-commit-hooks
  rev: v0.5.2
  hooks:
  - id: add-license-headers
    files: '(src|examples|tests|doc)/.*\.(py|rst)|\.(proto|cpp)'

This would run the hook on Python and ReStructuredText (RST) files in the src, examples, tests, and doc directories, as well as PROTO and CPP files in any directory.

Note

The default regex for the files field is '(src|examples|tests)/.*\.(py)|\.(proto)'. Add onto this regex to specify additional files and directories to ensure that the hook runs on Python files in the src, examples, and tests directories, as well as PROTO files in any directory at the minimum.

How do you ignore specific files or file types?

To ignore specific files or file types, add the exclude argument to the hook in your .pre-commit-config.yaml file:

- repo: https://github.com/ansys/pre-commit-hooks
  rev: v0.5.2
  hooks:
  - id: add-license-headers
    exclude: |
        (?x)^(
            path/to/file1.py |
            path/to/.*\.(ts|cpp) |
            (.folder1|folder2)/.* |
            .*\.js |
            \..* |
        )$
  • path/to/file1.py excludes the stated file.

  • path/to/.*\.(ts|cpp) excludes all TS and CPP files within the path/to directory.

  • (.folder1|folder2)/.* excludes directories named .folder1 and folder2.

  • .*\.js excludes all JS files in all directories.

  • \..* excludes all hidden files.

How do you change the copyright phrase from “ANSYS, Inc. and/or its affiliates.”?

To change the copyright phrase in the copyright line, add the --custom_copyright argument to the hook in your .pre-commit-config.yaml file:

- repo: https://github.com/ansys/pre-commit-hooks
  rev: v0.5.2
  hooks:
  - id: add-license-headers
    args:
    - --custom_copyright="custom copyright phrase"

This would change the copyright line to Copyright (C) 2025 custom copyright phrase.

How do you ignore checking for licensing information in the files?

To ignore checking for the MIT license in the files, add the --ignore_license_check argument to the hook in your .pre-commit-config.yaml file:

- repo: https://github.com/ansys/pre-commit-hooks
  rev: v0.5.2
  hooks:
  - id: add-license-headers
    args:
    - --ignore_license_check
How do you use a custom template?

To use a custom template, create the .reuse/templates/ directory in the root of your repository and add the Jinja template to that directory. The custom template cannot be named ansys.jinja2. Otherwise, it would be removed after the hook is done running.

project
├── .reuse
│   └── templates
│       └── template_name.jinja2
├── src
├── examples
├── tests
├── .pre-commit-config.yaml
├── pyproject.toml

Add the --custom_template argument to the hook in your .pre-commit-config.yaml file:

- repo: https://github.com/ansys/pre-commit-hooks
  rev: v0.5.2
  hooks:
  - id: add-license-headers
    args:
    - --custom_template=template_name
How do you use a custom license?

To use a custom license, create the LICENSES directory in the root of your repository and add the license to that directory. The custom license cannot be named MIT.txt. Otherwise, it would be removed after the hook is done running.

project
├── LICENCES
│   └── license_name.txt
├── src
├── examples
├── tests
├── .pre-commit-config.yaml
├── pyproject.toml

To use a custom license, add the --custom_license argument to the hook in your .pre-commit-config.yaml file:

- repo: https://github.com/ansys/pre-commit-hooks
  rev: v0.5.2
  hooks:
  - id: add-license-headers
    args:
    - --custom_license=license_name

Licenses supported by REUSE can be found in the spdx/license-list-data repository. Select a license text file from that repository and copy it to the LICENSES directory.

What should the start year be if my repository was created before the current year?

If you are adding license headers to repositories that were started prior to the current year, add the --start_year argument with the year that your first file was committed. For example, if start_year is 2023 and the current year is 2025, the copyright statement would be:

Copyright (C) 2023 - 2025 ANSYS, Inc. and/or its affiliates.

Known issues and limitations#

After running the hook, I am seeing a “Skipped unrecognized file” message

The hook skips files that are not recognized by REUSE. If you see this message, the file type is not supported by the hook. For a list of supported file types, see the EXTENSION_COMMENT_STYLE_MAP in the reuse-tool repository.

To request support for an unrecognized file type, open an issue in the ansys/pre-commit-hooks repository.