addlicense

repository·master·Indexed 21 days ago

https://github.com/google/addlicense

A command-line tool that ensures source code files contain appropriate copyright license headers by scanning directories recursively and modifying files in place. It supports license types including apache, bsd, mit, and mpl, and can be integrated via Go, Docker, or as a pre-commit hook.

Tokens
1.3K
Snippets
5
Records
8
Agent score
25%

What's inside addlicense

  1. Use addlicense to add license headers

    master

    The addlicense tool scans directory patterns recursively and modifies source files in place to ensure they have copyright license headers. It avoids adding a header if one is already present.

    Basic Usage: addlicense [flags] pattern [pattern ...]

    Patterns can be single files or directories (which are processed recursively).

    # Run across everything in the current directory and all subdirectories
    addlicense .
  2. Integrate addlicense with pre-commit

    master

    You can use addlicense as an official pre-commit hook. Add the following configuration to your .pre-commit-config.yaml file. Replace <latest tag> with the actual latest version tag from the repository.

    repos:
    - repo: https://github.com/google/addlicense
      rev: <latest tag>
      hooks:
      - id: addlicense
        args: [ "-c", "Company, Inc", "*.go" ]
  3. Run addlicense in a Docker Container

    master

    You can run addlicense using a Docker image from the GitHub Container Registry. To process files, mount your local directory to /src inside the container.

    1. Pull the image:

    docker pull ghcr.io/google/addlicense:latest

    2. Run against local files: Mount the current working directory to /src and pass the target files/patterns. Note that since the files are mounted at /src, you should target the files relative to that mount or use the patterns provided to the tool.

    Note: The example below uses *.go which will look for go files in the working directory of the container (which is /src if mounted correctly).

    # Pull the image
    docker pull ghcr.io/google/addlicense:latest
    
    # Run it by mounting the current directory to /src
    docker run -it -v ${PWD}:/src ghcr.io/google/addlicense -c "Google LLC" *.go
  4. How addlicense identifies generated files and existing licenses

    master

    To prevent accidental modification of sensitive files, addlicense uses the following logic:

    Generated Files

    Files are considered generated and will be skipped if they contain specific markers:

    • Go: Code generated .* DO NOT EDIT.
    • Cargo Raze: DO NOT EDIT! Replaced on runs of cargo-raze$

    Existing Licenses

    A file is considered to already have a license if the first 1000 bytes contain any of the following (case-insensitive):

    • copyright
    • mozilla public
    • spdx-license-identifier
  5. Use addlicense CLI to manage copyright headers

    master

    addlicense is a command-line tool that ensures source code files have copyright license headers. It scans directory patterns recursively, modifies files in place, and avoids adding headers to files that already contain them or are marked as generated.

    Basic Usage

    addlicense [flags] pattern [pattern ...]

    Key Features

    • In-place modification: Automatically inserts headers into source files.
    • Pattern matching: Supports multiple patterns and recursive directory scanning.
    • Safety: Detects and skips files that already have a license or are identified as generated code.
    • Check mode: Can be used in CI/CD to verify presence of headers without modifying files.
    addlicense .
  6. Reference addlicense CLI flags

    master

    The following flags are available for the addlicense command:

    FlagDescription
    -cCopyright holder (default "Google LLC")
    -checkCheck only mode: verify presence of license headers and exit with non-zero code if missing
    -fLicense file
    -ignoreFile patterns to ignore (supports doublestar patterns, e.g., -ignore vendor/**)
    -lLicense type: apache, bsd, mit, mpl (default "apache")
    -sInclude SPDX identifier in license header. Use -s=only to include only the SPDX identifier
    -vVerbose mode: print the names of the files that are modified
    -yCopyright year(s) (default is the current year)
  7. Configure addlicense flags

    master

    Use the following flags to customize how addlicense applies headers:

    FlagShorthandDefaultDescription
    -c--holderGoogle LLCThe copyright holder name
    -l--licenseapacheLicense type: apache, bsd, mit, mpl
    -f--licensef(empty)Path to a custom license file
    -y--yearCurrent YearThe copyright year(s)
    -s--spdx(off)Include SPDX identifier. Use -s=only to include only the SPDX identifier
    -v--verbosefalsePrint names of modified or skipped files
    --check(none)falseVerify presence of headers and exit with non-zero code if missing
    --ignore(none)(none)File patterns to ignore (e.g., -ignore **/*.go -ignore vendor/**)
    --skip(none)(none)Deprecated: Use --ignore instead. Accepts file extensions (e.g., -skip rb)