Skip to content

Commit 41260df

Browse files
committed
Improve change file detection
1 parent 59571c4 commit 41260df

File tree

2 files changed

+81
-29
lines changed

2 files changed

+81
-29
lines changed

scripts/detectChangedFiles.sh

Lines changed: 81 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@ ARTIFACTS_CHANGE_DETECTION_HASH_FILE=${ARTIFACTS_CHANGE_DETECTION_HASH_FILE:-"ar
2121
CHANGE_DETECTION_HASH_FILE=${CHANGE_DETECTION_HASH_FILE:-"${ARTIFACTS_CHANGE_DETECTION_HASH_FILE}"} # Name of the file that contains the hash code of the file list for change detection
2222
CHANGE_DETECTION_HASH_FILE_PATH=${CHANGE_DETECTION_HASH_FILE_PATH:-"./${ARTIFACTS_DIRECTORY}/${CHANGE_DETECTION_HASH_FILE}"} # Default path of the file that contains the hash code of the file list for change detection. Can be overridden by a command line option.
2323

24+
COLOR_INFO='\033[0;30m' # dark grey
25+
COLOR_ERROR='\033[0;31m' # red
26+
COLOR_DEFAULT='\033[0m'
27+
2428
# Function to display script usage
2529
usage() {
26-
echo "Usage: $0 [--readonly]"
27-
echo " [--paths <comma separated list of file and directory names> (default=artifacts)]"
28-
echo " [--hashfile <path to the file that contains the hash for change detection> (default=env var CHANGE_DETECTION_HASH_FILE_PATH)]"
30+
echo -e "${COLOR_ERROR}" >&2
31+
echo "Usage: $0 [--readonly]" >&2
32+
echo " [--paths <comma separated list of file and directory names> (default=artifacts)]" >&2
33+
echo " [--hashfile <path to the file that contains the hash for change detection> (default=env var CHANGE_DETECTION_HASH_FILE_PATH)]" >&2
34+
echo -e "${COLOR_DEFAULT}" >&2
2935
exit 1
3036
}
3137

@@ -52,24 +58,51 @@ while [[ $# -gt 0 ]]; do
5258
shift
5359
;;
5460
*)
55-
echo "detectChangedFiles: Error: Unknown option: ${key}"
61+
echo -e "${COLOR_ERROR}detectChangedFiles: Error: Unknown option: ${key}${COLOR_DEFAULT}" >&2
5662
usage
5763
;;
5864
esac
5965
shift || true # ignore error when there are no more arguments
6066
done
6167

68+
exit_failed() {
69+
case "$0" in
70+
*/sh) return 1 ;; # Script is sourced
71+
*) exit 1 ;; # Script is executed directly
72+
esac
73+
}
74+
75+
exit_successful() {
76+
case "$0" in
77+
*/sh) return 0 ;; # Script is sourced
78+
*) exit 0 ;; # Script is executed directly
79+
esac
80+
}
81+
6282
if ${readonlyMode}; then
63-
echo "detectChangedFiles: Readonly mode activated. Change detection file won't be created." >&2
83+
echo -e "${COLOR_INFO}detectChangedFiles: Readonly mode activated. Change detection file won't be created.${COLOR_DEFAULT}" >&2
6484
else
65-
echo "detectChangedFiles: ${hashFilePath} will be used as change detection file." >&2
85+
echo -e "${COLOR_INFO}detectChangedFiles: ${hashFilePath} will be used as change detection file.${COLOR_DEFAULT}" >&2
6686
fi
6787

6888
# Check if the paths parameter exist
6989
if [ -z "${paths}" ] ; then
7090
echo 0 # 0=No change detected. The path list is empty. There is nothing to compare. Therefore assume that there are no changes.
71-
exit 0
72-
fi
91+
exit_successful
92+
fi
93+
94+
# Check all paths if they are valid files or valid directories
95+
for path in ${paths//,/ }; do
96+
if [ -f "${path}" ] ; then
97+
continue # Valid file
98+
fi
99+
if [ -d "${path}" ] ; then
100+
continue # Valid directory
101+
fi
102+
# Neither a valid directory and file
103+
echo -e "${COLOR_ERROR}detectChangedFiles: Error: Invalid path: ${path}${COLOR_DEFAULT}" >&2
104+
exit_failed
105+
done
73106

74107
# Function to get file size
75108
get_file_size() {
@@ -80,23 +113,46 @@ get_file_size() {
80113
fi
81114
}
82115

116+
isMacOS() {
117+
[ "$(uname -s)" = "Darwin" ]
118+
}
119+
83120
# Function to process a single path
84121
file_names_and_sizes() {
85122
if [ -d "$1" ]; then
123+
# TODO Remove after debugging
124+
echo "detectChangedFiles: Checking directory $1" >&2
125+
86126
# If it's a directory, list all files inside
87127
# except for "node_modules", "target", "temp" and the change detection file itself
88-
find -L "$1" \
89-
-type d -name "node_modules" -prune -o \
90-
-type d -name "target" -prune -o \
91-
-type d -name "temp" -prune -o \
92-
-type d -name ".reports" -prune -o \
93-
-not -path "${hashFilePath}" \
94-
-type f \
95-
-exec stat -f "%N %z" {} + \
96-
| sort
128+
if isMacOS; then
129+
find -L "$1" \
130+
-type d -name "node_modules" -prune -o \
131+
-type d -name "target" -prune -o \
132+
-type d -name "temp" -prune -o \
133+
-type d -name ".reports" -prune -o \
134+
-not -path "${hashFilePath}" \
135+
-type f \
136+
-exec stat -f "%N %z" {} + \
137+
| sort
138+
else
139+
find -L "$1" \
140+
-type d -name "node_modules" -prune -o \
141+
-type d -name "target" -prune -o \
142+
-type d -name "temp" -prune -o \
143+
-type d -name ".reports" -prune -o \
144+
-not -path "${hashFilePath}" \
145+
-type f \
146+
-exec stat --printf="%n %s\n" {} + \
147+
| sort
148+
fi
97149
elif [ -f "$1" ]; then
98-
# If it's a file, just echo the file path
99-
stat -f "%N %z" < "$1"
150+
# The path is a file. Print its path and size.
151+
if isMacOS; then
152+
stat -f "%N %z" "$1"
153+
else
154+
stat --printf="%n %s\n" "$1"
155+
fi
100156
fi
101157
}
102158

@@ -113,7 +169,7 @@ get_md5_checksum_of_all_file_names_and_sizes() {
113169
local files_and_their_size; files_and_their_size=$(file_names_and_sizes "${path}")
114170
all_files_and_sizes="${all_files_and_sizes}${files_and_their_size}"
115171
processed_paths=$((processed_paths + 1))
116-
echo -ne "detectChangedFiles: Calculate checksum progress: ($processed_paths/$total_paths)\r" >&2
172+
echo -ne "${COLOR_INFO}detectChangedFiles: Calculate checksum progress: ($processed_paths/$total_paths)\r${COLOR_DEFAULT}" >&2
117173
done
118174
echo "" >&2
119175
echo "${all_files_and_sizes}" | openssl md5 | awk '{print $2}'
@@ -133,12 +189,12 @@ if [ ! -f "${hashFilePath}" ] ; then
133189
mkdir -p "${hash_file_directory}"
134190
# Create the file containing the hash of the files list to a new file for the next call
135191
echo "${CURRENT_FILES_HASH}" > "${hashFilePath}"
136-
echo "detectChangedFiles: Change detection file created" >&2
192+
echo -e "${COLOR_INFO}detectChangedFiles: Change detection file created.${COLOR_DEFAULT}" >&2
137193
else
138-
echo "detectChangedFiles: Skipping file creation with content (=hash) ${CURRENT_FILES_HASH}" >&2
194+
echo -e "${COLOR_INFO}detectChangedFiles: Skipping file creation with content (=hash) ${CURRENT_FILES_HASH}${COLOR_DEFAULT}" >&2
139195
fi
140196
echo 1 # 1=Change detected and change detection file created
141-
exit 0
197+
exit_successful
142198
fi
143199

144200
# Assume that there is no change if the saved hash is equal to the current one.
@@ -149,9 +205,9 @@ else
149205
if ! ${readonlyMode}; then
150206
# Write the updated hash into the file containing the hash of the files list for the next call
151207
echo "${CURRENT_FILES_HASH}" > "${hashFilePath}"
152-
echo "detectChangedFiles: Change detection file updated" >&2
208+
echo -e "${COLOR_INFO}detectChangedFiles: Change detection file updated.${COLOR_DEFAULT}" >&2
153209
else
154-
echo "detectChangedFiles: Skipping file update with content (=hash) ${CURRENT_FILES_HASH}" >&2
210+
echo -e "${COLOR_INFO}detectChangedFiles: Skipping file update with content (=hash) ${CURRENT_FILES_HASH}.${COLOR_DEFAULT}" >&2
155211
fi
156212
echo 2 # 2=Change detected and change detection file updated
157213
fi

scripts/scanTypescript.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,10 @@ is_valid_scan_result() {
115115
}
116116

117117
is_change_detected() {
118-
local COLOR_DARK_GREY='\033[0;30m'
119-
local COLOR_DEFAULT='\033[0m'
120118
local source_directory_name; source_directory_name=$(basename "${source_directory}");
121119

122-
echo -e "${COLOR_DARK_GREY}"
123120
changeDetectionHashFilePath="./${SOURCE_DIRECTORY}/typescriptScanChangeDetection-${source_directory_name}.sha"
124121
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedFiles.sh" --readonly --hashfile "${changeDetectionHashFilePath}" --paths "${source_directory}")
125-
echo -e "${COLOR_DEFAULT}"
126122

127123
if [ "${changeDetectionReturnCode}" == "0" ] && [ "${TYPESCRIPT_SCAN_CHANGE_DETECTION}" = true ]; then
128124
true

0 commit comments

Comments
 (0)