|
| 1 | +#!/bin/bash |
| 2 | +# This file was generated using AI assistance (Cursor AI) and reviewed by the maintainers. |
| 3 | +# |
| 4 | +# Detect files under code/ changed in a PR whose modifications are NOT fully |
| 5 | +# protected by rebase rules. Two categories: |
| 6 | +# - "Missing rule" — no rebase rule exists for the file at all |
| 7 | +# - "Incomplete rule" — a rule exists but doesn't reproduce the current content |
| 8 | +# |
| 9 | +# For "incomplete" detection the script runs the actual rebase handler against |
| 10 | +# upstream content and diffs the result against the working tree, reusing the |
| 11 | +# test infrastructure from .claude/skills/test-rebase-rules/. |
| 12 | +# |
| 13 | +# Usage: |
| 14 | +# bash check-unprotected-changes.sh --pr-comment <base>..<head> |
| 15 | +# |
| 16 | +# Prerequisites (for incomplete-rule detection): |
| 17 | +# - upstream-code remote must be fetched for the current upstream version |
| 18 | +# |
| 19 | +# Exit codes: |
| 20 | +# 0 — All modifications are fully covered |
| 21 | +# 1 — Problems found |
| 22 | +# 2 — Error |
| 23 | +set -u |
| 24 | + |
| 25 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 26 | +REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" |
| 27 | +cd "$REPO_ROOT" |
| 28 | + |
| 29 | +OUTPUT_MODE="text" |
| 30 | +COMMIT_RANGE="" |
| 31 | +for arg in "$@"; do |
| 32 | + case "$arg" in |
| 33 | + --pr-comment) OUTPUT_MODE="pr-comment" ;; |
| 34 | + *..*) COMMIT_RANGE="$arg" ;; |
| 35 | + esac |
| 36 | +done |
| 37 | + |
| 38 | +if [ -z "$COMMIT_RANGE" ]; then |
| 39 | + REBASE_COMMIT=$(git log --format='%H' --grep="^Rebase against the upstream" -1) |
| 40 | + if [ -z "$REBASE_COMMIT" ]; then |
| 41 | + echo "ERROR: Could not find a rebase commit and no commit range provided." >&2 |
| 42 | + exit 2 |
| 43 | + fi |
| 44 | + COMMIT_RANGE="${REBASE_COMMIT}..HEAD" |
| 45 | +fi |
| 46 | + |
| 47 | +UPSTREAM_VERSION=$(grep '^CURRENT_UPSTREAM_VERSION=' rebase.sh | head -1 | sed 's/.*="\(.*\)"/\1/') |
| 48 | + |
| 49 | +HANDLER_SCRIPT="$REPO_ROOT/.claude/skills/test-rebase-rules/test-rebase-handler.sh" |
| 50 | +RUN_ALL_TESTS="$REPO_ROOT/.claude/skills/test-rebase-rules/run-all-tests.sh" |
| 51 | + |
| 52 | +# --- Build the set of files covered by rebase rules --- |
| 53 | +COVERED_LIST=$(mktemp) |
| 54 | +trap 'rm -f "$COVERED_LIST"' EXIT |
| 55 | + |
| 56 | +while IFS= read -r line; do |
| 57 | + if [[ "$line" =~ \[\[.*==.*\"([^\"]+)\".*\]\] ]]; then |
| 58 | + echo "${BASH_REMATCH[1]}" |
| 59 | + fi |
| 60 | +done < <(sed -n '/^resolve_conflicts()/,/^}/p' rebase.sh) >> "$COVERED_LIST" |
| 61 | + |
| 62 | +find .rebase/replace -name '*.json' -type f 2>/dev/null | while IFS= read -r rule_file; do |
| 63 | + code_path="${rule_file#.rebase/replace/}" |
| 64 | + code_path="${code_path%.json}" |
| 65 | + if [[ "$code_path" != code/* ]]; then |
| 66 | + code_path="code/$code_path" |
| 67 | + fi |
| 68 | + echo "$code_path" |
| 69 | +done >> "$COVERED_LIST" |
| 70 | + |
| 71 | +find .rebase/add -type f 2>/dev/null | while IFS= read -r f; do echo "${f#.rebase/add/}"; done >> "$COVERED_LIST" |
| 72 | +find .rebase/override -type f 2>/dev/null | while IFS= read -r f; do echo "${f#.rebase/override/}"; done >> "$COVERED_LIST" |
| 73 | + |
| 74 | +sort -u -o "$COVERED_LIST" "$COVERED_LIST" |
| 75 | + |
| 76 | +# --- Collect changed files and classify --- |
| 77 | +MISSING_RULE=() |
| 78 | +TO_TEST=() |
| 79 | + |
| 80 | +while IFS= read -r file_path; do |
| 81 | + case "$file_path" in |
| 82 | + */package-lock.json) continue ;; |
| 83 | + code/extensions/che-*) continue ;; |
| 84 | + */che/*.ts|*/che/*.js) continue ;; |
| 85 | + esac |
| 86 | + |
| 87 | + if grep -qxF "$file_path" "$COVERED_LIST"; then |
| 88 | + TO_TEST+=("$file_path") |
| 89 | + else |
| 90 | + MISSING_RULE+=("$file_path") |
| 91 | + fi |
| 92 | +done < <(git diff --name-only "$COMMIT_RANGE" -- code/) |
| 93 | + |
| 94 | +# --- Test files that have rules: are rules complete? --- |
| 95 | +INCOMPLETE_RULE=() |
| 96 | +HAS_UPSTREAM=false |
| 97 | +if [ -n "$UPSTREAM_VERSION" ]; then |
| 98 | + git rev-parse "upstream-code/$UPSTREAM_VERSION" > /dev/null 2>&1 && HAS_UPSTREAM=true |
| 99 | +fi |
| 100 | + |
| 101 | +if [ "$HAS_UPSTREAM" = true ] && [ ${#TO_TEST[@]} -gt 0 ] && [ -f "$RUN_ALL_TESTS" ]; then |
| 102 | + TEST_OUTPUT=$(bash "$RUN_ALL_TESTS" "${TO_TEST[@]}" 2>&1) || true |
| 103 | + |
| 104 | + while IFS= read -r line; do |
| 105 | + if [[ "$line" =~ ^\|\ \`([^\`]+)\`\ \| ]]; then |
| 106 | + failed_file="${BASH_REMATCH[1]}" |
| 107 | + INCOMPLETE_RULE+=("$failed_file") |
| 108 | + fi |
| 109 | + done < <(echo "$TEST_OUTPUT" | sed -n '/^### Failures/,/^### /p' | grep '^| `') |
| 110 | +fi |
| 111 | + |
| 112 | +# --- Output --- |
| 113 | +TOTAL_ISSUES=$(( ${#MISSING_RULE[@]} + ${#INCOMPLETE_RULE[@]} )) |
| 114 | + |
| 115 | +if [ "$OUTPUT_MODE" = "pr-comment" ]; then |
| 116 | + if [ $TOTAL_ISSUES -eq 0 ]; then |
| 117 | + exit 0 |
| 118 | + fi |
| 119 | + |
| 120 | + echo "<!-- rebase-rules-check -->" |
| 121 | + echo "### Rebase Rules Check" |
| 122 | + echo "" |
| 123 | + echo "The following files were modified in this PR but their changes are **not fully protected** by rebase rules." |
| 124 | + echo "Without proper rules, these changes **will be lost** during the next upstream rebase." |
| 125 | + echo "" |
| 126 | + |
| 127 | + if [ ${#MISSING_RULE[@]} -gt 0 ]; then |
| 128 | + echo "#### Missing rules (no rebase rule exists)" |
| 129 | + echo "" |
| 130 | + echo "| # | File |" |
| 131 | + echo "|---|------|" |
| 132 | + i=1 |
| 133 | + for f in "${MISSING_RULE[@]}"; do |
| 134 | + echo "| $i | \`$f\` |" |
| 135 | + ((i++)) |
| 136 | + done |
| 137 | + echo "" |
| 138 | + fi |
| 139 | + |
| 140 | + if [ ${#INCOMPLETE_RULE[@]} -gt 0 ]; then |
| 141 | + echo "#### Incomplete rules (rule exists but doesn't cover new changes)" |
| 142 | + echo "" |
| 143 | + echo "| # | File |" |
| 144 | + echo "|---|------|" |
| 145 | + i=1 |
| 146 | + for f in "${INCOMPLETE_RULE[@]}"; do |
| 147 | + echo "| $i | \`$f\` |" |
| 148 | + ((i++)) |
| 149 | + done |
| 150 | + echo "" |
| 151 | + fi |
| 152 | + |
| 153 | + echo "Comment \`/add-rebase-rules\` on this PR to add or update the rules automatically." |
| 154 | + exit 1 |
| 155 | +fi |
| 156 | + |
| 157 | +# Default text mode |
| 158 | +if [ $TOTAL_ISSUES -eq 0 ]; then |
| 159 | + echo "All modified files in code/ are fully covered by rebase rules." |
| 160 | + exit 0 |
| 161 | +fi |
| 162 | + |
| 163 | +echo "Found $TOTAL_ISSUES file(s) with rebase rule problems:" |
| 164 | +echo "" |
| 165 | +if [ ${#MISSING_RULE[@]} -gt 0 ]; then |
| 166 | + echo "Missing rules:" |
| 167 | + for f in "${MISSING_RULE[@]}"; do echo " - $f"; done |
| 168 | + echo "" |
| 169 | +fi |
| 170 | +if [ ${#INCOMPLETE_RULE[@]} -gt 0 ]; then |
| 171 | + echo "Incomplete rules:" |
| 172 | + for f in "${INCOMPLETE_RULE[@]}"; do echo " - $f"; done |
| 173 | + echo "" |
| 174 | +fi |
| 175 | +echo "Comment /add-rebase-rules on this PR to fix automatically." |
| 176 | +exit 1 |
0 commit comments