Repository navigation
Expand file tree
/
Copy pathverify_backports.py
More file actions
431 lines (381 loc) · 22.7 KB
/
Copy pathverify_backports.py
File metadata and controls
431 lines (381 loc) · 22.7 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/usr/bin/env python3
"""Verify that a patch level is built only from php/php-src code.
For each branch of versions.json built from a repository other than php/php-src:
- the base commit must have the tree of the official release tag (README.md aside, and the
files of base_exceptions in versions.json, for a branch based on its "-dev" version bump);
- every commit between the base and the pinned commit must come from php/php-src.
Its official counterpart is found through the "cherry picked from commit" trailer,
or else by author and subject, and the two patches are compared with git patch-id.
Official counterparts are looked up on the official branches of the same version
(PHP-x.y and the PHP-x.y-* fix branches) first, then on the supported branches.
GitHub serves every commit of the fork network under php/php-src, backport branches'
included, so a counterpart must be on a branch or tag of a clone of php/php-src. A
trailer that names a commit of another backport branch is followed through that commit's
own trailers.
A commit passes when it is identical to its official counterpart; when it differs, or
has no counterpart, only in files that do not go into the binaries (NEWS, tests,
README.md, .github); or when it adds and removes the same lines as its counterpart, in
hunks of the same functions, so that only the surrounding context differs (the older
branch's code around the change). A merge commit passes when its tree is the one git
computes from its parents (git merge-tree), so that it brings no change of its own; the
commits it merges are verified one by one. Any other commit must be reviewed in
verification/reviews.json.
Writes verification/<branch>.md and exits 1 when a commit is not reviewed.
Commits and trees fetched by their full id are cached in the temporary directory, and
commit searches for a day, so that a rerun after a review is quick.
Usage: verify_backports.py [branch ...] (default: every branch of versions.json)
The VERSIONS environment variable names another file than versions.json, for a branch
that is verified before it is built (PHP 5.6).
"""
import datetime
import difflib
import hashlib
import json
import os
import re
import shutil
import subprocess
import sys
import tempfile
import time
import urllib.error
import urllib.parse
import urllib.request
OFFICIAL = 'php/php-src'
TOKEN = os.environ.get('GITHUB_TOKEN', '')
BASE_EXCEPTIONS = {'README.md'}
CHERRY_PICK = re.compile(r'cherry picked from commit ([0-9a-f]{40})')
ADVISORY = re.compile(r'GHSA(?:-[0-9a-z]{4}){3}|CVE-\d{4}-\d{4,}')
# Above this many lines, the difference between an adapted file's two patches is replaced in the
# report by one line when the file is the same after both commits (same blob id)
LONG_DIFF = 1000
# API responses kept between runs, so that a rerun after a review only fetches what may change
CACHE = os.path.join(tempfile.gettempdir(), 'php-src-verify-cache')
IMMUTABLE = re.compile(r'/repos/[^/]+/[^/]+/(commits|git/trees)/[0-9a-f]{40}(\?recursive=1)?$')
def cache_ttl(path):
"""Seconds a response stays cached: None for ever (a commit or tree named by its full id
does not change), one day for a commit search, False when it is not cached."""
if IMMUTABLE.match(path):
return None
return 86400 if path.startswith('/search/') else False
def request(path, accept='application/vnd.github+json'):
ttl = cache_ttl(path)
cached = os.path.join(CACHE, hashlib.sha256(f'{accept} {path}'.encode()).hexdigest())
if ttl is not False and os.path.exists(cached) and (ttl is None or time.time() - os.path.getmtime(cached) < ttl):
with open(cached, encoding='utf-8') as f:
text = f.read()
return json.loads(text) if accept.endswith('json') else text
headers = {'Accept': accept, 'X-GitHub-Api-Version': '2022-11-28'}
if TOKEN:
headers['Authorization'] = 'Bearer ' + TOKEN
for attempt in range(6):
try:
with urllib.request.urlopen(urllib.request.Request('https://api.github.com' + path, headers=headers)) as r:
text = r.read().decode('utf-8', 'replace')
if ttl is not False:
os.makedirs(CACHE, exist_ok=True)
with open(cached, 'w', encoding='utf-8') as f:
f.write(text)
return json.loads(text) if accept.endswith('json') else text
except urllib.error.HTTPError as error:
if error.code in (404, 422):
return None
if error.code in (403, 429) and attempt < 5: # rate limits, the search API's in particular
time.sleep(15 * (attempt + 1))
continue
raise
def tree(repo, commit):
sha = request(f'/repos/{repo}/commits/{commit}')['commit']['tree']['sha']
data = request(f'/repos/{repo}/git/trees/{sha}?recursive=1')
if data['truncated']:
sys.exit(f'{repo}@{commit}: tree listing truncated')
return {item['path']: item['sha'] for item in data['tree'] if item['type'] == 'blob'}
def patch(repo, sha):
return request(f'/repos/{repo}/commits/{sha}', 'application/vnd.github.patch')
def official_commits():
"""Commits of the branches and tags of php/php-src, from a clone without trees (about
50 MB) kept in the temporary directory and fetched again at each run."""
path = os.path.join(tempfile.gettempdir(), 'php-src-official.git')
if os.path.isdir(path) and not os.path.isfile(os.path.join(path, 'HEAD')):
# Left incomplete by a cleanup of the temporary directory; git packs are read-only
shutil.rmtree(path, onexc=lambda func, p, _: (os.chmod(p, 0o600), func(p)))
if os.path.isdir(path):
subprocess.run(['git', '-C', path, 'fetch', '--quiet', '--prune', 'origin',
'+refs/heads/*:refs/heads/*', '+refs/tags/*:refs/tags/*'], check=True)
else:
subprocess.run(['git', 'clone', '--quiet', '--bare', '--filter=tree:0',
f'https://github.com/{OFFICIAL}.git', path], check=True)
out = subprocess.run(['git', '-C', path, 'rev-list', '--all'], capture_output=True, text=True, check=True)
return set(out.stdout.split())
def branch_clone(repo, branch, since):
"""Clone of a branch's history since a date, commits and trees only (blobs come on demand),
kept in the temporary directory: what git merge-tree needs to recompute its merges."""
path = os.path.join(tempfile.gettempdir(), 'php-src-verify-' + repo.replace('/', '-') + '.git')
if os.path.isdir(path) and not os.path.isfile(os.path.join(path, 'HEAD')):
shutil.rmtree(path, onexc=lambda func, p, _: (os.chmod(p, 0o600), func(p)))
if os.path.isdir(path):
subprocess.run(['git', '-C', path, 'fetch', '--quiet', f'--shallow-since={since}', 'origin',
f'+refs/heads/{branch}:refs/heads/{branch}'], check=True)
else:
subprocess.run(['git', 'clone', '--quiet', '--bare', '--filter=blob:none', f'--shallow-since={since}',
'--single-branch', '--branch', branch, f'https://github.com/{repo}.git', path], check=True)
return path
def merge_check(path, commit):
"""('clean merge', []) when the merge commit has the tree git computes from its parents,
else ('merge with changes', the differences from that tree)."""
parents = [p['sha'] for p in commit['parents']]
if len(parents) != 2:
return 'merge with changes', [f'# {len(parents)} parents: not checked']
out = subprocess.run(['git', '-C', path, 'merge-tree', '--write-tree', '--no-messages', *parents],
capture_output=True, text=True)
computed = out.stdout.split()[0] if out.stdout.strip() else None
actual = subprocess.run(['git', '-C', path, 'rev-parse', commit['sha'] + '^{tree}'],
capture_output=True, text=True, check=True).stdout.strip()
if out.returncode == 0 and computed == actual:
return 'clean merge', []
if computed is None:
return 'merge with changes', [f'# git merge-tree failed: {out.stderr.strip()}']
diff = subprocess.run(['git', '-C', path, 'diff', computed, actual], capture_output=True, text=True,
encoding='utf-8', errors='replace', check=True).stdout
return 'merge with changes', [f'# differences from the tree git computes from the parents'
f'{" (conflicts)" if out.returncode == 1 else ""}'] + diff.splitlines()
def official_sha(sha, official):
"""Full id of a commit of php/php-src, None for a commit of a fork."""
if sha in official:
return sha
matches = [s for s in official if s.startswith(sha)] if len(sha) < 40 else []
return matches[0] if len(matches) == 1 else None
def patch_id(text):
out = subprocess.run(['git', 'patch-id', '--stable'], input=text.encode('utf-8'), capture_output=True, check=True)
return out.stdout.split()[0].decode() if out.stdout else None
def files(text):
"""Patch content per file, without index lines and hunk line numbers."""
result, current = {}, None
for line in text.splitlines():
match = re.match(r'diff --git a/(\S+) b/', line)
if match:
current = match.group(1)
result[current] = []
elif current and not line.startswith('index '):
result[current].append(re.sub(r'^@@ -\d+(,\d+)? \+\d+(,\d+)? @@', '@@', line))
if result: # format-patch signature at the end of the last file
last = result[current]
while last and (last[-1] in ('--', '') or re.fullmatch(r'\d+(\.\d+)+.*', last[-1])):
last.pop()
return result
def changed_lines(lines):
"""Added and removed lines of a file's patch, whitespace-normalised, as sorted lists."""
def pick(sign):
return sorted(' '.join(l[1:].split()) for l in lines if l.startswith(sign) and not l.startswith(sign * 3))
return pick('+'), pick('-')
def hunk_functions(lines):
"""Function names git gives in the hunk headers of a file's patch."""
return [l for l in lines if l.startswith('@@')]
def same_changes(mine, theirs):
"""True when, in every file that goes into the binaries, the backport adds and removes the
same lines as the official patch, in hunks of the same functions: only the context differs."""
paths = {p for p in set(mine) | set(theirs) if not outside_binaries(p)}
return all(p in mine and p in theirs and changed_lines(mine[p]) == changed_lines(theirs[p])
and hunk_functions(mine[p]) == hunk_functions(theirs[p]) for p in paths)
def adaptation_summary(mine, theirs):
"""What an adapted backport changed compared with the official patch, in files that go into the binaries."""
notes = []
dropped = sorted(p for p in theirs if p not in mine and not outside_binaries(p))
extra = sorted(p for p in mine if p not in theirs and not outside_binaries(p))
common = [p for p in mine if p in theirs and not outside_binaries(p)]
same = [p for p in common if changed_lines(mine[p]) == changed_lines(theirs[p])]
added_same = [p for p in common if p not in same and changed_lines(mine[p])[0] == changed_lines(theirs[p])[0]]
other = [p for p in common if p not in same and p not in added_same]
if same:
notes.append(f'{len(same)} file(s) with the same added and removed lines (context differs)')
if added_same:
notes.append(f'same added lines, different removed lines in {", ".join(added_same)}')
if other:
notes.append(f'different changes in {", ".join(other)}')
if dropped:
notes.append(f'official changes not applied to {", ".join(dropped)}')
if extra:
notes.append(f'backport-only changes to {", ".join(extra)}')
return '; '.join(notes)
def outside_binaries(path):
"""Files that are not compiled into, nor shipped with, the binary zips."""
return (path in ('NEWS', 'README.md') or path.startswith(('.github/', 'tests/'))
or '/tests/' in path or path.endswith('.phpt'))
def official_branch_commits(minor, tag):
"""Subject -> commits of the official PHP-x.y and PHP-x.y-* branches since the release tag."""
index = {}
refs = request(f'/repos/{OFFICIAL}/git/matching-refs/heads/PHP-{minor}') or []
for ref in refs:
branch = ref['ref'].removeprefix('refs/heads/')
if branch != f'PHP-{minor}' and not branch.startswith(f'PHP-{minor}-'):
continue # PHP-8.1.34 and the like are release branches
for commit in (request(f'/repos/{OFFICIAL}/compare/{tag}...{branch}') or {'commits': []})['commits']:
index.setdefault(commit['commit']['message'].splitlines()[0], []).append(commit['sha'])
return index
def official_counterpart(repo, commit, own_patch_id, code, branch_index, official, known=None):
"""php/php-src commit this one was taken from, preferring one with the same patch id.
code: the commit changes files that go into the binaries.
known: the official commit recorded by a review, for backports that do not name it."""
message = commit['commit']['message']
pending = [known] if known else []
pending += branch_index.get(message.splitlines()[0], [])
pending += CHERRY_PICK.findall(message)
candidates, followed = [], set()
while pending:
sha = pending.pop(0)
full = official_sha(sha, official)
if full:
candidates += [full] if full not in candidates else []
elif sha not in followed and len(followed) < 10:
# Cherry-picked from another backport branch: follow that commit's trailers
followed.add(sha)
picked = request(f'/repos/{repo}/commits/{sha}')
pending += CHERRY_PICK.findall(picked['commit']['message']) if picked else []
# Same author and subject, for a commit that names none, or for a code commit whose
# trailers stop at another backport (a bare "NEWS" subject would match unrelated commits)
if not candidates and (not followed or code):
subject = message.splitlines()[0].replace('"', ' ')
query = urllib.parse.quote(f'repo:{OFFICIAL} author-name:"{commit["commit"]["author"]["name"]}" "{subject}"')
found = request(f'/search/commits?q={query}&per_page=20') or {'items': []}
candidates = [item['sha'] for item in found['items'] if item['sha'] in official
and item['commit']['message'].splitlines()[0] == message.splitlines()[0]]
best = None
for sha in candidates:
text = patch(OFFICIAL, sha)
if text is None:
continue
if patch_id(text) == own_patch_id:
return sha, text
best = best or (sha, text)
return best or (None, None)
def verify(minor, config, reviews, official):
repo, head, base = config['php_src_repo'], config['php_src_sha'], config['base_commit']
tag = config['base_tag']
base_files, tag_files = tree(repo, base), tree(OFFICIAL, tag)
base_diff = sorted(p for p in set(base_files) | set(tag_files) if base_files.get(p) != tag_files.get(p))
base_ok = set(base_diff) <= BASE_EXCEPTIONS | set(config.get('base_exceptions', []))
comparison = request(f'/repos/{repo}/compare/{base}...{head}?per_page=100')
if comparison is None or comparison['status'] != 'ahead':
sys.exit(f'{minor}: {head} is not a descendant of {base}')
commits = list(comparison['commits'])
while len(commits) < comparison['total_commits']:
more = request(f'/repos/{repo}/compare/{base}...{head}?per_page=100&page={len(commits) // 100 + 1}')
if not more or not more['commits']:
sys.exit(f'{minor}: {len(commits)} of {comparison["total_commits"]} commits listed')
commits += more['commits']
clone = None
if any(len(c['parents']) > 1 for c in commits):
# A day before the base, so that the clone holds the base, the common ancestor of the first merges
date = request(f'/repos/{repo}/commits/{base}')['commit']['committer']['date']
since = (datetime.datetime.fromisoformat(date.replace('Z', '+00:00')) - datetime.timedelta(days=1)).date().isoformat()
clone = branch_clone(repo, config['php_src_branch'], since)
branch_index = official_branch_commits(minor, tag)
rows, details, counts, unreviewed = [], [], {}, []
for number, commit in enumerate(commits, 1):
sha = commit['sha']
if len(commit['parents']) > 1:
result, diff = merge_check(clone, commit)
if diff:
details.append((number, sha, commit['commit']['message'].splitlines()[0], None, diff))
counts[result] = counts.get(result, 0) + 1
review = reviews.get(sha, {}).get('note', '')
if result != 'clean merge' and not review:
unreviewed.append(sha)
rows.append(f"| {number} | [{sha[:7]}](https://github.com/{repo}/commit/{sha}) | - "
f"| {commit['commit']['message'].splitlines()[0].replace('|', chr(92) + '|')} "
f"| - | {result} | {review or ('**to review**' if sha in unreviewed else '')} |")
print(f'{minor} {number:3}/{len(commits)} {sha[:7]} {result}', flush=True)
continue
own = patch(repo, sha)
own_id = patch_id(own)
mine = files(own)
code = not all(outside_binaries(p) for p in mine)
counterpart, official_text = official_counterpart(repo, commit, own_id, code, branch_index, official,
reviews.get(sha, {}).get('official'))
if counterpart is None:
if not code:
result = 'no official commit, NEWS/tests only'
else:
result = 'no official commit'
details.append((number, sha, commit['commit']['message'].splitlines()[0], None,
[line for path in mine for line in [f'+++ backport/{path}', *mine[path]]]))
elif patch_id(official_text) == own_id:
result = 'identical'
else:
theirs = files(official_text)
differing = sorted(p for p in set(mine) | set(theirs) if mine.get(p) != theirs.get(p))
if all(outside_binaries(p) for p in differing):
result = 'identical code, NEWS/tests differ'
elif same_changes(mine, theirs):
result = 'same changes, context differs'
else:
result = 'adapted'
diff = [f'# {adaptation_summary(mine, theirs)}']
for path in differing:
if not outside_binaries(path):
file_diff = list(difflib.unified_diff(theirs.get(path, []), mine.get(path, []),
f'official/{path}', f'backport/{path}', n=1, lineterm=''))
# A bundled library upgraded from an older version: same file, very different patches
blob = tree(repo, sha).get(path) if len(file_diff) > LONG_DIFF else None
if blob and blob == tree(OFFICIAL, counterpart).get(path):
diff.append(f'# {path}: {len(file_diff)} lines of difference between the two patches, '
f'not shown: the file is the same after both commits (blob {blob})')
else:
diff += file_diff
details.append((number, sha, commit['commit']['message'].splitlines()[0], counterpart, diff))
counts[result] = counts.get(result, 0) + 1
review = reviews.get(sha, {}).get('note', '')
if result in ('adapted', 'no official commit') and not review:
unreviewed.append(sha)
# Advisory ids from the backport's message and from the official commit's
official_message = official_text.split('\n---\n', 1)[0] if official_text else ''
advisories = sorted(set(ADVISORY.findall(commit['commit']['message'] + official_message)))
rows.append(f"| {number} | [{sha[:7]}](https://github.com/{repo}/commit/{sha}) "
f"| {f'[{counterpart[:7]}](https://github.com/{OFFICIAL}/commit/{counterpart})' if counterpart else '-'} "
f"| {commit['commit']['message'].splitlines()[0].replace('|', chr(92) + '|')} "
f"| {', '.join(advisories) or '-'} | {result} | {review or ('**to review**' if sha in unreviewed else '')} |")
print(f'{minor} {number:3}/{len(commits)} {sha[:7]} {result}', flush=True)
summary = ', '.join(f'{n} {result}' for result, n in sorted(counts.items()))
lines = [f'# PHP {minor}: verification of {repo}@{head[:7]}', '',
f'Generated by `.github/scripts/verify_backports.py`. Branch `{config["php_src_branch"]}`, '
f'pinned at [{head[:7]}](https://github.com/{repo}/commit/{head}), compared with {OFFICIAL}.', '',
f'Base: [{base[:7]}](https://github.com/{repo}/commit/{base}) has the tree of the official tag '
f'[{tag}](https://github.com/{OFFICIAL}/releases/tag/{tag})'
+ (f', apart from {", ".join(base_diff)}.' if base_diff else '.') if base_ok
else f'Base: **{base[:7]} differs from {tag}**: {", ".join(base_diff)}', '',
f'{len(rows)} commits: {summary}.', '',
'| # | Commit | Official commit | Subject | Advisories | Result | Review |',
'|---|---|---|---|---|---|---|', *rows]
if details:
lines += ['', '## Commits to review', '',
'Adapted commits: differences between the official patch and the backport in files that go '
'into the binaries (hunk line numbers ignored). Commits without an official counterpart: '
'their whole patch.']
for number, sha, subject, _, diff in details:
lines += ['', f'<details><summary>#{number} {sha[:7]} {subject}</summary>', '', '```diff', *diff, '```',
'', '</details>']
os.makedirs('verification', exist_ok=True)
with open(f'verification/{minor}.md', 'w', encoding='utf-8', newline='\n') as f:
f.write('\n'.join(lines) + '\n')
return base_ok, unreviewed
def main():
with open(os.environ.get('VERSIONS', 'versions.json'), encoding='utf-8') as f:
versions = json.load(f)
reviews_path = 'verification/reviews.json'
reviews = json.load(open(reviews_path, encoding='utf-8')) if os.path.exists(reviews_path) else {}
official = official_commits()
failed = False
for minor in sys.argv[1:] or list(versions):
config = versions[minor]
if config['php_src_repo'] == OFFICIAL:
print(f'{minor}: built from {OFFICIAL}, nothing to verify')
continue
base_ok, unreviewed = verify(minor, config, reviews, official)
if not base_ok:
print(f'{minor}: base commit differs from {config["base_tag"]}')
if unreviewed:
print(f'{minor}: {len(unreviewed)} commit(s) to review: {" ".join(s[:7] for s in unreviewed)}')
failed = failed or not base_ok or bool(unreviewed)
sys.exit(1 if failed else 0)
if __name__ == '__main__':
main()