|
| 1 | +#!/usr/bin/env bash |
| 2 | +# The components a change reaches, from .github/components.tsv (README "Versions"). |
| 3 | +# |
| 4 | +# changes.sh <changed-paths-file> prints `<component>=true|false`, `full=`, each flag, and `digest=` |
| 5 | +# |
| 6 | +# EVENT, BASE_REF and HEAD_REF (the workflow's event and a pull request's branches) decide a full run: |
| 7 | +# anything but a pull request, and a pull request from or into a release branch, reaches every component. |
| 8 | +# Otherwise each path reaches the component of the first line of the table that matches it; a path no |
| 9 | +# line matches, or one of `all`, reaches every component; and a change of the `@core` component reaches |
| 10 | +# every other, since each is tested over it. A `@flag` is a pull request's question: true when a changed |
| 11 | +# path matches it, or when the pull request is from or into a release branch; false for any other event. |
| 12 | +# |
| 13 | +# `digest` names the core's inputs (the blobs of every path of the core and of `all`), so a run that built |
| 14 | +# the core from the same inputs can lend what it built to a run that did not change them. |
| 15 | +set -euo pipefail |
| 16 | +root="$(cd "$(dirname "$0")/../.." && pwd)" |
| 17 | +table="$root/.github/components.tsv" |
| 18 | +changed="${1:?the file of changed paths}" |
| 19 | + |
| 20 | +rows=(); flags=(); core=""; components=() |
| 21 | +while IFS= read -r line; do |
| 22 | + case "$line" in ''|'#'*) continue ;; esac |
| 23 | + case "$line" in |
| 24 | + @core$'\t'*) core="${line#*$'\t'}" ;; |
| 25 | + @flag\ *) flags+=("${line#@flag }") ;; |
| 26 | + *) rows+=("$line") |
| 27 | + name="${line%%$'\t'*}" |
| 28 | + [ "$name" = all ] || [[ " ${components[*]} " == *" $name "* ]] || components+=("$name") ;; |
| 29 | + esac |
| 30 | +done < "$table" |
| 31 | + |
| 32 | +declare -A reached=() flagged=() printed=() |
| 33 | +full=false |
| 34 | +release=false |
| 35 | +case "${EVENT:-}" in pull_request) ;; *) full=true ;; esac |
| 36 | +case "${BASE_REF:-}" in release/*) full=true; release=true ;; esac |
| 37 | +case "${HEAD_REF:-}" in release/*) full=true; release=true ;; esac |
| 38 | + |
| 39 | +while IFS= read -r path; do |
| 40 | + [ -n "$path" ] || continue |
| 41 | + component="" |
| 42 | + for row in "${rows[@]}"; do |
| 43 | + name="${row%%$'\t'*}"; pattern="${row#*$'\t'}" |
| 44 | + # shellcheck disable=SC2254 |
| 45 | + case "$path" in $pattern) component="$name"; break ;; esac |
| 46 | + done |
| 47 | + if [ -z "$component" ] || [ "$component" = all ]; then |
| 48 | + echo "every component: $path" >&2 |
| 49 | + full=true |
| 50 | + else |
| 51 | + reached[$component]=true |
| 52 | + fi |
| 53 | + for flag in "${flags[@]}"; do |
| 54 | + name="${flag%%$'\t'*}"; pattern="${flag#*$'\t'}" |
| 55 | + # shellcheck disable=SC2254 |
| 56 | + case "$path" in $pattern) flagged[$name]=true ;; esac |
| 57 | + done |
| 58 | +done < "$changed" |
| 59 | + |
| 60 | +if [ "$full" = true ] || { [ -n "$core" ] && [ "${reached[$core]:-}" = true ]; }; then |
| 61 | + for c in "${components[@]}"; do reached[$c]=true; done |
| 62 | +fi |
| 63 | +for c in "${components[@]}"; do echo "$c=${reached[$c]:-false}"; done |
| 64 | +echo "full=$full" |
| 65 | +for flag in "${flags[@]}"; do |
| 66 | + name="${flag%%$'\t'*}" |
| 67 | + [ "$release" = true ] && flagged[$name]=true |
| 68 | + [ "${EVENT:-}" = pull_request ] || flagged[$name]=false |
| 69 | + [ -n "${printed[$name]:-}" ] && continue |
| 70 | + printed[$name]=1 |
| 71 | + echo "$name=${flagged[$name]:-false}" |
| 72 | +done |
| 73 | + |
| 74 | +# The core's inputs: every tracked path the table gives to the core or to `all`. |
| 75 | +mapfile -t inputs < <(for row in "${rows[@]}"; do |
| 76 | + name="${row%%$'\t'*}"; pattern="${row#*$'\t'}" |
| 77 | + if [ "$name" = "$core" ] || [ "$name" = all ]; then echo "${pattern%%\**}"; fi |
| 78 | +done | sort -u) |
| 79 | +digest=$(cd "$root" && git ls-files -s -- "${inputs[@]}" | sha256sum | cut -c1-24) |
| 80 | +echo "digest=$digest" |
0 commit comments