已合并
chore: add pre-commit checks #34
chore: add pre-commit checks #34
已合并
汪超创建于 9月15日
共 2 个文件变更+65-1
@@ -1,2 +1 @@
1"""Deterministic retrieval support for knowledge-query."""1"""Deterministic retrieval support for knowledge-query."""
2- 
@@ -0,0 +1,65 @@
1+# ----------------------------------------------------------------------------------------------------------
2+# Copyright (c) 2026 Huawei Technologies Co., Ltd.
3+# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4+# CANN Open Software License Agreement Version 2.0 (the "License").
5+# Please refer to the License for details. You may not use this file except in compliance with the License.
6+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7+# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8+# See LICENSE in the root of the software repository for the full text of the License.
9+# ----------------------------------------------------------------------------------------------------------
10+ 
11+minimum_pre_commit_version: 4.0.0
12+default_install_hook_types: [pre-commit, pre-push]
13+default_stages: [pre-commit]
14+ 
15+repos:
16+ # Use the CANN community GitCode mirrors also used by ops-nn.
17+ - repo: https://gitcode.com/pre-commit/pre-commit-hooks
18+ rev: v6.0.0
19+ hooks:
20+ # LICENSE is byte-for-byte governed. knowledge/ can contain preserved
21+ # whitespace from official documentation mirrors, so neither is rewritten.
22+ - id: trailing-whitespace
23+ exclude: ^(?:LICENSE$|knowledge/)
24+ - id: end-of-file-fixer
25+ exclude: ^(?:LICENSE$|knowledge/)
26+ - id: check-yaml
27+ args: [--allow-multiple-documents]
28+ - id: check-json
29+ - id: check-toml
30+ - id: check-added-large-files
31+ - id: check-merge-conflict
32+ - id: detect-private-key
33+ - id: check-symlinks
34+ 
35+ - repo: https://gitcode.com/gh_mirrors/ru/ruff-pre-commit
36+ rev: v0.15.22
37+ hooks:
38+ # check.sh intentionally enforces linting without repository-wide formatting.
39+ - id: ruff-check
40+ files: ^(?:\.agents/skills/|governance/|evals/).+\.py$
41+ args: [--output-format, concise]
42+ 
43+ - repo: local
44+ hooks:
45+ - id: shell-syntax
zhanghz1
zhanghz1zhanghz19月16日

pre-commit 会把匹配的文件批量追加到 entry 后面,实际执行形如 bash -n a.sh b.sh c.sh。Bash 只解析第一个脚本,其余路径成为该脚本的位置参数,不会被检查。因此,一次提交多个 Shell 文件时,只要错误文件没有排在某个执行批次的首位,Check shell syntax 就会错误地显示通过。

已复现: 在隔离 Git 仓库中原样使用该 hook,暂存 00.sh 至 15.sh 共 16 个 Bash 脚本;前 15 个内容合法,只有 15.sh 含非法语句 if then。使用 pre-commit 4.6.2、默认并发配置执行:

pre-commit run shell-syntax --all-files -v
→ Check shell syntax ... Passed;退出码 0

bash -n 15.sh
→ syntax error near unexpected token `then';退出码 2

这会使本 PR 新增的提交前检查无法可靠拦截 Shell 语法错误,不是单纯的提示或配置风格问题。

必须调整: 通过包装脚本逐个遍历 "$@",对每个文件执行 bash -n "$file",遇错返回非零。仅添加 require_serial: true 不能解决问题,因为串行执行仍会传入多个文件。包装逻辑可采用:

for file in "$@"; do
    bash -n "$file" || exit $?
done

增加“同批次包含合法脚本及非首位错误脚本”的回归。已在临时夹具中验证上述修正:原错误样例被拒绝,pre-commit 退出 1;将错误脚本改为合法内容后退出 0。

likedislike
46+ name: Check shell syntax
47+ entry: bash -c 'for file do bash -n -- "$file" || exit $?; done' --
48+ language: system
49+ types: [shell]
50+ 
51+ - id: license-policy
52+ name: Check CANN 2.0 license policy
53+ entry: python3 -m unittest evals.test_format_check.LicenseComplianceTests
54+ language: system
55+ files: ^(?:LICENSE$|OAT\.xml$|[^/]+\.sh$|(?:governance|evals)/.*\.(?:py|ya?ml)$)
56+ pass_filenames: false
57+ 
58+ - id: full-quality-gate
59+ name: Run full repository quality gate
60+ entry: bash check.sh
61+ language: system
62+ stages: [pre-push]
63+ always_run: true
64+ pass_filenames: false
65+ require_serial: true