-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck.sh
More file actions
148 lines (121 loc) · 3.33 KB
/
check.sh
File metadata and controls
148 lines (121 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "$SCRIPT_DIR/lib/json-node.sh"
require_node
print_help() {
cat <<'EOF'
Usage: check.sh
Report managed-file drift for the current repo.
EOF
}
for arg in "$@"; do
case "$arg" in
--help|-h) print_help; exit 0 ;;
*)
echo "Unknown argument: $arg" >&2
print_help >&2
exit 1
;;
esac
done
node - <<'NODE'
const crypto = require("crypto");
const fs = require("fs");
const path = require("path");
const cwd = process.cwd();
const manifestPath = path.join(cwd, ".codex-sdlc", "manifest.json");
function printJson(value) {
process.stdout.write(`${JSON.stringify(value, null, 2)}\n`);
}
function sha256File(filePath) {
const hash = crypto.createHash("sha256");
hash.update(fs.readFileSync(filePath));
return `sha256:${hash.digest("hex")}`;
}
if (!fs.existsSync(manifestPath)) {
printJson({
repo_state: "uninitialized",
reason: "manifest_missing",
managed_files: {}
});
process.exit(0);
}
let manifest;
try {
manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
} catch (error) {
printJson({
repo_state: "broken",
reason: "manifest_invalid",
error: error.message,
managed_files: {}
});
process.exit(0);
}
function hasPlatformHookDrift(relativePath, absolutePath) {
if (relativePath !== ".codex/hooks.json") {
return false;
}
const content = fs.readFileSync(absolutePath, "utf8");
if (content.includes(".codex/hooks/git-guard.js") || content.includes(".codex/hooks/session-start.js")) {
return true;
}
if (process.platform === "win32") {
return content.includes("bash-guard.sh") || content.includes("session-start.sh");
}
return content.includes("powershell.exe");
}
function hasManagedHookSurfaceDrift(relativePath, absolutePath) {
if (relativePath !== ".codex/hooks.json") {
return false;
}
const content = fs.readFileSync(absolutePath, "utf8");
return !content.includes('"PreCompact"') ||
!content.includes('"PostCompact"') ||
!content.includes("node .codex/hooks/compact-guard.cjs");
}
function isRetiredManagedPath(relativePath) {
return relativePath === ".codex/hooks/git-guard.js" || relativePath === ".codex/hooks/session-start.js";
}
const managedFiles = {};
const summary = {
match: 0,
missing: 0,
customized: 0,
"drift / broken": 0
};
for (const [relativePath, expectedHash] of Object.entries(manifest.managed_files || {})) {
if (!expectedHash) {
continue;
}
const absolutePath = path.join(cwd, relativePath);
let actualHash = "";
let status;
if (!fs.existsSync(absolutePath)) {
status = "missing";
} else {
actualHash = sha256File(absolutePath);
if (isRetiredManagedPath(relativePath) || hasPlatformHookDrift(relativePath, absolutePath)) {
status = "drift / broken";
} else if (actualHash === expectedHash) {
status = hasManagedHookSurfaceDrift(relativePath, absolutePath) ? "drift / broken" : "match";
} else {
status = "customized";
}
}
managedFiles[relativePath] = {
status,
expected_hash: expectedHash,
actual_hash: actualHash
};
summary[status] += 1;
}
printJson({
repo_state: "initialized",
adapter_version: manifest.adapter_version || "",
scan: manifest.scan || {},
summary,
managed_files: managedFiles
});
NODE