|
| 1 | +#!/bin/bash |
| 2 | +# ===----------------------------------------------------------------------===// |
| 3 | +# |
| 4 | +# This source file is part of the Swift.org open source project |
| 5 | +# |
| 6 | +# Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 7 | +# Licensed under Apache License v2.0 with Runtime Library Exception |
| 8 | +# |
| 9 | +# See https://swift.org/LICENSE.txt for license information |
| 10 | +# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 11 | +# |
| 12 | +# ===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +set -euo pipefail |
| 15 | + |
| 16 | +log() { printf -- "** %s\n" "$*" >&2; } |
| 17 | +error() { printf -- "** ERROR: %s\n" "$*" >&2; } |
| 18 | +fatal() { error "$@"; exit 1; } |
| 19 | + |
| 20 | +test -n "${PROJECT_NAME:-}" || fatal "PROJECT_NAME unset" |
| 21 | + |
| 22 | +expected_file_header_template="@@===----------------------------------------------------------------------===@@ |
| 23 | +@@ |
| 24 | +@@ This source file is part of the ${PROJECT_NAME} open source project |
| 25 | +@@ |
| 26 | +@@ Copyright (c) YEARS Apple Inc. and the ${PROJECT_NAME} project authors |
| 27 | +@@ Licensed under Apache License v2.0 |
| 28 | +@@ |
| 29 | +@@ See LICENSE.txt for license information |
| 30 | +@@ See CONTRIBUTORS.txt for the list of ${PROJECT_NAME} project authors |
| 31 | +@@ |
| 32 | +@@ SPDX-License-Identifier: Apache-2.0 |
| 33 | +@@ |
| 34 | +@@===----------------------------------------------------------------------===@@" |
| 35 | + |
| 36 | +paths_with_missing_license=( ) |
| 37 | + |
| 38 | +file_paths=$(tr '\n' '\0' < .licenseignore | xargs -0 -I% printf '":(exclude)%" '| xargs git ls-files) |
| 39 | + |
| 40 | +while IFS= read -r file_path; do |
| 41 | + file_basename=$(basename -- "${file_path}") |
| 42 | + file_extension="${file_basename##*.}" |
| 43 | + |
| 44 | + # shellcheck disable=SC2001 # We prefer to use sed here instead of bash search/replace |
| 45 | + case "${file_extension}" in |
| 46 | + swift) expected_file_header=$(sed -e 's|@@|//|g' <<<"${expected_file_header_template}") ;; |
| 47 | + h) expected_file_header=$(sed -e 's|@@|//|g' <<<"${expected_file_header_template}") ;; |
| 48 | + c) expected_file_header=$(sed -e 's|@@|//|g' <<<"${expected_file_header_template}") ;; |
| 49 | + sh) expected_file_header=$(cat <(echo '#!/bin/bash') <(sed -e 's|@@|##|g' <<<"${expected_file_header_template}")) ;; |
| 50 | + py) expected_file_header=$(cat <(echo '#!/usr/bin/env python3') <(sed -e 's|@@|##|g' <<<"${expected_file_header_template}")) ;; |
| 51 | + rb) expected_file_header=$(cat <(echo '#!/usr/bin/env ruby') <(sed -e 's|@@|##|g' <<<"${expected_file_header_template}")) ;; |
| 52 | + in) expected_file_header=$(sed -e 's|@@|##|g' <<<"${expected_file_header_template}") ;; |
| 53 | + cmake) expected_file_header=$(sed -e 's|@@|##|g' <<<"${expected_file_header_template}") ;; |
| 54 | + *) fatal "Unsupported file extension for file (exclude or update this script): ${file_path}" ;; |
| 55 | + esac |
| 56 | + expected_file_header_linecount=$(wc -l <<<"${expected_file_header}") |
| 57 | + |
| 58 | + file_header=$(head -n "${expected_file_header_linecount}" "${file_path}") |
| 59 | + normalized_file_header=$( |
| 60 | + echo "${file_header}" \ |
| 61 | + | sed -e 's/20[12][0123456789]-20[12][0123456789]/YEARS/' -e 's/20[12][0123456789]/YEARS/' \ |
| 62 | + ) |
| 63 | + |
| 64 | + if ! diff -u \ |
| 65 | + --label "Expected header" <(echo "${expected_file_header}") \ |
| 66 | + --label "${file_path}" <(echo "${normalized_file_header}") |
| 67 | + then |
| 68 | + paths_with_missing_license+=("${file_path} ") |
| 69 | + fi |
| 70 | +done <<< "$file_paths" |
| 71 | + |
| 72 | +if [ "${#paths_with_missing_license[@]}" -gt 0 ]; then |
| 73 | + fatal "❌ Found missing license header in files: ${paths_with_missing_license[*]}." |
| 74 | +fi |
| 75 | + |
| 76 | +log "✅ Found no files with missing license header." |
0 commit comments