|
| 1 | +#!/bin/bash |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +# Commit message hook to require Developer Certificate of Origin sign-off. |
| 18 | +# Install with: ./scripts/install-hooks.sh |
| 19 | + |
| 20 | +set -euo pipefail |
| 21 | + |
| 22 | +RED='\033[0;31m' |
| 23 | +GREEN='\033[0;32m' |
| 24 | +YELLOW='\033[1;33m' |
| 25 | +NC='\033[0m' |
| 26 | + |
| 27 | +COMMIT_MSG_FILE="${1:-}" |
| 28 | + |
| 29 | +if [[ -z "$COMMIT_MSG_FILE" || ! -f "$COMMIT_MSG_FILE" ]]; then |
| 30 | + echo -e "${RED}ERROR: commit-msg hook did not receive a commit message file.${NC}" |
| 31 | + exit 1 |
| 32 | +fi |
| 33 | + |
| 34 | +trim() { |
| 35 | + local value="$1" |
| 36 | + value="${value#"${value%%[![:space:]]*}"}" |
| 37 | + value="${value%"${value##*[![:space:]]}"}" |
| 38 | + printf '%s' "$value" |
| 39 | +} |
| 40 | + |
| 41 | +trailers="$(git interpret-trailers --parse "$COMMIT_MSG_FILE")" |
| 42 | +valid_signoff_count=0 |
| 43 | +invalid_signoffs=() |
| 44 | +git_user_name="$(git config user.name || true)" |
| 45 | +git_user_email="$(git config user.email || true)" |
| 46 | + |
| 47 | +while IFS= read -r line; do |
| 48 | + token="$(trim "${line%%:*}")" |
| 49 | + if [[ "${token,,}" != "signed-off-by" ]]; then |
| 50 | + continue |
| 51 | + fi |
| 52 | + |
| 53 | + value="$(trim "${line#*:}")" |
| 54 | + if [[ "$value" =~ ^[^[:space:]\<\>][^\<\>]*[[:space:]]\<[^\<\>@[:space:]]+@[^\<\>[:space:]]+\>$ ]] \ |
| 55 | + && [[ "${value,,}" != *"your name"* ]] \ |
| 56 | + && [[ "${value,,}" != *"your.email@example.com"* ]]; then |
| 57 | + ((valid_signoff_count += 1)) |
| 58 | + else |
| 59 | + invalid_signoffs+=("$value") |
| 60 | + fi |
| 61 | +done <<< "$trailers" |
| 62 | + |
| 63 | +if [[ "$valid_signoff_count" -gt 0 ]]; then |
| 64 | + echo -e "${GREEN}✓ DCO sign-off present${NC}" |
| 65 | + exit 0 |
| 66 | +fi |
| 67 | + |
| 68 | +echo -e "${RED}ERROR: commit message is missing a valid DCO sign-off.${NC}" |
| 69 | +echo "" |
| 70 | +if [[ -n "$git_user_name" && -n "$git_user_email" ]]; then |
| 71 | + echo "Add a Signed-off-by trailer using your real name and email:" |
| 72 | + echo "" |
| 73 | + echo " Signed-off-by: $git_user_name <$git_user_email>" |
| 74 | + echo "" |
| 75 | +else |
| 76 | + echo "Add a Signed-off-by trailer using your real name and email." |
| 77 | + echo "" |
| 78 | +fi |
| 79 | +echo "Usually you can let Git add this automatically:" |
| 80 | +echo "" |
| 81 | +echo " git commit -s" |
| 82 | +echo " git commit --amend -s" |
| 83 | +echo "" |
| 84 | + |
| 85 | +if [[ "${#invalid_signoffs[@]}" -gt 0 ]]; then |
| 86 | + echo -e "${YELLOW}Found malformed sign-off trailer(s):${NC}" |
| 87 | + for signoff in "${invalid_signoffs[@]}"; do |
| 88 | + echo " Signed-off-by: $signoff" |
| 89 | + done |
| 90 | + echo "" |
| 91 | +fi |
| 92 | + |
| 93 | +echo "The organization DCO check is the final enforcement gate in CI; this hook catches" |
| 94 | +echo "the issue before you push." |
| 95 | + |
| 96 | +exit 1 |
0 commit comments