MCPcopy Create free account
hub / github.com/anthropics/claude-code / _prioritize_diff_files

Function _prioritize_diff_files

plugins/security-guidance/hooks/gitutil.py:512–547  ·  view source on GitHub ↗

When `diff_files` exceeds `cap`, return the top-`cap` by security relevance plus the count dropped. Otherwise return (diff_files, 0). Score = (risk_tokens_in_path, not_low_priority, added_lines). The added-lines proxy is `content.count('\\n+')` which counts diff additions cheaply wi

(diff_files, cap)

Source from the content-addressed store, hash-verified

510
511
512def _prioritize_diff_files(diff_files, cap):
513 """When `diff_files` exceeds `cap`, return the top-`cap` by security
514 relevance plus the count dropped. Otherwise return (diff_files, 0).
515
516 Score = (risk_tokens_in_path, not_low_priority, added_lines). The
517 added-lines proxy is `content.count('\\n+')` which counts diff additions
518 cheaply without re-parsing hunks. This is a heuristic, not a guarantee —
519 the goal is to review the likely-dangerous subset of an over-cap diff
520 instead of reviewing nothing. Diffs that exceed the cap are typically
521 large multi-file scaffolds, and the cross-file source→sink vulnerabilities
522 in them concentrate in a handful of api/client/route files.
523 """
524 if len(diff_files) <= cap:
525 return diff_files, 0
526
527 def _score(item):
528 fp, content = item
529 low = fp.lower()
530 # Prepend "/" so leading-slash patterns in _LOW_PRIORITY_PATH_TOKENS
531 # match top-level dirs (git diff paths are repo-root-relative, e.g.
532 # `migrations/001.py` not `/migrations/001.py`). Same trick as
533 # _is_reviewable_source.
534 low_slashed = "/" + low
535 risk = sum(1 for t in _SECURITY_RISK_PATH_TOKENS if t in low)
536 low_prio = (
537 fp.endswith(_LOW_PRIORITY_SUFFIXES)
538 or any(t in low_slashed for t in _LOW_PRIORITY_PATH_TOKENS)
539 )
540 # added_lines: count('\n+') over-counts by including '+++' header and
541 # any literal '+' at line start in context, but it's a consistent
542 # ordinal across files in the same diff which is all we need.
543 added = content.count("\n+")
544 return (risk, not low_prio, added)
545
546 ranked = sorted(diff_files, key=_score, reverse=True)
547 return ranked[:cap], len(diff_files) - cap
548
549
550def _is_reviewable_source(file_path):

Callers 3

handle_stop_hookFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected