Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[et] Simplify path canonicalisation logic #52275

Merged
merged 2 commits into from
Apr 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions bin/et
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,19 @@ set -e
# Needed because if it is set, cd may print the path it changed to.
unset CDPATH

# On Mac OS, readlink -f doesn't work, so follow_links traverses the path one
# link at a time, and then cds into the link destination and find out where it
# ends up.
#
# The function is enclosed in a subshell to avoid changing the working directory
# of the caller.
function follow_links() (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is copy-pasted all over the engine repo. If this change is a better way to do things, should we change all occurrences?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting -- I'll set a reminder for a week and if no one's raised any issues, it seems like a cleanup worth doing.

cd -P "$(dirname -- "$1")"
file="$PWD/$(basename -- "$1")"
while [[ -h "$file" ]]; do
cd -P "$(dirname -- "$file")"
file="$(readlink -- "$file")"
cd -P "$(dirname -- "$file")"
file="$PWD/$(basename -- "$file")"
done
echo "$file"
)
# Returns the canonical path for its argument, with any symlinks resolved.
function canonical_path() {
if [[ -x "$(which realpath)" ]]; then
realpath -q -- "$1"
elif [[ -x "$(which readlink)" ]]; then
readlink -f -- "$1"
else
echo "The host platform is not supported by this tool"
exit 1
fi
}

SCRIPT_DIR="$(dirname -- "$(follow_links "${BASH_SOURCE[0]}")")"
SCRIPT_DIR="$(dirname -- "$(canonical_path "${BASH_SOURCE[0]}")")"
ENGINE_DIR="$(cd "$SCRIPT_DIR/.."; pwd -P)"

case "$(uname -s)" in
Expand Down