Skip to content

Commit fc3829e

Browse files
committed
Make sure all scripts are actually changed by the PR
1 parent 4e6b3a2 commit fc3829e

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

install_scripts.sh

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,31 @@ display_help() {
88
echo " -h | --help - display this usage information"
99
}
1010

11+
file_changed_in_pr() {
12+
local full_path="$1"
13+
local base_branch="${2:-origin/2023.06-software.eessi.io}" # Default to origin/2023.06-software.eessi.io
14+
15+
# Make sure file exists
16+
[[ -f "$full_path" ]] || return 1
17+
18+
# Check if the file is in a Git repo
19+
local repo_root
20+
repo_root=$(git -C "$(dirname "$full_path")" rev-parse --show-toplevel 2>/dev/null)
21+
if [[ -z "$repo_root" ]]; then
22+
return 2 # Not in a git repository
23+
fi
24+
25+
# Compute relative path to the repo root
26+
local rel_path
27+
rel_path=$(realpath --relative-to="$repo_root" "$full_path")
28+
29+
# Check if the file changed in the diff range
30+
(
31+
cd "$repo_root" || return 2
32+
git diff --name-only "$base_branch"...HEAD | grep -q "^$rel_path$"
33+
) && return 0 || return 1
34+
}
35+
1136
compare_and_copy() {
1237
if [ "$#" -ne 2 ]; then
1338
echo "Usage of function: compare_and_copy <source_file> <destination_file>"
@@ -18,8 +43,19 @@ compare_and_copy() {
1843
destination_file="$2"
1944

2045
if [ ! -f "$destination_file" ] || ! diff -q "$source_file" "$destination_file" ; then
21-
cp "$source_file" "$destination_file"
22-
echo "File $1 copied to $2"
46+
echo "Files $source_file and $destination_file differ, checking if we should copy or not"
47+
# We only copy if the file is part of the PR
48+
if file_changed_in_pr "$source_file"; then
49+
echo "File has changed in the PR"
50+
cp "$source_file" "$destination_file"
51+
echo "File $source_file copied to $destination_file"
52+
else
53+
case $? in
54+
1) echo "❌ File has NOT changed" ;;
55+
2) echo "🚫 Not in a Git repo" ;;
56+
*) echo "⚠️ Unknown error" ;;
57+
esac
58+
fi
2359
else
2460
echo "Files $1 and $2 are identical. No copy needed."
2561
fi

0 commit comments

Comments
 (0)