Merged
[pre-commit检查切换]: 将 OAT 检查切换为远程 pre-commit hook模式 #6927
liuliu876Add on Oct 23
[pre-commit检查切换]: 将 OAT 检查切换为远程 pre-commit hook模式 #6927
Merged
Merged fromliuliu876/asc-devkit_liuliufix_issueintocann/asc-devkitmaster
Total: 2 files changed+4-490
| @@ -63,18 +63,16 @@ repos: | |||
| 63 | "*.py,*.cpp,*.hpp,*.c,*.h,*.asc,*.cc", | 63 | "*.py,*.cpp,*.hpp,*.c,*.h,*.asc,*.cc", |
| 64 | ] | 64 | ] |
| 65 | 65 | ||
| 66 | - # OAT 合规性检查 (Open Source Audit Tool) | 66 | + # OAT 合规性检查 (Open Source Audit Tool) — 远程 Python hook |
| 67 | - - repo: local | 67 | + - repo: https://gitcode.com/HiAgent/compliance |
| 68 | + rev: v1.0.6 | ||
| 68 | hooks: | 69 | hooks: |
| 69 | - id: oat-check | 70 | - id: oat-check |
| 70 | - name: OAT Compliance Check | ||
| 71 | - entry: bash scripts/oat_check.sh | ||
| 72 | - language: system | ||
| 73 | pass_filenames: true | 71 | pass_filenames: true |
| 74 | - types: [file] | ||
| 75 | stages: [pre-commit] | 72 | stages: [pre-commit] |
| 76 | verbose: true | 73 | verbose: true |
| 77 | 74 | ||
| 75 | + | ||
| 78 | # Markdown 链接有效性检查 | 76 | # Markdown 链接有效性检查 |
| 79 | - repo: local | 77 | - repo: local |
| 80 | hooks: | 78 | hooks: |
| @@ -1,484 +0,0 @@ | |||
| 1 | -#!/bin/sh | ||
| 2 | -# ----------------------------------------------------------------------------------------------------------- | ||
| 3 | -# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 4 | -# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 5 | -# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 6 | -# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 7 | -# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 8 | -# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 9 | -# See LICENSE in the root of the software repository for the full text of the License. | ||
| 10 | -# ----------------------------------------------------------------------------------------------------------- | ||
| 11 | -# OAT Pre-commit Check Script — Python Edition (oat-py) | ||
| 12 | -# Replaces the Java-binary-based oat_check.sh. | ||
| 13 | -# | ||
| 14 | -# Requires: Python 3.7+ (pip install oat-py>=1.0.1) | ||
| 15 | -# Works on: Linux / macOS / Windows (Git Bash / MSYS2) | ||
| 16 | -# | ||
| 17 | -# Self-healing: strip Windows CRLF if present | ||
| 18 | -_SCRIPT="$(cd "$(dirname "$0")" && pwd)/$(basename "$0")" | ||
| 19 | -if sed 's/\r//' "$_SCRIPT" 2>/dev/null | diff -q - "$_SCRIPT" >/dev/null 2>&1; then | ||
| 20 | - : | ||
| 21 | -else | ||
| 22 | - echo "[OAT] CRLF detected, auto-fixing and re-running..." | ||
| 23 | - _TMP="${_SCRIPT}.lf" | ||
| 24 | - sed 's/\r//' "$_SCRIPT" > "$_TMP" && mv "$_TMP" "$_SCRIPT" | ||
| 25 | - exec sh "$_SCRIPT" "$@" | ||
| 26 | -fi | ||
| 27 | - | ||
| 28 | -set -e | ||
| 29 | - | ||
| 30 | -# --------------------------------------------------------------------------- | ||
| 31 | -# 0. Locate Python interpreter | ||
| 32 | -# --------------------------------------------------------------------------- | ||
| 33 | -_PYTHON="" | ||
| 34 | -for _candidate in python3 python py; do | ||
| 35 | - if command -v "$_candidate" >/dev/null 2>&1; then | ||
| 36 | - _VER=$("$_candidate" -c "import sys; print(sys.version_info >= (3,7))" 2>/dev/null || echo "False") | ||
| 37 | - if [ "$_VER" = "True" ]; then | ||
| 38 | - _PYTHON="$_candidate" | ||
| 39 | - break | ||
| 40 | - fi | ||
| 41 | - fi | ||
| 42 | -done | ||
| 43 | - | ||
| 44 | -if [ -z "$_PYTHON" ]; then | ||
| 45 | - echo "[OAT] [WARNING] Python 3.7+ is required but not found. Please install Python 3.7 or later." | ||
| 46 | - echo "[OAT] Skipping OAT check, continuing commit..." | ||
| 47 | - exit 0 | ||
| 48 | -fi | ||
| 49 | - | ||
| 50 | -# --------------------------------------------------------------------------- | ||
| 51 | -# 1. Ensure oat-py>=1.0.2 is installed (serialized via flock to prevent parallel pip conflicts) | ||
| 52 | -# --------------------------------------------------------------------------- | ||
| 53 | -_OAT_MIN="1.0.2" | ||
| 54 | - | ||
| 55 | -# Check oat-py version AND structural integrity (key modules must be importable). | ||
| 56 | -# Returns: "ok" | "old" | "broken" | "missing" | ||
| 57 | -_check_oat_version() { | ||
| 58 | - "$_PYTHON" -c " | ||
| 59 | -import importlib.util, sys | ||
| 60 | - | ||
| 61 | -# 1. Module presence check | ||
| 62 | -if not importlib.util.find_spec('oat'): | ||
| 63 | - print('missing'); sys.exit() | ||
| 64 | - | ||
| 65 | -# 2. Structural sanity: verify the key modules added in 1.0.x exist. | ||
| 66 | -# An old/partial installation may pass the version check but be unusable. | ||
| 67 | -# find_spec raises ModuleNotFoundError when the parent package is missing, | ||
| 68 | -# so wrap each check in try/except. | ||
| 69 | -for mod in ('oat.walker.directory_walker', 'oat.analysis.pipeline', 'oat.config.loader'): | ||
| 70 | - try: | ||
| 71 | - found = importlib.util.find_spec(mod) | ||
| 72 | - except (ModuleNotFoundError, ValueError): | ||
| 73 | - found = None | ||
| 74 | - if not found: | ||
| 75 | - print('broken'); sys.exit() | ||
| 76 | - | ||
| 77 | -# 3. Version comparison | ||
| 78 | -try: | ||
| 79 | - from importlib.metadata import version | ||
| 80 | - v = version('oat-py') | ||
| 81 | - parts_have = [int(x) for x in v.split('.')[:3]] | ||
| 82 | - parts_need = [int(x) for x in '${_OAT_MIN}'.split('.')[:3]] | ||
| 83 | - print('ok' if parts_have >= parts_need else 'old') | ||
| 84 | -except Exception: | ||
| 85 | - # Metadata unavailable (e.g. installed without wheel metadata). | ||
| 86 | - # Structural check already passed, treat as acceptable. | ||
| 87 | - print('ok') | ||
| 88 | -" 2>/dev/null || echo "missing" | ||
| 89 | -} | ||
| 90 | - | ||
| 91 | -_OAT_OK=$(_check_oat_version) | ||
| 92 | -if [ "$_OAT_OK" != "ok" ]; then | ||
| 93 | - case "$_OAT_OK" in | ||
| 94 | - old) echo "[OAT] oat-py is outdated. Upgrading to oat-py>=${_OAT_MIN} ..." ;; | ||
| 95 | - broken) echo "[OAT] oat-py installation is broken. Reinstalling oat-py>=${_OAT_MIN} ..." ;; | ||
| 96 | - *) echo "[OAT] oat-py not found. Installing oat-py>=${_OAT_MIN} ..." ;; | ||
| 97 | - esac | ||
| 98 | - _LOCK_FILE="/tmp/oat_pip_install.lock" | ||
| 99 | - # flock serialises concurrent pip installs; re-check inside so waiting | ||
| 100 | - # processes skip the install when the first one already finished. | ||
| 101 | - if command -v flock >/dev/null 2>&1; then | ||
| 102 | - ( | ||
| 103 | - flock -w 120 9 | ||
| 104 | - _RECHECK=$(_check_oat_version) | ||
| 105 | - if [ "$_RECHECK" != "ok" ]; then | ||
| 106 | - # Use --force-reinstall for broken installations to ensure a clean state. | ||
| 107 | - if [ "$_RECHECK" = "broken" ] || [ "$_OAT_OK" = "broken" ]; then | ||
| 108 | - "$_PYTHON" -m pip install --quiet --force-reinstall "oat-py>=${_OAT_MIN}" | ||
| 109 | - else | ||
| 110 | - "$_PYTHON" -m pip install --quiet "oat-py>=${_OAT_MIN}" | ||
| 111 | - fi | ||
| 112 | - fi | ||
| 113 | - ) 9>"$_LOCK_FILE" | ||
| 114 | - else | ||
| 115 | - if [ "$_OAT_OK" = "broken" ]; then | ||
| 116 | - "$_PYTHON" -m pip install --quiet --force-reinstall "oat-py>=${_OAT_MIN}" | ||
| 117 | - else | ||
| 118 | - "$_PYTHON" -m pip install --quiet "oat-py>=${_OAT_MIN}" | ||
| 119 | - fi | ||
| 120 | - fi | ||
| 121 | - _OAT_OK=$(_check_oat_version) | ||
| 122 | - if [ "$_OAT_OK" != "ok" ]; then | ||
| 123 | - echo "[OAT] [WARNING] Failed to install oat-py. Please run: pip install oat-py>=${_OAT_MIN}" | ||
| 124 | - echo "[OAT] Skipping OAT check, continuing commit..." | ||
| 125 | - exit 0 | ||
| 126 | - fi | ||
| 127 | - echo "[OAT] oat-py installed successfully." | ||
| 128 | -fi | ||
| 129 | - | ||
| 130 | -# --------------------------------------------------------------------------- | ||
| 131 | -# 2. Determine repo root and name | ||
| 132 | -# --------------------------------------------------------------------------- | ||
| 133 | -REPO_ROOT=$(git rev-parse --show-toplevel 2>/dev/null || pwd) | ||
| 134 | -REPO_NAME=$(basename "$REPO_ROOT") | ||
| 135 | -OAT_REPORT_DIR="${TMPDIR:-/tmp}/oat_reports_$$" | ||
| 136 | -OAT_RESULT_DIR="$REPO_ROOT/oat_reports" | ||
| 137 | - | ||
| 138 | -# --------------------------------------------------------------------------- | ||
| 139 | -# 2a. PR-range deduplication (pure git, no CI env vars required) | ||
| 140 | -# | ||
| 141 | -# Strategy: | ||
| 142 | -# 1. Find the merge-base between HEAD and the upstream branch (origin/master | ||
| 143 | -# or similar). If found, this is a feature-branch context ??collect ALL | ||
| 144 | -# files changed since the branch diverged (full PR diff). | ||
| 145 | -# 2. Key a done-marker on the HEAD SHA. The first invocation runs the scan; | ||
| 146 | -# every subsequent invocation for the same HEAD exits 0 immediately. | ||
| 147 | -# This eliminates redundant scans when CI calls the hook once per commit. | ||
| 148 | -# 3. If no merge-base is found (e.g. committing directly on master) fall back | ||
| 149 | -# to scanning only the currently staged files, with no done-marker. | ||
| 150 | -# --------------------------------------------------------------------------- | ||
| 151 | -_PR_MERGE_BASE="" | ||
| 152 | -_DONE_MARKER="" | ||
| 153 | -_HEAD_SHA=$(git rev-parse HEAD 2>/dev/null | cut -c1-12 || true) | ||
| 154 | - | ||
| 155 | -if [ -n "$_HEAD_SHA" ]; then | ||
| 156 | - # Try to find merge-base with a known upstream branch | ||
| 157 | - if [ -n "${PRE_COMMIT_FROM_REF:-}" ]; then | ||
| 158 | - # pre-commit --from-ref/--to-ref: use the provided base directly | ||
| 159 | - _PR_MERGE_BASE=$(git merge-base "$PRE_COMMIT_FROM_REF" HEAD 2>/dev/null || true) | ||
| 160 | - fi | ||
| 161 | - if [ -z "$_PR_MERGE_BASE" ]; then | ||
| 162 | - # Search all remotes (origin, upstream, etc.) for common trunk branch names. | ||
| 163 | - # This handles fork setups where origin=fork and upstream=main-repo. | ||
| 164 | - _CANDIDATE_BASE="" | ||
| 165 | - _CANDIDATE_DIST=0 | ||
| 166 | - for _b in $(git for-each-ref --format='%(refname:short)' \ | ||
| 167 | - 'refs/remotes/*/master' 'refs/remotes/*/main' \ | ||
| 168 | - 'refs/remotes/*/develop' 'refs/remotes/*/dev' 2>/dev/null); do | ||
| 169 | - _mb=$(git merge-base HEAD "$_b" 2>/dev/null || true) | ||
| 170 | - [ -z "$_mb" ] && continue | ||
| 171 | - # Skip if merge-base is HEAD itself (branch is ahead of or equal to HEAD) | ||
| 172 | - [ "$_mb" = "$(git rev-parse HEAD 2>/dev/null)" ] && continue | ||
| 173 | - # Pick the candidate whose merge-base is furthest from HEAD | ||
| 174 | - # (most commits since divergence = most likely true PR base) | ||
| 175 | - _dist=$(git rev-list --count "$_mb"..HEAD 2>/dev/null || echo 0) | ||
| 176 | - if [ -z "$_CANDIDATE_BASE" ] || [ "$_dist" -gt "$_CANDIDATE_DIST" ]; then | ||
| 177 | - _CANDIDATE_BASE="$_mb" | ||
| 178 | - _CANDIDATE_DIST="$_dist" | ||
| 179 | - fi | ||
| 180 | - done | ||
| 181 | - _PR_MERGE_BASE="$_CANDIDATE_BASE" | ||
| 182 | - fi | ||
| 183 | - | ||
| 184 | - # Only use PR-range mode when HEAD has diverged from the upstream base | ||
| 185 | - if [ -n "$_PR_MERGE_BASE" ] && [ "$_PR_MERGE_BASE" != "$(git rev-parse HEAD 2>/dev/null)" ]; then | ||
| 186 | - mkdir -p "$OAT_RESULT_DIR" | ||
| 187 | - _DONE_MARKER="$OAT_RESULT_DIR/.done_${_HEAD_SHA}" | ||
| 188 | - if [ -f "$_DONE_MARKER" ]; then | ||
| 189 | - echo "[OAT] [SKIP] Already scanned HEAD=${_HEAD_SHA}. Skipping duplicate invocation." | ||
| 190 | - exit 0 | ||
| 191 | - fi | ||
| 192 | - else | ||
| 193 | - # HEAD == merge-base: on the upstream branch itself, no PR range | ||
| 194 | - _PR_MERGE_BASE="" | ||
| 195 | - fi | ||
| 196 | -fi | ||
| 197 | - | ||
| 198 | -# --------------------------------------------------------------------------- | ||
| 199 | -# 2b. Deduplicate parallel invocations within the same pre-commit run. | ||
| 200 | -# Only the first instance that acquires the lock actually runs the scan; | ||
| 201 | -# all others wait then exit 0. | ||
| 202 | -# --------------------------------------------------------------------------- | ||
| 203 | -if command -v flock >/dev/null 2>&1; then | ||
| 204 | - _OAT_SCAN_LOCK="${TMPDIR:-/tmp}/oat_scan_$(echo "$REPO_ROOT" | tr '/\\: ' '____').lock" | ||
| 205 | - exec 9>"$_OAT_SCAN_LOCK" | ||
| 206 | - _WAITED_FOR_LOCK=0 | ||
| 207 | - if ! flock -n 9; then | ||
| 208 | - echo "[OAT] Another OAT scan instance is running. Waiting..." | ||
| 209 | - flock -w 120 9 | ||
| 210 | - _WAITED_FOR_LOCK=1 | ||
| 211 | - fi | ||
| 212 | - # Re-check done-marker after acquiring the lock � covers both paths: | ||
| 213 | - # (a) waited for lock: previous instance may have completed normally | ||
| 214 | - # (b) acquired immediately: previous holder may have already created the marker | ||
| 215 | - # before releasing the lock (race window between pre-flock check and flock acquire) | ||
| 216 | - if [ -n "$_DONE_MARKER" ] && [ -f "$_DONE_MARKER" ]; then | ||
| 217 | - echo "[OAT] Scan already completed by another instance. Skipping." | ||
| 218 | - exit 0 | ||
| 219 | - fi | ||
| 220 | - if [ "$_WAITED_FOR_LOCK" = "1" ]; then | ||
| 221 | - echo "[OAT] Previous instance did not complete. Proceeding with scan..." | ||
| 222 | - fi | ||
| 223 | - # This instance now owns the lock and will run the scan. | ||
| 224 | -fi | ||
| 225 | - | ||
| 226 | -echo "[OAT] Running OAT scan (Python Edition) ??INCREMENTAL MODE" | ||
| 227 | -echo "[OAT] Project: $REPO_NAME" | ||
| 228 | -if [ -n "$_PR_MERGE_BASE" ]; then | ||
| 229 | - echo "[OAT] Mode: PR range ??scanning all files changed since merge-base" | ||
| 230 | -else | ||
| 231 | - echo "[OAT] Mode: staged files ??scanning only currently staged files" | ||
| 232 | -fi | ||
| 233 | - | ||
| 234 | -# --------------------------------------------------------------------------- | ||
| 235 | -# 3. Collect staged files | ||
| 236 | -# --------------------------------------------------------------------------- | ||
| 237 | -if [ $# -gt 0 ]; then | ||
| 238 | - # Called directly with file arguments (e.g. manual test) | ||
| 239 | - FILE_COUNT=$# | ||
| 240 | - FILE_LIST="" | ||
| 241 | - for _f in "$@"; do | ||
| 242 | - # Convert relative paths to absolute | ||
| 243 | - case "$_f" in | ||
| 244 | - /*) _abs="$_f" ;; | ||
| 245 | - *) _abs="$REPO_ROOT/$_f" ;; | ||
| 246 | - esac | ||
| 247 | - [ -f "$_abs" ] || continue | ||
| 248 | - if [ -z "$FILE_LIST" ]; then | ||
| 249 | - FILE_LIST="$_abs" | ||
| 250 | - else | ||
| 251 | - FILE_LIST="$FILE_LIST,$_abs" | ||
| 252 | - fi | ||
| 253 | - done | ||
| 254 | -elif [ -n "$_PR_MERGE_BASE" ]; then | ||
| 255 | - # PR range mode: collect ALL files changed since the branch diverged | ||
| 256 | - _STAGED=$(git diff --name-only --diff-filter=ACM "$_PR_MERGE_BASE" HEAD \ | ||
| 257 | - 2>/dev/null || true) | ||
| 258 | - if [ -z "$_STAGED" ]; then | ||
| 259 | - echo "[OAT] No files changed in PR range. Skipping." | ||
| 260 | - [ -n "$_DONE_MARKER" ] && touch "$_DONE_MARKER" 2>/dev/null || true | ||
| 261 | - exit 0 | ||
| 262 | - fi | ||
| 263 | - FILE_COUNT=$(echo "$_STAGED" | wc -l | tr -d ' ') | ||
| 264 | - FILE_LIST="" | ||
| 265 | - while IFS= read -r _f; do | ||
| 266 | - [ -z "$_f" ] && continue | ||
| 267 | - case "$_f" in | ||
| 268 | - /*) _abs="$_f" ;; | ||
| 269 | - *) _abs="$REPO_ROOT/$_f" ;; | ||
| 270 | - esac | ||
| 271 | - [ -f "$_abs" ] || continue | ||
| 272 | - if [ -z "$FILE_LIST" ]; then | ||
| 273 | - FILE_LIST="$_abs" | ||
| 274 | - else | ||
| 275 | - FILE_LIST="$FILE_LIST,$_abs" | ||
| 276 | - fi | ||
| 277 | - done <<EOF | ||
| 278 | -$_STAGED | ||
| 279 | -EOF | ||
| 280 | -else | ||
| 281 | - # Staged files mode: on upstream branch directly, scan only staged files | ||
| 282 | - _STAGED=$(git diff --cached --name-only --diff-filter=ACM 2>/dev/null || true) | ||
| 283 | - if [ -z "$_STAGED" ]; then | ||
| 284 | - echo "[OAT] No staged files to check. Skipping." | ||
| 285 | - exit 0 | ||
| 286 | - fi | ||
| 287 | - FILE_COUNT=$(echo "$_STAGED" | wc -l | tr -d ' ') | ||
| 288 | - FILE_LIST="" | ||
| 289 | - while IFS= read -r _f; do | ||
| 290 | - [ -z "$_f" ] && continue | ||
| 291 | - case "$_f" in | ||
| 292 | - /*) _abs="$_f" ;; | ||
| 293 | - *) _abs="$REPO_ROOT/$_f" ;; | ||
| 294 | - esac | ||
| 295 | - [ -f "$_abs" ] || continue | ||
| 296 | - if [ -z "$FILE_LIST" ]; then | ||
| 297 | - FILE_LIST="$_abs" | ||
| 298 | - else | ||
| 299 | - FILE_LIST="$FILE_LIST,$_abs" | ||
| 300 | - fi | ||
| 301 | - done <<EOF | ||
| 302 | -$_STAGED | ||
| 303 | -EOF | ||
| 304 | -fi | ||
| 305 | - | ||
| 306 | -if [ -z "$FILE_LIST" ]; then | ||
| 307 | - echo "[OAT] No existing staged files found. Skipping." | ||
| 308 | - exit 0 | ||
| 309 | -fi | ||
| 310 | - | ||
| 311 | -echo "[OAT] Checking $FILE_COUNT staged file(s)..." | ||
| 312 | - | ||
| 313 | -# --------------------------------------------------------------------------- | ||
| 314 | -# 4. Ensure report directories exist | ||
| 315 | -# --------------------------------------------------------------------------- | ||
| 316 | -mkdir -p "$OAT_REPORT_DIR" | ||
| 317 | -mkdir -p "$OAT_RESULT_DIR" | ||
| 318 | - | ||
| 319 | -# --------------------------------------------------------------------------- | ||
| 320 | -# 5. Resolve OAT.xml path | ||
| 321 | -# --------------------------------------------------------------------------- | ||
| 322 | -_OAT_XML="$REPO_ROOT/OAT.xml" | ||
| 323 | -if [ ! -f "$_OAT_XML" ] && [ -f "$REPO_ROOT/scripts/OAT.xml" ]; then | ||
| 324 | - _OAT_XML="$REPO_ROOT/scripts/OAT.xml" | ||
| 325 | -fi | ||
| 326 | - | ||
| 327 | -# _OAT_CMD is kept for display purposes only (shown in error messages). | ||
| 328 | -# Actual execution uses direct argument passing below to avoid eval word-splitting | ||
| 329 | -# and command-injection risks when paths or filenames contain special characters. | ||
| 330 | -if [ -f "$_OAT_XML" ]; then | ||
| 331 | - _OAT_CMD="\"$_PYTHON\" -m oat -mode s -s \"$REPO_ROOT\" -r \"$OAT_REPORT_DIR\" -n \"$REPO_NAME\" -w 1 -f \"$FILE_LIST\" -oatconfig \"$_OAT_XML\"" | ||
| 332 | -else | ||
| 333 | - _OAT_CMD="\"$_PYTHON\" -m oat -mode s -s \"$REPO_ROOT\" -r \"$OAT_REPORT_DIR\" -n \"$REPO_NAME\" -w 1 -f \"$FILE_LIST\"" | ||
| 334 | -fi | ||
| 335 | - | ||
| 336 | -# --------------------------------------------------------------------------- | ||
| 337 | -# 6. Run oat scan (direct argument passing � no eval) | ||
| 338 | -# --------------------------------------------------------------------------- | ||
| 339 | -echo "" | ||
| 340 | -echo "[OAT] Running compliance scan..." | ||
| 341 | - | ||
| 342 | -set +e | ||
| 343 | -if [ -f "$_OAT_XML" ]; then | ||
| 344 | - "$_PYTHON" -m oat -mode s -s "$REPO_ROOT" -r "$OAT_REPORT_DIR" -n "$REPO_NAME" -w 1 -f "$FILE_LIST" -oatconfig "$_OAT_XML" >/dev/null 2>&1 | ||
| 345 | -else | ||
| 346 | - "$_PYTHON" -m oat -mode s -s "$REPO_ROOT" -r "$OAT_REPORT_DIR" -n "$REPO_NAME" -w 1 -f "$FILE_LIST" >/dev/null 2>&1 | ||
| 347 | -fi | ||
| 348 | -_OAT_RC=$? | ||
| 349 | -set -e | ||
| 350 | - | ||
| 351 | -if [ "$_OAT_RC" -ne 0 ] && [ "$_OAT_RC" -ne 1 ]; then | ||
| 352 | - echo "" | ||
| 353 | - echo "[OAT] [WARNING] oat exited with unexpected code $_OAT_RC." | ||
| 354 | - echo "[OAT] Try re-running manually:" | ||
| 355 | - echo " $_OAT_CMD" | ||
| 356 | - echo "[OAT] Skipping OAT check, continuing commit..." | ||
| 357 | - exit 0 | ||
| 358 | -fi | ||
| 359 | - | ||
| 360 | -# --------------------------------------------------------------------------- | ||
| 361 | -# 7. Parse report and write result.txt | ||
| 362 | -# Only: Invalid File Type + License Header Invalid (no copyright) | ||
| 363 | -# --------------------------------------------------------------------------- | ||
| 364 | -REPORT_FILE="$OAT_REPORT_DIR/PlainReport_${REPO_NAME}.txt" | ||
| 365 | -RESULT_FILE="$OAT_RESULT_DIR/result.txt" | ||
| 366 | - | ||
| 367 | -# Section headers used as stop-boundaries when extracting sections | ||
| 368 | -_ALL_HEADERS="Invalid File Type Total Count:|License Not Compatible Total Count:|License Header Invalid Total Count:|Copyright Header Invalid Total Count:|No License File Total Count:|No Readme.OpenSource Total Count:|No Readme Total Count:|Import Invalid Total Count:|Redundant License File Total Count:|Third Party Software Info Total Count:" | ||
| 369 | - | ||
| 370 | -# Extract one section from the report (header line + detail Name: lines) | ||
| 371 | -# Usage: _extract_section <file> <start_marker> | ||
| 372 | -_extract_section() { | ||
| 373 | - _file="$1" | ||
| 374 | - _marker="$2" | ||
| 375 | - awk -v marker="$_marker" -v boundaries="$_ALL_HEADERS" ' | ||
| 376 | - BEGIN { capturing=0 } | ||
| 377 | - !capturing { | ||
| 378 | - if (index($0, marker) == 1) { capturing=1; print; next } | ||
| 379 | - next | ||
| 380 | - } | ||
| 381 | - capturing { | ||
| 382 | - # Check if this line starts a different section header | ||
| 383 | - n = split(boundaries, hdrs, "|") | ||
| 384 | - for (i=1; i<=n; i++) { | ||
| 385 | - if (hdrs[i] != marker && index($0, hdrs[i]) == 1) { exit } | ||
| 386 | - } | ||
| 387 | |||
| 388 | - } | ||
| 389 | - ' "$_file" | sed '/^[[:space:]]*$/{ N; /^\n$/d }' | awk ' | ||
| 390 | - { lines[NR]=$0 } END { while(NR>0 && lines[NR]~/^[[:space:]]*$/) NR--; for(i=1;i<=NR;i++) print lines[i] } | ||
| 391 | - ' | ||
| 392 | -} | ||
| 393 | - | ||
| 394 | -if [ ! -f "$REPORT_FILE" ]; then | ||
| 395 | - if [ "$_OAT_RC" -eq 0 ]; then | ||
| 396 | - # oat exited cleanly with no report: all staged files were filtered out | ||
| 397 | - echo "[OAT] [OK] All checks passed ($FILE_COUNT file(s) checked)." | ||
| 398 | - [ -n "$_DONE_MARKER" ] && touch "$_DONE_MARKER" 2>/dev/null || true | ||
| 399 | - rm -rf "$OAT_REPORT_DIR" | ||
| 400 | - exit 0 | ||
| 401 | - else | ||
| 402 | - # oat returned exit code 1 but produced no report �?possible disk issue | ||
| 403 | - # or oat internal error. Do not silently pass; block the commit. | ||
| 404 | - echo "" | ||
| 405 | - echo "[OAT] [ERROR] oat exited with code $_OAT_RC but no report was generated." | ||
| 406 | - echo "[OAT] This may indicate a disk error or an oat internal bug." | ||
| 407 | - echo "[OAT] To investigate, run manually:" | ||
| 408 | - echo " $_OAT_CMD" | ||
| 409 | - echo "[OAT] Blocking commit to prevent silent compliance bypass." | ||
| 410 | - echo "" | ||
| 411 | - rm -rf "$OAT_REPORT_DIR" | ||
| 412 | - exit 1 | ||
| 413 | - fi | ||
| 414 | -fi | ||
| 415 | - | ||
| 416 | -# Parse counts ??use || true to prevent set -e from triggering if grep finds no match | ||
| 417 | -_INVALID_TYPE=$(grep "^Invalid File Type Total Count:" "$REPORT_FILE" | grep -oE '[0-9]+' | head -1 || true) | ||
| 418 | -_LICENSE_INVALID=$(grep "^License Header Invalid Total Count:" "$REPORT_FILE" | grep -oE '[0-9]+' | head -1 || true) | ||
| 419 | -_INVALID_TYPE=${_INVALID_TYPE:-0} | ||
| 420 | -_LICENSE_INVALID=${_LICENSE_INVALID:-0} | ||
| 421 | - | ||
| 422 | -# Extract detail sections | ||
| 423 | -_SECTION_TYPE=$(_extract_section "$REPORT_FILE" "Invalid File Type Total Count:") | ||
| 424 | -_SECTION_LIC=$(_extract_section "$REPORT_FILE" "License Header Invalid Total Count:") | ||
| 425 | - | ||
| 426 | -# Fallback to bare count line if section is empty | ||
| 427 | -[ -n "$_SECTION_TYPE" ] || _SECTION_TYPE="Invalid File Type Total Count: $_INVALID_TYPE" | ||
| 428 | -[ -n "$_SECTION_LIC" ] || _SECTION_LIC="License Header Invalid Total Count: $_LICENSE_INVALID" | ||
| 429 | - | ||
| 430 | -# Write result.txt | ||
| 431 | -{ | ||
| 432 | - echo "===================================" | ||
| 433 | - echo "OAT Scan Result Summary" | ||
| 434 | - echo "===================================" | ||
| 435 | - printf "Scan Time: %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" | ||
| 436 | - echo "Project: $REPO_NAME" | ||
| 437 | - echo "Files Checked: $FILE_COUNT" | ||
| 438 | - echo "" | ||
| 439 | - echo "-----------------------------------" | ||
| 440 | - echo "$_SECTION_TYPE" | ||
| 441 | - echo "" | ||
| 442 | - echo "-----------------------------------" | ||
| 443 | - echo "$_SECTION_LIC" | ||
| 444 | - echo "===================================" | ||
| 445 | -} > "$RESULT_FILE" | ||
| 446 | - | ||
| 447 | -# --------------------------------------------------------------------------- | ||
| 448 | -# 8. Block commit if issues found; always clean up temp dir | ||
| 449 | -# --------------------------------------------------------------------------- | ||
| 450 | -TOTAL_ISSUES=$(( _INVALID_TYPE + _LICENSE_INVALID )) | ||
| 451 | - | ||
| 452 | -if [ "$TOTAL_ISSUES" -gt 0 ]; then | ||
| 453 | - echo "" | ||
| 454 | - echo "====================================================================" | ||
| 455 | - echo " OAT: Compliance issues found. Commit blocked." | ||
| 456 | - echo "====================================================================" | ||
| 457 | - echo "" | ||
| 458 | - echo "[OAT] Found $TOTAL_ISSUES compliance issue(s):" | ||
| 459 | - echo " - Invalid File Type: $_INVALID_TYPE" | ||
| 460 | - echo " - License Header Invalid: $_LICENSE_INVALID" | ||
| 461 | - echo "" | ||
| 462 | - echo "[OAT] Details (also saved to: $RESULT_FILE):" | ||
| 463 | - echo "---" | ||
| 464 | - cat "$RESULT_FILE" | ||
| 465 | - echo "---" | ||
| 466 | - echo "" | ||
| 467 | - echo "Fix the issues and recommit, or skip with:" | ||
| 468 | - echo " git commit --no-verify" | ||
| 469 | - echo "" | ||
| 470 | - rm -rf "$OAT_REPORT_DIR" | ||
| 471 | - exit 1 | ||
| 472 | -fi | ||
| 473 | - | ||
| 474 | -echo "" | ||
| 475 | -echo "[OAT] [OK] All checks passed ($FILE_COUNT file(s) checked)." | ||
| 476 | -echo "[OAT] Summary: cat $RESULT_FILE" | ||
| 477 | -echo "" | ||
| 478 | -# Mark scan as done for CI range mode (prevents redundant re-runs on same PR head) | ||
| 479 | -if [ -n "$_DONE_MARKER" ]; then | ||
| 480 | - touch "$_DONE_MARKER" 2>/dev/null || true | ||
| 481 | - echo "[OAT] Done-marker created: $_DONE_MARKER" | ||
| 482 | -fi | ||
| 483 | -rm -rf "$OAT_REPORT_DIR" | ||
| 484 | -exit 0 | ||