Repository navigation
Expand file tree
/
Copy pathverify_pin.py
More file actions
executable file
·211 lines (180 loc) · 6.6 KB
/
Copy pathverify_pin.py
File metadata and controls
executable file
·211 lines (180 loc) · 6.6 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python3
"""Check that an integrated app's control-skill folder in its checkout is the pinned commit.
python3 -I verify_pin.py <checkout> <app> <sha>
Implements "Checkout off the pin" in reference/control-skill.md; that section is the rule. <sha> is
pins.<app> from the including control skill's committed config, or an empty string when unset.
Exit 0: the folder is the pinned one. Exit 1: refused; stderr carries the contract's message and a
`reason:` line naming the first check that failed. Exit 2: usage. Writes nothing into the checkout.
"""
import os
import re
import subprocess
import sys
SHA = re.compile(r"[0-9a-f]{40}|[0-9a-f]{64}")
APP = re.compile(r"[A-Za-z0-9_-][A-Za-z0-9._-]*")
ENV = {k: v for k, v in os.environ.items() if not k.startswith("GIT_")}
class Refused(Exception):
pass
def git(top, *args, stdin=None):
return subprocess.run(
["git", "--no-replace-objects", "--literal-pathspecs", "-C", top]
+ ["-c", "core.fsmonitor=false", *args],
input=stdin,
capture_output=True,
env=ENV,
check=False,
)
def git_ok(top, *args, stdin=None, why):
res = git(top, *args, stdin=stdin)
if res.returncode != 0:
raise Refused(why)
return res.stdout
def blob_id(top, data):
why = "git hash-object failed"
out = git_ok(top, "hash-object", "--no-filters", "--stdin", stdin=data, why=why)
return out.strip().decode()
def records(out):
return [r for r in out.split(b"\0") if r]
def show(path):
return os.fsdecode(path).encode("unicode_escape").decode("ascii")
def walk(root, rel):
"""Yield (relative path, absolute path) of every non-directory entry, never following a link."""
with os.scandir(root) as it:
for entry in it:
sub = rel + b"/" + entry.name
if entry.is_dir(follow_symlinks=False):
yield from walk(entry.path, sub)
else:
yield sub, entry.path
def check(checkout, app, sha):
if not APP.fullmatch(app):
raise Refused(f"app name {show(os.fsencode(app))} is not a folder name")
if not os.path.isdir(checkout):
raise Refused("the checkout is not a directory")
top = os.path.realpath(checkout)
shown = git_ok(
top, "rev-parse", "--show-toplevel", why="the checkout is not a git checkout"
)
if os.path.realpath(os.fsdecode(shown.rstrip(b"\n"))) != top:
raise Refused("the checkout is not the top of its repository")
rel_dir = os.fsencode(f".claude/skills/control-{app}")
abs_dir = os.path.join(os.fsencode(top), rel_dir)
if not os.path.isdir(abs_dir) or os.path.realpath(abs_dir) != abs_dir:
raise Refused(f"{show(rel_dir)} is missing or passes through a symlink")
pathspec = rel_dir.decode() + "/"
git_ok(
top,
"cat-file",
"-e",
"--end-of-options",
f"{sha}^{{commit}}",
why="the pin is not a commit in the checkout's object store",
)
diff = git(
top,
"diff",
"--quiet",
"--no-ext-diff",
"--no-textconv",
"--end-of-options",
sha,
"HEAD",
"--",
pathspec,
)
if diff.returncode == 1:
raise Refused(f"HEAD differs from the pin in {pathspec}")
if diff.returncode != 0:
raise Refused("git diff against HEAD failed")
for rec in records(
git_ok(top, "ls-files", "-v", "-z", "--", pathspec, why="git ls-files failed")
):
tag, _, path = rec.partition(b" ")
if tag != b"H":
raise Refused(f"index flag {tag.decode(errors='replace')} on {show(path)}")
tree = {}
for rec in records(
git_ok(
top,
"ls-tree",
"-r",
"-z",
"--end-of-options",
sha,
"--",
pathspec,
why="git ls-tree failed",
)
):
meta, _, path = rec.partition(b"\t")
mode, _, oid = meta.split(b" ")
tree[path] = (mode, oid.decode())
local_json = rel_dir + b"/config/local.json"
walked = {}
for rel, path in walk(abs_dir, rel_dir):
walked[rel] = path
if rel == local_json:
continue
if rel not in tree:
raise Refused(f"untracked or ignored {show(rel)}")
mode, oid = tree[rel]
if mode == b"120000":
text = git_ok(top, "cat-file", "blob", oid, why="git cat-file failed")
if not os.path.islink(path) or os.readlink(path) != text:
raise Refused(f"symlink {show(rel)} does not match the pin")
try:
target = os.path.realpath(path, strict=True)
except OSError:
raise Refused(f"symlink {show(rel)} does not resolve") from None
if os.path.commonpath([target, abs_dir]) != abs_dir:
raise Refused(f"symlink {show(rel)} resolves outside the folder")
continue
if os.path.islink(path) or not os.path.isfile(path):
raise Refused(f"{show(rel)} is not a regular file")
with open(path, "rb") as f:
data = f.read()
forms = [data] if b"\r\n" not in data else [data, data.replace(b"\r\n", b"\n")]
if not any(blob_id(top, d) == oid for d in forms):
raise Refused(f"{show(rel)} differs from the pin")
for path, (mode, _) in tree.items():
if mode != b"160000" and path not in walked:
raise Refused(f"missing {show(path)}")
paths = sorted(set(tree) | set(walked))
out = git_ok(
top,
"check-attr",
"--stdin",
"-z",
"filter",
stdin=b"".join(p + b"\0" for p in paths),
why="git check-attr failed",
)
fields = records(out)
for i in range(0, len(fields) - 2, 3):
if fields[i + 2] != b"unspecified":
raise Refused(f"{show(fields[i])} has a filter attribute")
def main(argv):
if len(argv) != 4:
print("usage: verify_pin.py <checkout> <app> <sha>", file=sys.stderr)
return 2
checkout, app, sha = argv[1:]
setting = f"pins.{app}"
if not sha:
print(
f"{setting} is unset: commit {setting}, for example through /app-control:map",
file=sys.stderr,
)
return 1
try:
if not SHA.fullmatch(sha):
raise Refused("not 40 or 64 lowercase hex characters")
check(checkout, app, sha)
except Refused as why:
print(
f"{setting} {show(os.fsencode(sha))}: fetch or check out", file=sys.stderr
)
print(f"reason: {why}", file=sys.stderr)
return 1
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))