Skip to content

Commit 79cfdf1

Browse files
Add pr_changed_files script to detect changed files in a PR (#148)
* Add `pr_changed_files` script to detect if files changed in a PR * Fix shellsheck warning * Update `CHANGELOG.md` * DRY error parameter validation message * Apply suggestions from code review Co-authored-by: Olivier Halligon <[email protected]> * DRY arguments parsing code Co-authored-by: Olivier Halligon <[email protected]> * Fix mode parsing * Add tests for `pr_changed_files` * Improve handling of filenames with spaces / multiple files in `pr_changed_file` * Improve shellcheck comment * Fix for running tests on CI * Update documented examples Co-authored-by: Olivier Halligon <[email protected]> * Update to get list of changed files as an array * Fix code to unescape file names * Rename arguments `--includes` to `--any-match` and `--only-in` to `--all-match` * Add `git fetch origin` to make sure the branch is fetched in the agent * Extract file pattern match to a function * Add more examples and details in the command documentation * Improve file pattern arguments handling & tests * Update documentation (suggestions from code review) Co-authored-by: Olivier Halligon <[email protected]> * Update bin/pr_changed_files Co-authored-by: Olivier Halligon <[email protected]> * Apply suggestions from code review Co-authored-by: Olivier Halligon <[email protected]> * Consistently use the order 1. "all-match" 2. "any-match" * Print result for succeeded tests and improve empty states tests * Remove post-process of the quotes and escaped characters in the files in the diff Co-authored-by: Olivier Halligon <[email protected]> * Fix escaping in all_match_patterns tests * Improve tests with special characters Using single quotes to make it easier to understand the use of special characters—including in the code creating the test files * Use single quotes on other tests too For consistency and to make it easier to understand the special characters we are playing with without having to escape them in double-quote contexts --------- Co-authored-by: Olivier Halligon <[email protected]>
1 parent 7cc4099 commit 79cfdf1

8 files changed

+477
-1
lines changed

Diff for: .buildkite/pipeline.yml

+26
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,29 @@ steps:
9292
notify:
9393
- github_commit_status:
9494
context: "install_swiftpm_dependencies Tests: Xcode Workspace and Project - Explicit"
95+
96+
- group: ":github: pr_changed_files Tests"
97+
steps:
98+
- label: ":github: pr_changed_files Tests - Basic Changes"
99+
command: tests/pr_changed_files/test_basic_changes.sh
100+
notify:
101+
- github_commit_status:
102+
context: "pr_changed_files Tests: Basic Changes"
103+
104+
- label: ":github: pr_changed_files Tests - Any Match"
105+
command: tests/pr_changed_files/test_any_match_patterns.sh
106+
notify:
107+
- github_commit_status:
108+
context: "pr_changed_files Tests: Any Match"
109+
110+
- label: ":github: pr_changed_files Tests - All Match"
111+
command: tests/pr_changed_files/test_all_match_patterns.sh
112+
notify:
113+
- github_commit_status:
114+
context: "pr_changed_files Tests: All Match"
115+
116+
- label: ":github: pr_changed_files Tests - Edge Cases"
117+
command: tests/pr_changed_files/test_edge_cases.sh
118+
notify:
119+
- github_commit_status:
120+
context: "pr_changed_files Tests: Edge Cases"

Diff for: CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ _None._
3838

3939
### New Features
4040

41-
_None._
41+
- Add `pr_changed_files` command to detect changes made in a Pull Request [#148]
4242

4343
### Bug Fixes
4444

Diff for: bin/pr_changed_files

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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

Diff for: tests/pr_changed_files/test_all_match_patterns.sh

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash -eu
2+
3+
set -o pipefail
4+
5+
source "$(dirname "${BASH_SOURCE[0]}")/test_helpers.sh"
6+
7+
echo "--- :git: Testing all-match pattern matching"
8+
9+
# Create test repository
10+
repo_path=$(create_tmp_repo_dir)
11+
trap 'cleanup_git_repo "$repo_path"' EXIT
12+
13+
# Set up environment variables
14+
export BUILDKITE_PULL_REQUEST="123"
15+
export BUILDKITE_PULL_REQUEST_BASE_BRANCH="base"
16+
17+
# Initialize the repository
18+
init_test_repo "$repo_path"
19+
20+
# Create test files (using single quotes to avoid special chars being interpreted by the shell)
21+
mkdir -p docs src/swift
22+
echo "doc1" > 'docs/read me.md'
23+
echo "doc2" > 'docs/guide with spaces.md'
24+
echo "doc3" > 'docs/special\!@*#$chars.md'
25+
git add .
26+
git commit -m "Add doc files"
27+
28+
# [Test] All changes in docs
29+
result=$(pr_changed_files --all-match "docs/*")
30+
assert_output "true" "$result" "Should return true when all changes match patterns"
31+
32+
# [Test] All changes in docs with explicit patterns including spaces and special chars
33+
# Note: we need to escape the '\` and `*` special chars in the pattern to match them literally instead of as special characters
34+
result=$(pr_changed_files --all-match 'docs/read me.md' 'docs/guide with spaces.md' 'docs/special\\!@\*#$chars.md')
35+
assert_output "true" "$result" "Should return true when all changes match patterns with spaces and special chars"
36+
37+
# [Test] All changes in docs with globbing patterns including spaces and special chars
38+
result=$(pr_changed_files --all-match 'docs/read me.md' 'docs/guide with spaces.md' 'docs/special\\!*.md')
39+
assert_output "true" "$result" "Should return true when all changes match patterns with spaces and special chars, even when using globbing"
40+
41+
# [Test] Changes outside pattern
42+
echo "swift" > 'src/swift/main with spaces.swift'
43+
echo "swift" > 'src/swift/special!\@#*$chars.swift'
44+
git add .
45+
git commit -m "Add swift file"
46+
47+
result=$(pr_changed_files --all-match "docs/*")
48+
assert_output "false" "$result" "Should return false when changes exist outside patterns"
49+
50+
# [Test] Multiple patterns, all matching
51+
# Note: we need to escape the '\` and `*` special chars in the pattern to match them literally instead of as special characters
52+
result=$(pr_changed_files --all-match 'docs/*' 'src/swift/main with spaces.swift' 'src/swift/special\!\\@#\*$chars.swift')
53+
assert_output "true" "$result" "Should return true when all changes match multiple patterns"
54+
55+
# [Test] Multiple patterns, all matching, including some using globbing
56+
result=$(pr_changed_files --all-match 'docs/*' 'src/swift/main with spaces.swift' 'src/swift/special*chars.swift')
57+
assert_output "true" "$result" "Should return true when all changes match multiple patterns, including some using globbing"
58+
59+
echo "✅ All-match pattern tests passed"

Diff for: tests/pr_changed_files/test_any_match_patterns.sh

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/bin/bash -eu
2+
3+
set -o pipefail
4+
5+
source "$(dirname "${BASH_SOURCE[0]}")/test_helpers.sh"
6+
7+
echo "--- :git: Testing any-match pattern matching"
8+
9+
# Create test repository
10+
repo_path=$(create_tmp_repo_dir)
11+
trap 'cleanup_git_repo "$repo_path"' EXIT
12+
13+
# Set up environment variables
14+
export BUILDKITE_PULL_REQUEST="123"
15+
export BUILDKITE_PULL_REQUEST_BASE_BRANCH="base"
16+
17+
# Initialize the repository
18+
init_test_repo "$repo_path"
19+
20+
# Create test files (using single quotes to avoid special chars being interpreted by the shell)
21+
mkdir -p docs src/swift src/ruby
22+
echo "doc" > 'docs/read me.md'
23+
echo "doc" > 'docs/special!@*#$chars.md'
24+
echo "swift" > 'src/swift/main.swift'
25+
echo "ruby" > 'src/ruby/main.rb'
26+
git add .
27+
git commit -m "Add test files"
28+
29+
# [Test] Match specific extension
30+
result=$(pr_changed_files --any-match '*.swift')
31+
assert_output "true" "$result" "Should match .swift files"
32+
33+
# [Test] Match multiple patterns
34+
result=$(pr_changed_files --any-match 'docs/*.md' '*.rb')
35+
assert_output "true" "$result" "Should match multiple patterns"
36+
37+
# [Test] Match files with spaces and special characters
38+
result=$(pr_changed_files --any-match 'docs/read me.md' 'docs/special!@\*#$chars.md')
39+
assert_output "true" "$result" "Should match files with spaces and special characters"
40+
41+
# [Test] Match files with spaces and special characters, even when using globbing
42+
result=$(pr_changed_files --any-match 'docs/read me.md' 'docs/special*chars.md')
43+
assert_output "true" "$result" "Should match files with spaces and special characters, even when using globbing"
44+
45+
# [Test] No matches
46+
result=$(pr_changed_files --any-match '*.js')
47+
assert_output "false" "$result" "Should not match non-existent patterns"
48+
49+
# [Test] Directory pattern
50+
result=$(pr_changed_files --any-match 'docs/*')
51+
assert_output "true" "$result" "Should match directory patterns"
52+
53+
# [Test] Exact pattern matching
54+
echo "swiftfile" > swiftfile.txt
55+
git add swiftfile.txt
56+
git commit -m "Add file with swift in name"
57+
58+
result=$(pr_changed_files --any-match '*.swift')
59+
assert_output "true" "$result" "Should only match exact patterns"
60+
61+
echo "✅ Any-match pattern tests passed"

Diff for: tests/pr_changed_files/test_basic_changes.sh

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash -eu
2+
3+
set -o pipefail
4+
5+
source "$(dirname "${BASH_SOURCE[0]}")/test_helpers.sh"
6+
7+
echo "--- :git: Testing basic changes detection"
8+
9+
# Create test repository
10+
repo_path=$(create_tmp_repo_dir)
11+
trap 'cleanup_git_repo "$repo_path"' EXIT
12+
13+
# Set up environment variables
14+
export BUILDKITE_PULL_REQUEST="123"
15+
export BUILDKITE_PULL_REQUEST_BASE_BRANCH="base"
16+
17+
# Initialize the repository
18+
init_test_repo "$repo_path"
19+
20+
# [Test] No changes
21+
result=$(pr_changed_files)
22+
assert_output "false" "$result" "Should return false when no files changed"
23+
24+
# [Test] Single file change
25+
echo "change" > new.txt
26+
git add new.txt
27+
git commit -m "Add new file"
28+
29+
result=$(pr_changed_files)
30+
assert_output "true" "$result" "Should return true when files changed"
31+
32+
echo "✅ Basic changes tests passed"

0 commit comments

Comments
 (0)