fix: Do not rewrite original file if it hasn't changed in fix-local-i… (#2109)

…ncludes.sh
This commit is contained in:
Ayaz Salikhov
2025-05-14 13:09:10 +01:00
committed by GitHub
parent ca2a3ccee9
commit 0a7ce8c5be

View File

@@ -13,24 +13,29 @@ GNU_SED=$(sed --version 2>&1 | grep -q 'GNU' && echo true || echo false)
if [[ "$GNU_SED" == "false" ]]; then # macOS sed
main_src_dirs=$(find ./src -maxdepth 1 -type d -exec basename {} \; | tr '\n' '|' | sed 's/|$//' | sed 's/|/\\|/g')
else
main_src_dirs=$(find ./src -maxdepth 1 -type d -exec basename {} \; | paste -sd '|' | sed 's/|/\\|/g')
main_src_dirs=$(find ./src -maxdepth 1 -type d -exec basename {} \; | paste -sd '|' | sed 's/|/\\|/g')
fi
fix_includes() {
file_path="$1"
if [[ "$GNU_SED" == "false" ]]; then # macOS sed
# make all includes to be <...> style
sed -i '' -E 's|#include "(.*)"|#include <\1>|g' "$file_path"
file_path_all_global="${file_path}.tmp.global"
file_path_fixed="${file_path}.tmp.fixed"
# make local includes to be "..." style
sed -i '' -E "s|#include <(($main_src_dirs)/.*)>|#include \"\1\"|g" "$file_path"
# Make all includes to be <...> style
sed -E 's|#include "(.*)"|#include <\1>|g' "$file_path" > "$file_path_all_global"
# Make local includes to be "..." style
sed -E "s|#include <(($main_src_dirs)/.*)>|#include \"\1\"|g" "$file_path_all_global" > "$file_path_fixed"
rm "$file_path_all_global"
# Check if the temporary file is different from the original file
if ! cmp -s "$file_path" "$file_path_fixed"; then
# Replace the original file with the temporary file
mv "$file_path_fixed" "$file_path"
else
# make all includes to be <...> style
sed -i -E 's|#include "(.*)"|#include <\1>|g' "$file_path"
# make local includes to be "..." style
sed -i -E "s|#include <(($main_src_dirs)/.*)>|#include \"\1\"|g" "$file_path"
# Remove the temporary file if it's the same as the original
rm "$file_path_fixed"
fi
}