@@ -8,6 +8,35 @@ display_help() {
88 echo " -h | --help - display this usage information"
99}
1010
11+ file_changed_in_pr () {
12+ local full_path=" $1 "
13+
14+ # Make sure file exists
15+ [[ -f " $full_path " ]] || return 1
16+
17+ # Check if the file is in a Git repo (it should be)
18+ local repo_root
19+ repo_root=$( git -C " $( dirname " $full_path " ) " rev-parse --show-toplevel 2> /dev/null)
20+ if [[ -z " $repo_root " ]]; then
21+ return 2 # Not in a git repository
22+ fi
23+
24+ # Compute relative path to the repo root
25+ local rel_path
26+ rel_path=$( realpath --relative-to=" $repo_root " " $full_path " )
27+
28+ # Check if the file changed in the PR diff file that we have
29+ (
30+ cd " $repo_root " || return 2
31+ # $PR_DIFF should be set by the calling script
32+ if [[ ! -z ${PR_DIFF} ]] && [[ -f " $PR_DIFF " ]]; then
33+ grep -q " b/$rel_path " " $PR_DIFF " # Add b/ to match diff patterns
34+ else
35+ return 3
36+ fi
37+ ) && return 0 || return 1
38+ }
39+
1140compare_and_copy () {
1241 if [ " $# " -ne 2 ]; then
1342 echo " Usage of function: compare_and_copy <source_file> <destination_file>"
@@ -18,8 +47,20 @@ compare_and_copy() {
1847 destination_file=" $2 "
1948
2049 if [ ! -f " $destination_file " ] || ! diff -q " $source_file " " $destination_file " ; then
21- cp " $source_file " " $destination_file "
22- echo " File $1 copied to $2 "
50+ echo " Files $source_file and $destination_file differ, checking if we should copy or not"
51+ # We only copy if the file is part of the PR
52+ if file_changed_in_pr " $source_file " ; then
53+ echo " File has changed in the PR"
54+ cp " $source_file " " $destination_file "
55+ echo " File $source_file copied to $destination_file "
56+ else
57+ case $? in
58+ 1) echo " ❌ File has NOT changed in PR" ;;
59+ 2) echo " 🚫 Not in Git repository" ;;
60+ 3) echo " 🚫 No PR diff file found" ;;
61+ * ) echo " ⚠️ Unknown error" ;;
62+ esac
63+ fi
2364 else
2465 echo " Files $1 and $2 are identical. No copy needed."
2566 fi
0 commit comments