|
| 1 | +#!/bin/bash -eu |
| 2 | + |
| 3 | +# This script checks if files are changed in a PR. |
| 4 | +# |
| 5 | +# Usage: |
| 6 | +# pr_changed_files # Check if any files changed |
| 7 | +# pr_changed_files --all-match <file-patterns...> # Check if changes are limited to given patterns |
| 8 | +# pr_changed_files --any-match <file-patterns...> # Check if changes include files matching patterns |
| 9 | +# |
| 10 | +# Behavior: |
| 11 | +# With no arguments: |
| 12 | +# Returns "true" if the PR contains any changes, "false" otherwise |
| 13 | +# |
| 14 | +# With --all-match: |
| 15 | +# Returns "true" if ALL changed files match AT LEAST ONE of the patterns |
| 16 | +# Returns "false" if ANY changed file doesn't match ANY pattern |
| 17 | +# Note: Will return "true" even if not all patterns are matched by the changed files. |
| 18 | +# This mode is especially useful to check if the PR _only_ touches a particular subset of files/folders (but nothing else) |
| 19 | +# |
| 20 | +# With --any-match: |
| 21 | +# Returns "true" if ANY changed file matches ANY of the patterns |
| 22 | +# Returns "false" if NONE of the changed files match ANY pattern |
| 23 | +# Note: Will return "true" even if the PR includes other files not matching the patterns. |
| 24 | +# This mode is especially useful to check if the PR _includes_ (aka _contains at least_) particular files/folders |
| 25 | +# |
| 26 | +# Examples with expected outputs: |
| 27 | +# # Check if any files changed, returning "true" if PR has changes, "false" otherwise |
| 28 | +# $ pr_changed_files |
| 29 | +# |
| 30 | +# # Check if only documentation files changed (to skip UI tests for example) |
| 31 | +# $ pr_changed_files --all-match "*.md" "docs/*" |
| 32 | +# → "true" if PR only changes `docs/guide.md` and `README.md` |
| 33 | +# → "true" if PR only changes `docs/image.png` (not all patterns need to match, ok if no *.md) |
| 34 | +# → "false" if PR changes `docs/guide.md` and `src/main.swift` (ALL files need to match at least one pattern) |
| 35 | +# |
| 36 | +# # Check if any Swift files changed (to decide if we should run SwiftLint) |
| 37 | +# $ pr_changed_files --any-match "*.swift" ".swiftlint.yml" |
| 38 | +# → "true" if PR changes `src/main.swift` and `README.md` (AT LEAST one file matches one of the patterns) |
| 39 | +# → "true" if PR changes `.swiftlint.yml` |
| 40 | +# → "false" if PR only changes `README.md` (none of files match any of the patterns) |
| 41 | +# |
| 42 | +# Returns: |
| 43 | +# Prints "true" if the condition is met, "false" otherwise |
| 44 | +# Exits with code 0 regardless of if the condition was met or not, to allow easy variable assignment. |
| 45 | +# Only exits with non-zero if the command invocation itself was incorrect (called outside of a PR context, incorrect arguments…) |
| 46 | + |
| 47 | +if [[ ! "${BUILDKITE_PULL_REQUEST:-invalid}" =~ ^[0-9]+$ ]]; then |
| 48 | + echo "Error: this tool can only be called from a Buildkite PR job" >&2 |
| 49 | + exit 1 |
| 50 | +fi |
| 51 | + |
| 52 | +# Ensure we have the base branch locally |
| 53 | +git fetch origin "$BUILDKITE_PULL_REQUEST_BASE_BRANCH" >/dev/null 2>&1 || { |
| 54 | + echo "Error: failed to fetch base branch '$BUILDKITE_PULL_REQUEST_BASE_BRANCH'" >&2 |
| 55 | + exit 1 |
| 56 | +} |
| 57 | + |
| 58 | +mode="" |
| 59 | +patterns=() |
| 60 | + |
| 61 | +# Define error message for mutually exclusive options |
| 62 | +EXCLUSIVE_OPTIONS_ERROR="Error: either specify --all-match or --any-match; cannot specify both" |
| 63 | + |
| 64 | +while [[ "$#" -gt 0 ]]; do |
| 65 | + case $1 in |
| 66 | + --all-match | --any-match) |
| 67 | + if [[ -n "$mode" ]]; then |
| 68 | + echo "$EXCLUSIVE_OPTIONS_ERROR" >&2 |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | + mode="${1#--}" |
| 72 | + shift |
| 73 | + # Check if there are any patterns after the flag |
| 74 | + while [[ "$#" -gt 0 && "$1" != "--"* ]]; do |
| 75 | + patterns+=("$1") |
| 76 | + shift |
| 77 | + done |
| 78 | + if [[ "${#patterns[@]}" -eq 0 ]]; then |
| 79 | + echo "Error: must specify at least one file pattern" >&2 |
| 80 | + exit 1 |
| 81 | + fi |
| 82 | + ;; |
| 83 | + --*) |
| 84 | + echo "Error: unknown option $1" >&2 |
| 85 | + exit 1 |
| 86 | + ;; |
| 87 | + *) |
| 88 | + echo "Error: unexpected argument $1" >&2 |
| 89 | + exit 1 |
| 90 | + ;; |
| 91 | + esac |
| 92 | +done |
| 93 | + |
| 94 | +# Get list of changed files as an array |
| 95 | +changed_files=() |
| 96 | +while IFS= read -r -d '' file; do |
| 97 | + changed_files+=("$file") |
| 98 | +done < <(git --no-pager diff --name-only -z --merge-base "$BUILDKITE_PULL_REQUEST_BASE_BRANCH" HEAD | sort) |
| 99 | + |
| 100 | +if [[ -z "$mode" ]]; then |
| 101 | + # No arguments = any change |
| 102 | + if [[ ${#changed_files[@]} -gt 0 ]]; then |
| 103 | + echo "true" |
| 104 | + else |
| 105 | + echo "false" |
| 106 | + fi |
| 107 | + exit 0 |
| 108 | +fi |
| 109 | + |
| 110 | +# Returns 0 if the file matches any of the patterns, 1 otherwise |
| 111 | +file_matches_any_pattern() { |
| 112 | + local file="$1" |
| 113 | + shift |
| 114 | + for pattern in "$@"; do |
| 115 | + # shellcheck disable=SC2053 # We don't quote the rhs in the condition on the next line because we want to interpret pattern as a glob pattern |
| 116 | + if [[ "$file" == ${pattern} ]]; then |
| 117 | + return 0 |
| 118 | + fi |
| 119 | + done |
| 120 | + return 1 |
| 121 | +} |
| 122 | + |
| 123 | +if [[ "$mode" == "all-match" ]]; then |
| 124 | + # Check if all changed files match at least one pattern |
| 125 | + for file in "${changed_files[@]}"; do |
| 126 | + if ! file_matches_any_pattern "$file" "${patterns[@]}"; then |
| 127 | + echo "false" |
| 128 | + exit 0 |
| 129 | + fi |
| 130 | + done |
| 131 | + echo "true" |
| 132 | +elif [[ "$mode" == "any-match" ]]; then |
| 133 | + # Check if any changed file matches any pattern |
| 134 | + for file in "${changed_files[@]}"; do |
| 135 | + if file_matches_any_pattern "$file" "${patterns[@]}"; then |
| 136 | + echo "true" |
| 137 | + exit 0 |
| 138 | + fi |
| 139 | + done |
| 140 | + echo "false" |
| 141 | +fi |
0 commit comments