|
1 | 1 | #!/usr/bin/env bash |
2 | | -set -euo pipefail |
| 2 | +set -Eeuo pipefail |
3 | 3 |
|
4 | 4 | TRACKED_COMMIT="981d145117e8992842cdddee555c57e60c7a220a" |
| 5 | +REMOTE_URL='https://android.googlesource.com/platform/system/core' |
| 6 | +REMOTE_BRANCH='main' |
| 7 | +PROTO_PATH='debuggerd/proto/tombstone.proto' |
| 8 | +GITILES_REF="refs/heads/${REMOTE_BRANCH}" |
| 9 | +GITILES_LOG_URL="${REMOTE_URL}/+log/${GITILES_REF}/${PROTO_PATH}?format=JSON" |
5 | 10 |
|
6 | | -# tail -n +2 to remove the magic anti-XSSI prefix from the Gitiles JSON response |
7 | | -LATEST_COMMIT=$(curl -sf \ |
8 | | - 'https://android.googlesource.com/platform/system/core/+log/refs/heads/main/debuggerd/proto/tombstone.proto?format=JSON' \ |
9 | | - | tail -n +2 \ |
10 | | - | jq -r '.log[0].commit') |
| 11 | +MODE=auto |
| 12 | +case "${1:-}" in |
| 13 | + "") |
| 14 | + ;; |
| 15 | + --git-only) |
| 16 | + MODE=git |
| 17 | + ;; |
| 18 | + --gitiles-only) |
| 19 | + MODE=gitiles |
| 20 | + ;; |
| 21 | + *) |
| 22 | + echo "Usage: $0 [--git-only|--gitiles-only]" >&2 |
| 23 | + exit 2 |
| 24 | + ;; |
| 25 | +esac |
11 | 26 |
|
12 | | -if [ -z "$LATEST_COMMIT" ] || [ "$LATEST_COMMIT" = "null" ]; then |
13 | | - echo "ERROR: Failed to fetch latest commit from Gitiles" >&2 |
14 | | - exit 1 |
15 | | -fi |
| 27 | +TEMP_FILES=() |
| 28 | +TEMP_DIRS=() |
| 29 | +LATEST_COMMIT="" |
16 | 30 |
|
17 | | -echo "Tracked commit: $TRACKED_COMMIT" |
18 | | -echo "Latest commit: $LATEST_COMMIT" |
| 31 | +error() { |
| 32 | + echo "ERROR: $*" >&2 |
| 33 | +} |
19 | 34 |
|
20 | | -if [ "$LATEST_COMMIT" != "$TRACKED_COMMIT" ]; then |
21 | | - echo "Schema has been updated! Latest: https://android.googlesource.com/platform/system/core/+/${LATEST_COMMIT}/debuggerd/proto/tombstone.proto" |
22 | | - exit 1 |
23 | | -fi |
| 35 | +show_output() { |
| 36 | + local label=$1 |
| 37 | + local file=$2 |
24 | 38 |
|
25 | | -echo "Schema is up to date." |
| 39 | + if [ -s "$file" ]; then |
| 40 | + echo "$label:" >&2 |
| 41 | + sed 's/^/ /' "$file" >&2 |
| 42 | + fi |
| 43 | +} |
| 44 | + |
| 45 | +require_command() { |
| 46 | + local command_name=$1 |
| 47 | + |
| 48 | + if ! command -v "$command_name" >/dev/null 2>&1; then |
| 49 | + error "Required command not found: $command_name" |
| 50 | + return 1 |
| 51 | + fi |
| 52 | +} |
| 53 | + |
| 54 | +make_temp_file() { |
| 55 | + local file |
| 56 | + file=$(mktemp) |
| 57 | + TEMP_FILES+=("$file") |
| 58 | + printf '%s\n' "$file" |
| 59 | +} |
| 60 | + |
| 61 | +make_temp_dir() { |
| 62 | + local dir |
| 63 | + dir=$(mktemp -d) |
| 64 | + TEMP_DIRS+=("$dir") |
| 65 | + printf '%s\n' "$dir" |
| 66 | +} |
| 67 | + |
| 68 | +cleanup() { |
| 69 | + local path |
| 70 | + |
| 71 | + for path in "${TEMP_FILES[@]}"; do |
| 72 | + rm -f "$path" |
| 73 | + done |
| 74 | + |
| 75 | + for path in "${TEMP_DIRS[@]}"; do |
| 76 | + rm -rf "$path" |
| 77 | + done |
| 78 | +} |
| 79 | + |
| 80 | +handle_unexpected_error() { |
| 81 | + local exit_code=$? |
| 82 | + error "Unexpected failure at line $1 while running: $2 (exit $exit_code)" |
| 83 | + exit "$exit_code" |
| 84 | +} |
| 85 | + |
| 86 | +trap 'handle_unexpected_error "$LINENO" "$BASH_COMMAND"' ERR |
| 87 | +trap cleanup EXIT |
| 88 | + |
| 89 | +run_gitiles_check() { |
| 90 | + local response_file |
| 91 | + local stderr_file |
| 92 | + local status |
| 93 | + |
| 94 | + require_command curl || return 1 |
| 95 | + require_command jq || return 1 |
| 96 | + |
| 97 | + response_file=$(make_temp_file) |
| 98 | + stderr_file=$(make_temp_file) |
| 99 | + |
| 100 | + if curl -fsS "$GITILES_LOG_URL" -o "$response_file" 2>"$stderr_file"; then |
| 101 | + : |
| 102 | + else |
| 103 | + status=$? |
| 104 | + error "Failed to fetch Gitiles history from:" |
| 105 | + error " $GITILES_LOG_URL" |
| 106 | + error "curl exited with status $status." |
| 107 | + show_output "curl output" "$stderr_file" |
| 108 | + return 1 |
| 109 | + fi |
| 110 | + |
| 111 | + if LATEST_COMMIT=$(tail -n +2 "$response_file" | jq -er '.log[0].commit' 2>"$stderr_file"); then |
| 112 | + : |
| 113 | + else |
| 114 | + status=$? |
| 115 | + error "Failed to parse the latest commit from the Gitiles response." |
| 116 | + error "jq exited with status $status." |
| 117 | + show_output "jq output" "$stderr_file" |
| 118 | + echo "Response preview:" >&2 |
| 119 | + head -n 20 "$response_file" >&2 |
| 120 | + return 1 |
| 121 | + fi |
| 122 | + |
| 123 | + if [ -z "$LATEST_COMMIT" ]; then |
| 124 | + error "Gitiles response did not contain a commit hash." |
| 125 | + echo "Response preview:" >&2 |
| 126 | + head -n 20 "$response_file" >&2 |
| 127 | + return 1 |
| 128 | + fi |
| 129 | +} |
| 130 | + |
| 131 | +run_git_check() { |
| 132 | + local repo_dir |
| 133 | + local stderr_file |
| 134 | + local status |
| 135 | + |
| 136 | + require_command git || return 1 |
| 137 | + |
| 138 | + repo_dir=$(make_temp_dir) |
| 139 | + stderr_file=$(make_temp_file) |
| 140 | + |
| 141 | + if GIT_TERMINAL_PROMPT=0 git clone \ |
| 142 | + --quiet \ |
| 143 | + --filter=blob:none \ |
| 144 | + --single-branch \ |
| 145 | + --branch "$REMOTE_BRANCH" \ |
| 146 | + --no-checkout \ |
| 147 | + "$REMOTE_URL" "$repo_dir" 2>"$stderr_file"; then |
| 148 | + : |
| 149 | + else |
| 150 | + status=$? |
| 151 | + error "Failed to clone $REMOTE_BRANCH from:" |
| 152 | + error " $REMOTE_URL" |
| 153 | + error "git clone exited with status $status." |
| 154 | + show_output "git clone output" "$stderr_file" |
| 155 | + return 1 |
| 156 | + fi |
| 157 | + |
| 158 | + if LATEST_COMMIT=$(git -C "$repo_dir" log -n 1 --format=%H HEAD -- "$PROTO_PATH" 2>"$stderr_file"); then |
| 159 | + : |
| 160 | + else |
| 161 | + status=$? |
| 162 | + error "Failed to determine the latest commit that modified:" |
| 163 | + error " $PROTO_PATH" |
| 164 | + error "git log exited with status $status." |
| 165 | + show_output "git log output" "$stderr_file" |
| 166 | + return 1 |
| 167 | + fi |
| 168 | + |
| 169 | + if [ -z "$LATEST_COMMIT" ]; then |
| 170 | + error "Git history did not contain a commit for:" |
| 171 | + error " $PROTO_PATH" |
| 172 | + return 1 |
| 173 | + fi |
| 174 | +} |
| 175 | + |
| 176 | +report_result() { |
| 177 | + echo "Tracked commit: $TRACKED_COMMIT" |
| 178 | + echo "Latest commit: $LATEST_COMMIT" |
| 179 | + |
| 180 | + if [ "$LATEST_COMMIT" != "$TRACKED_COMMIT" ]; then |
| 181 | + echo "Schema has been updated! Latest: ${REMOTE_URL}/+/${LATEST_COMMIT}/${PROTO_PATH}" |
| 182 | + exit 1 |
| 183 | + fi |
| 184 | + |
| 185 | + echo "Schema is up to date." |
| 186 | +} |
| 187 | + |
| 188 | +case "$MODE" in |
| 189 | + auto) |
| 190 | + if run_gitiles_check; then |
| 191 | + report_result |
| 192 | + exit 0 |
| 193 | + fi |
| 194 | + |
| 195 | + echo "Falling back to git-based check." >&2 |
| 196 | + if run_git_check; then |
| 197 | + report_result |
| 198 | + exit 0 |
| 199 | + fi |
| 200 | + |
| 201 | + exit 1 |
| 202 | + ;; |
| 203 | + gitiles) |
| 204 | + if run_gitiles_check; then |
| 205 | + report_result |
| 206 | + exit 0 |
| 207 | + fi |
| 208 | + |
| 209 | + exit 1 |
| 210 | + ;; |
| 211 | + git) |
| 212 | + if run_git_check; then |
| 213 | + report_result |
| 214 | + exit 0 |
| 215 | + fi |
| 216 | + |
| 217 | + exit 1 |
| 218 | + ;; |
| 219 | +esac |
0 commit comments