Skip to content

Commit c0e290d

Browse files
committed
Add custom hashfile path option to change detection
1 parent 4333805 commit c0e290d

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

scripts/detectChangedFiles.sh

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,20 @@ set -o errexit -o pipefail
1818
ARTIFACTS_DIRECTORY=${ARTIFACTS_DIRECTORY:-"artifacts"}
1919
ARTIFACTS_CHANGE_DETECTION_HASH_FILE=${ARTIFACTS_CHANGE_DETECTION_HASH_FILE:-"artifactsChangeDetectionHash.txt"} # !DEPRECATED! Use CHANGE_DETECTION_HASH_FILE.
2020
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
21-
CHANGE_DETECTION_HASH_FILE_PATH=${CHANGE_DETECTION_HASH_FILE_PATH:-"./${ARTIFACTS_DIRECTORY}/${CHANGE_DETECTION_HASH_FILE}"} # Path of the file that contains the hash code of the file list for change detection
21+
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.
2222

2323
# Function to display script usage
2424
usage() {
25-
echo "Usage: $0 [--readonly] [--paths <comma separated list of file and directory names> (default=artifacts)]"
25+
echo "Usage: $0 [--readonly]"
26+
echo " [--paths <comma separated list of file and directory names> (default=artifacts)]"
27+
echo " [--hashfile <path to the file that contains the hash for change detection> (default=env var CHANGE_DETECTION_HASH_FILE_PATH)]"
2628
exit 1
2729
}
2830

2931
# Default values
3032
readonlyMode=false
3133
paths="./${ARTIFACTS_DIRECTORY}"
34+
hashFilePath="${CHANGE_DETECTION_HASH_FILE_PATH}"
3235

3336
# Parse command line arguments
3437
while [[ $# -gt 0 ]]; do
@@ -43,6 +46,10 @@ while [[ $# -gt 0 ]]; do
4346
paths="${value}"
4447
shift
4548
;;
49+
--hashfile)
50+
hashFilePath="${value}"
51+
shift
52+
;;
4653
*)
4754
echo "detectChangedFiles: Error: Unknown option: ${key}"
4855
usage
@@ -54,7 +61,7 @@ done
5461
if ${readonlyMode}; then
5562
echo "detectChangedFiles: Readonly mode activated. Change detection file won't be created." >&2
5663
else
57-
echo "detectChangedFiles: ${CHANGE_DETECTION_HASH_FILE_PATH} will be used as change detection file." >&2
64+
echo "detectChangedFiles: ${hashFilePath} will be used as change detection file." >&2
5865
fi
5966

6067
# Check if the paths parameter exist
@@ -81,7 +88,7 @@ file_names_and_sizes() {
8188
-type d -name "node_modules" -prune -o \
8289
-type d -name "target" -prune -o \
8390
-type d -name "temp" -prune -o \
84-
-not -path "${CHANGE_DETECTION_HASH_FILE_PATH}" \
91+
-not -path "${hashFilePath}" \
8592
-type f \
8693
-exec stat -f "%N %z" {} + \
8794
| sort
@@ -117,13 +124,13 @@ get_md5_checksum_of_all_file_names_and_sizes() {
117124
CURRENT_FILES_HASH=$(get_md5_checksum_of_all_file_names_and_sizes "${paths}")
118125

119126
# Assume that the files where changed if the file containing the hash of the file list does not exist yet.
120-
if [ ! -f "${CHANGE_DETECTION_HASH_FILE_PATH}" ] ; then
127+
if [ ! -f "${hashFilePath}" ] ; then
121128
if [ "${readonlyMode}" = false ] ; then
122129
# Create the directory for the hash file if it hadn't existed yet.
123-
hash_file_directory=$(dirname "${CHANGE_DETECTION_HASH_FILE_PATH}")
130+
hash_file_directory=$(dirname "${hashFilePath}")
124131
mkdir -p "${hash_file_directory}"
125132
# Create the file containing the hash of the files list to a new file for the next call
126-
echo "${CURRENT_FILES_HASH}" > "${CHANGE_DETECTION_HASH_FILE_PATH}"
133+
echo "${CURRENT_FILES_HASH}" > "${hashFilePath}"
127134
echo "detectChangedFiles: Change detection file created" >&2
128135
else
129136
echo "detectChangedFiles: Skipping file creation with content (=hash) ${CURRENT_FILES_HASH}" >&2
@@ -134,12 +141,12 @@ fi
134141

135142
# Assume that there is no change if the saved hash is equal to the current one.
136143
# Otherwise assume that the files where changed and overwrite the hash with the current one for the next call
137-
if [[ $(< "${CHANGE_DETECTION_HASH_FILE_PATH}") == "$CURRENT_FILES_HASH" ]] ; then
144+
if [[ $(< "${hashFilePath}") == "${CURRENT_FILES_HASH}" ]] ; then
138145
echo 0 # 0=No change detected
139146
else
140147
if ! ${readonlyMode}; then
141148
# Write the updated hash into the file containing the hash of the files list for the next call
142-
echo "$CURRENT_FILES_HASH" > "${CHANGE_DETECTION_HASH_FILE_PATH}"
149+
echo "${CURRENT_FILES_HASH}" > "${hashFilePath}"
143150
echo "detectChangedFiles: Change detection file updated" >&2
144151
else
145152
echo "detectChangedFiles: Skipping file update with content (=hash) ${CURRENT_FILES_HASH}" >&2

0 commit comments

Comments
 (0)