From d93249eeab12f18563b895da3113715234f157c3 Mon Sep 17 00:00:00 2001 From: Arne Welzel Date: Fri, 6 Dec 2024 12:43:39 +0100 Subject: [PATCH] pre-commit: Add license-header check inspired by Spicy --- .pre-commit-config.yaml | 11 +++++++++++ ci/license-header.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100755 ci/license-header.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1997a9be57..b0fc21d692 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,6 +2,17 @@ # See https://pre-commit.com/hooks.html for more hooks # repos: +- repo: local + hooks: + - id: license + name: Check for license headers + entry: ./ci/license-header.py + language: python + types_or: + - "c" + - "c++" + exclude: '^(testing/btest/plugins/.*|testing/builtin-plugins/.*)$' + - repo: https://github.com/pre-commit/mirrors-clang-format rev: 'v18.1.8' hooks: diff --git a/ci/license-header.py b/ci/license-header.py new file mode 100755 index 0000000000..2af95aba0c --- /dev/null +++ b/ci/license-header.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 + +import sys +import re + +exit_code = 0 + +pat1 = re.compile(r"See the file \"COPYING\" in the main distribution directory for copyright.") + +# This is the copyright line used within Spicy plugin and popular in +# Spicy analyzers. +pat2 = re.compile(r"Copyright \(c\) 2... by the Zeek Project. See COPYING for details.") + + +def match_line(line): + for pat in [pat1, pat2]: + m = pat.search(line) + if m is not None: + return True + + return False + + +for f in sys.argv[1:]: + has_license_header = False + with open(f) as fp: + for line in fp: + line = line.strip() + if has_license_header := match_line(line): + break + + if not has_license_header: + print(f"{f}:does not seem to contain a license header", file=sys.stderr) + exit_code = 1 + +sys.exit(exit_code)