Skip to content

Commit 6fcab85

Browse files
committed
Improve change file detection
1 parent 59571c4 commit 6fcab85

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

scripts/detectChangedFiles.sh

Lines changed: 30 additions & 11 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,17 +58,17 @@ 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

6268
if ${readonlyMode}; then
63-
echo "detectChangedFiles: Readonly mode activated. Change detection file won't be created." >&2
69+
echo -e "${COLOR_INFO}detectChangedFiles: Readonly mode activated. Change detection file won't be created.${COLOR_DEFAULT}" >&2
6470
else
65-
echo "detectChangedFiles: ${hashFilePath} will be used as change detection file." >&2
71+
echo -e "${COLOR_INFO}detectChangedFiles: ${hashFilePath} will be used as change detection file.${COLOR_DEFAULT}" >&2
6672
fi
6773

6874
# Check if the paths parameter exist
@@ -71,6 +77,19 @@ if [ -z "${paths}" ] ; then
7177
exit 0
7278
fi
7379

80+
# Check all paths if they are valid files or valid directories
81+
for path in ${paths//,/ }; do
82+
if [ -f "${path}" ] ; then
83+
continue # Valid file
84+
fi
85+
if [ -d "${path}" ] ; then
86+
continue # Valid directory
87+
fi
88+
# Neither a valid directory and file
89+
echo -e "${COLOR_ERROR}detectChangedFiles: Error: Invalid path: ${path}${COLOR_DEFAULT}" >&2
90+
exit 1
91+
done
92+
7493
# Function to get file size
7594
get_file_size() {
7695
if [ -f "$1" ]; then
@@ -113,7 +132,7 @@ get_md5_checksum_of_all_file_names_and_sizes() {
113132
local files_and_their_size; files_and_their_size=$(file_names_and_sizes "${path}")
114133
all_files_and_sizes="${all_files_and_sizes}${files_and_their_size}"
115134
processed_paths=$((processed_paths + 1))
116-
echo -ne "detectChangedFiles: Calculate checksum progress: ($processed_paths/$total_paths)\r" >&2
135+
echo -ne "${COLOR_INFO}detectChangedFiles: Calculate checksum progress: ($processed_paths/$total_paths)\r${COLOR_DEFAULT}" >&2
117136
done
118137
echo "" >&2
119138
echo "${all_files_and_sizes}" | openssl md5 | awk '{print $2}'
@@ -133,9 +152,9 @@ if [ ! -f "${hashFilePath}" ] ; then
133152
mkdir -p "${hash_file_directory}"
134153
# Create the file containing the hash of the files list to a new file for the next call
135154
echo "${CURRENT_FILES_HASH}" > "${hashFilePath}"
136-
echo "detectChangedFiles: Change detection file created" >&2
155+
echo -e "${COLOR_INFO}detectChangedFiles: Change detection file created.${COLOR_DEFAULT}" >&2
137156
else
138-
echo "detectChangedFiles: Skipping file creation with content (=hash) ${CURRENT_FILES_HASH}" >&2
157+
echo -e "${COLOR_INFO}detectChangedFiles: Skipping file creation with content (=hash) ${CURRENT_FILES_HASH}${COLOR_DEFAULT}" >&2
139158
fi
140159
echo 1 # 1=Change detected and change detection file created
141160
exit 0
@@ -149,9 +168,9 @@ else
149168
if ! ${readonlyMode}; then
150169
# Write the updated hash into the file containing the hash of the files list for the next call
151170
echo "${CURRENT_FILES_HASH}" > "${hashFilePath}"
152-
echo "detectChangedFiles: Change detection file updated" >&2
171+
echo -e "${COLOR_INFO}detectChangedFiles: Change detection file updated.${COLOR_DEFAULT}" >&2
153172
else
154-
echo "detectChangedFiles: Skipping file update with content (=hash) ${CURRENT_FILES_HASH}" >&2
173+
echo -e "${COLOR_INFO}detectChangedFiles: Skipping file update with content (=hash) ${CURRENT_FILES_HASH}.${COLOR_DEFAULT}" >&2
155174
fi
156175
echo 2 # 2=Change detected and change detection file updated
157176
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)