chore: 收敛模板证据哈希并接入提交钩子
This commit is contained in:
1 parent
ec0cf78d69
commit
669d4a7de6
7 files changed
+146
-15
No files matched your search
@@ -431,32 +431,55 @@ function walkRepositoryFiles(root, directory = root) {
|
||||
return files;
|
||||
}
|
||||
|
||||
export function sourceTreeHash(root) {
|
||||
const EVIDENCE_SCOPE_IGNORED_PREFIXES = [".codex-tasks/", ".gitea/", "deploy/", "scripts/deploy/"];
|
||||
const EVIDENCE_SCOPE_IGNORED_EXTENSIONS = [".md", ".mdx"];
|
||||
const EVIDENCE_SCOPE_ALWAYS_INCLUDED = ["docs/template-completion-rules.md"];
|
||||
|
||||
// 证据哈希只覆盖会影响模板交付的源码:文档、CI 配置和部署脚本的改动不使证据失效。
|
||||
export function isEvidenceSourcePath(relativePath) {
|
||||
const normalized = relativePath.split(path.sep).join("/");
|
||||
if (EVIDENCE_SCOPE_ALWAYS_INCLUDED.includes(normalized)) return true;
|
||||
if (EVIDENCE_SCOPE_IGNORED_PREFIXES.some((prefix) => normalized.startsWith(prefix))) return false;
|
||||
return !EVIDENCE_SCOPE_IGNORED_EXTENSIONS.some((extension) => normalized.endsWith(extension));
|
||||
}
|
||||
|
||||
export function sourceTreeHash(root, options = {}) {
|
||||
const rootPath = path.resolve(root);
|
||||
const { staged = false } = options;
|
||||
let files;
|
||||
let readContent;
|
||||
if (fs.existsSync(path.join(rootPath, ".git"))) {
|
||||
const output = execFileSync(
|
||||
"git",
|
||||
["ls-files", "-z", "--cached", "--others", "--exclude-standard"],
|
||||
{ cwd: rootPath, maxBuffer: 64 * 1024 * 1024 },
|
||||
);
|
||||
// 只统计已跟踪文件:未纳入版本控制的文件不属于任何一次可复现的交付。
|
||||
// staged 模式从 Git index 读取内容,供 pre-commit 使用,避免部分暂存时把
|
||||
// 工作区里的未提交改动算进提交对应的证据哈希。
|
||||
const output = execFileSync("git", ["ls-files", "-z", "--cached"], {
|
||||
cwd: rootPath,
|
||||
maxBuffer: 64 * 1024 * 1024,
|
||||
});
|
||||
files = output
|
||||
.toString("utf8")
|
||||
.split("\0")
|
||||
.filter((file) => file && !file.startsWith(".codex-tasks/"))
|
||||
.map((file) => path.join(rootPath, file))
|
||||
.filter((file) => fs.existsSync(file) && fs.statSync(file).isFile());
|
||||
.filter((file) => file && isEvidenceSourcePath(file))
|
||||
.map((file) => path.join(rootPath, file));
|
||||
readContent = staged
|
||||
? (file) =>
|
||||
execFileSync("git", ["show", ":" + path.relative(rootPath, file)], {
|
||||
cwd: rootPath,
|
||||
maxBuffer: 64 * 1024 * 1024,
|
||||
})
|
||||
: (file) => fs.readFileSync(file);
|
||||
} else {
|
||||
files = walkRepositoryFiles(rootPath).filter(
|
||||
(file) => !path.relative(rootPath, file).startsWith(".codex-tasks/"),
|
||||
files = walkRepositoryFiles(rootPath).filter((file) =>
|
||||
isEvidenceSourcePath(path.relative(rootPath, file)),
|
||||
);
|
||||
readContent = (file) => fs.readFileSync(file);
|
||||
}
|
||||
files.sort();
|
||||
const hash = crypto.createHash("sha256");
|
||||
for (const file of files) {
|
||||
hash.update(path.relative(rootPath, file));
|
||||
hash.update("\0");
|
||||
hash.update(fs.readFileSync(file));
|
||||
hash.update(readContent(file));
|
||||
hash.update("\0");
|
||||
}
|
||||
return hash.digest("hex");
|
||||
|
||||
Reference in new issue
Block a user