licensecheck

package
v0.25.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2026 License: MPL-2.0 Imports: 12 Imported by: 0

README

License Check

This module provides helper functions to validating and remediating any problems with a LICENSE file.

Entry

The Entry(dirPath string) function takes in a directory path and will do the following:

  • Check if any files appear to be licenses
  • If no files are found, a stubbed out addLicenseFile function is called
  • If a file is found but it does not adhere to the LICENSE desired nomenclature, it will be renamed
  • If a file is found that matches the desired naming scheme, it is left alone
  • If multiple files are found, an error will be returned

License File Criteria

Potential LICENSE files are found by searching all files in a directory to find matching files with the name LICENSE with or without .txt or .md extensions in a case-insensitive manner. As an example, the following all qualify:

  • LICENSE
  • LICENSE.txt
  • LICENSE.md
  • license.TXT
  • LiCeNsE (for those who woke up and chose chaos)

Testing

Due to the nature of mutating the filesystem, some functions in this module are not suited to being tested with a more common testdata paradigm. Instead, testing/TempDir() is used to generate an ephemeral testing directory for each sub-test.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddHeader

func AddHeader(filePath string, header string) error

AddHeader prepends a given string to a file. It will automatically handle newline characters

func AddLicenseFile

func AddLicenseFile(dirPath string, spdxID string) (string, error)

AddLicenseFile creates a file named "LICENSE" in the target directory pre-populated with license text based on the SPDX Identifier you supply. Returns the fully qualified path to the license file it created

NOTE: this function will NOT add a copyright statement for you. You must manually call AddHeader() afterward if you wish to have copyright headers

func EnsureCorrectName

func EnsureCorrectName(filePath string) (string, error)

EnsureCorrectName fixes a malformed license file name and returns the new (corrected) file path E.g., "license.txt" --> "LICENSE"

func FindLicenseFiles

func FindLicenseFiles(dirPath string) ([]string, error)

FindLicenseFiles returns a list of filepaths for licenses in a given directory

func GetRepoFirstCommitYear added in v0.23.0

func GetRepoFirstCommitYear(workingDir string) (int, error)

GetRepoFirstCommitYear returns the year of the first commit in the repository

func GetRepoLastCommitYear added in v0.23.0

func GetRepoLastCommitYear(workingDir string) (int, error)

GetRepoLastCommitYear returns the year of the last commit in the repository

func GetRepoRoot added in v0.25.0

func GetRepoRoot(workingDir string) (string, error)

GetRepoRoot finds the git repository root from a given directory

func HasCopyright

func HasCopyright(filePath string) (bool, error)

HasCopyright reports whether or not a file contains a copyright statement It makes no promises as to the validity of the copyright statement, however! If you wish to validate the contents of the statement, use hasValidCopyright

func HasMatchingCopyright

func HasMatchingCopyright(filePath string, copyrightStatement string, caseSensitive bool) (bool, error)

HasMatchingCopyright takes an explicit copyright statement and validates that a given file contains that string in the header (first 1k chars)

func InitializeGitCache added in v0.25.0

func InitializeGitCache(repoRoot string) error

func NeedsUpdate added in v0.23.0

func NeedsUpdate(filePath string, targetHolder string, configYear int, forceCurrentYear bool) (bool, error)

NeedsUpdate checks if a file would be updated without actually modifying it If forceCurrentYear is true, forces end year to current year regardless of git history Returns true if the file has copyrights matching targetHolder that need year updates

func NeedsUpdateWithCache added in v0.25.0

func NeedsUpdateWithCache(filePath string, targetHolder string, configYear int, forceCurrentYear bool, repoFirstCommitYear int, repoRoot string) (bool, error)

NeedsUpdateWithCache checks if a file would be updated without actually modifying it If forceCurrentYear is true, forces end year to current year regardless of git history repoFirstYear and repoRoot can be provided to avoid repeated git lookups when processing multiple files Returns true if the file has copyrights matching targetHolder that need year updates

func UpdateCopyrightHeader added in v0.23.0

func UpdateCopyrightHeader(filePath string, targetHolder string, configYear int, forceCurrentYear bool) (bool, error)

UpdateCopyrightHeader updates all copyright headers in a file if needed If forceCurrentYear is true, forces end year to current year regardless of git history Returns true if the file was modified

func UpdateCopyrightHeaderWithCache added in v0.25.0

func UpdateCopyrightHeaderWithCache(filePath string, targetHolder string, configYear int, forceCurrentYear bool, repoFirstYear int, repoRoot string) (bool, error)

UpdateCopyrightHeaderWithCache updates all copyright headers in a file if needed If forceCurrentYear is true, forces end year to current year regardless of git history repoFirstYear and repoRoot can be provided to avoid repeated git lookups when processing multiple files Returns true if the file was modified

Types

type CopyrightInfo added in v0.23.0

type CopyrightInfo struct {
	LineNumber   int
	OriginalLine string
	Holder       string
	StartYear    int
	EndYear      int
	Prefix       string // Comment prefix (e.g., "// ", "# ")
	TrailingText string // Any text after the years
	PrefixIndex  int    // Byte index in the line where the comment prefix starts (for inline comments)
}

CopyrightInfo holds parsed copyright information from a file

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL