diff --git a/pre-commit-action/action.yml b/pre-commit-action/action.yml index aadeff5..f0a8260 100644 --- a/pre-commit-action/action.yml +++ b/pre-commit-action/action.yml @@ -40,6 +40,11 @@ inputs: Name of the reuse Jinja2 template in .reuse/templates/ (without .jinja2 suffix). Consumer repos can provide their own template. Defaults to "opensovd". required: false default: 'opensovd' + ignore-paths: + description: > + Comma-separated list of file patterns to ignore during REUSE checks (e.g., "*.md,docs/**,*.txt"). Files matching these patterns will be skipped by reuse annotate. + required: false + default: '' runs: using: 'composite' @@ -99,4 +104,5 @@ runs: --hook-script="${GITHUB_ACTION_PATH}/reuse-annotate-hook.py" \ --copyright="${{ inputs.copyright-text }}" \ --license="${{ inputs.license }}" \ - --template="${{ inputs.reuse-template }}" + --template="${{ inputs.reuse-template }}" \ + --ignore-paths="${{ inputs.ignore-paths }}" diff --git a/pre-commit-action/reuse-annotate-hook.py b/pre-commit-action/reuse-annotate-hook.py index 92c0cb1..ebb9add 100644 --- a/pre-commit-action/reuse-annotate-hook.py +++ b/pre-commit-action/reuse-annotate-hook.py @@ -51,6 +51,7 @@ DEFAULT_COPYRIGHT = "The Contributors to Eclipse OpenSOVD (see CONTRIBUTORS)" DEFAULT_LICENSE = "Apache-2.0" DEFAULT_TEMPLATE = "opensovd" +DEFAULT_IGNORE_PATHS = "" STYLES_CONFIG = ".reuse/styles.toml" @@ -163,10 +164,39 @@ def fix_wrong_copyright(filepath: str, copyright_text: str) -> None: path.write_text("".join(lines)) +def should_ignore(filepath: str, ignore_patterns: list[str]) -> bool: + """Check if a file should be ignored based on ignore patterns.""" + if not ignore_patterns: + return False + + # Normalize the filepath + filepath = filepath.replace("\\", "/") + + for pattern in ignore_patterns: + pattern = pattern.strip() + if not pattern: + continue + + # Support both simple glob patterns and path-based patterns + if fnmatch(filepath, pattern): + return True + + # Also check just the filename + basename = os.path.basename(filepath) + if fnmatch(basename, pattern): + return True + + return False + + def main() -> int: copyright_text = os.environ.get("REUSE_COPYRIGHT", DEFAULT_COPYRIGHT) license_id = os.environ.get("REUSE_LICENSE", DEFAULT_LICENSE) template = os.environ.get("REUSE_TEMPLATE", DEFAULT_TEMPLATE) + ignore_paths_str = os.environ.get("REUSE_IGNORE_PATHS", DEFAULT_IGNORE_PATHS) + + # Parse ignore patterns from comma-separated string + ignore_patterns = [p.strip() for p in ignore_paths_str.split(",") if p.strip()] files = sys.argv[1:] if not files: @@ -181,6 +211,10 @@ def main() -> int: styles = load_styles() for filepath in files: + # Skip ignored files + if should_ignore(filepath, ignore_patterns): + continue + # Never annotate license text files in the LICENSES/ directory if filepath.startswith("LICENSES/") or "/LICENSES/" in filepath: continue diff --git a/run_checks.py b/run_checks.py index e5214a5..925473f 100755 --- a/run_checks.py +++ b/run_checks.py @@ -70,7 +70,7 @@ def patch_config(config_content, *, fix_mode): def patch_hook_script( - script_content, *, copyright_text, license_id, template, fix_mode + script_content, *, copyright_text, license_id, template, ignore_paths, fix_mode ): """Patch the reuse-annotate hook script with configured values. @@ -89,6 +89,10 @@ def patch_hook_script( 'DEFAULT_TEMPLATE = "opensovd"', f'DEFAULT_TEMPLATE = "{template}"', ) + script_content = script_content.replace( + 'DEFAULT_IGNORE_PATHS = ""', + f'DEFAULT_IGNORE_PATHS = "{ignore_paths}"', + ) return script_content @@ -181,6 +185,11 @@ def main(): action="store_true", help="Run in check-only mode (no auto-fix). Used in CI to report issues without modifying files.", ) + parser.add_argument( + "--ignore-paths", + default="", + help="Comma-separated list of file patterns to ignore during REUSE checks (e.g., '*.md,docs/**,*.txt')", + ) args = parser.parse_args() @@ -249,6 +258,7 @@ def main(): copyright_text=args.copyright, license_id=args.license, template=args.template, + ignore_paths=args.ignore_paths, fix_mode=fix_mode, )