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

Commit dec38dd

Browse files
committed
[et] Simplify path canonicalisation logic
In order to canonicalise paths, previously we were doing an iterative computation to resolve symlinks to a canonical path, directory by directory. This was because on macOS and other BSDs, readlink doesn't support the `-f` (follow symlinks) option. However, macOS and other BSD-based systems *do* bundle the `realpath` utility, which resolves symlinks.
1 parent 59f5b1a commit dec38dd

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

bin/et

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,21 @@ set -e
99
# Needed because if it is set, cd may print the path it changed to.
1010
unset CDPATH
1111

12-
# On Mac OS, readlink -f doesn't work, so follow_links traverses the path one
13-
# link at a time, and then cds into the link destination and find out where it
14-
# ends up.
15-
#
16-
# The function is enclosed in a subshell to avoid changing the working directory
17-
# of the caller.
18-
function follow_links() (
19-
cd -P "$(dirname -- "$1")"
20-
file="$PWD/$(basename -- "$1")"
21-
while [[ -h "$file" ]]; do
22-
cd -P "$(dirname -- "$file")"
23-
file="$(readlink -- "$file")"
24-
cd -P "$(dirname -- "$file")"
25-
file="$PWD/$(basename -- "$file")"
26-
done
27-
echo "$file"
28-
)
29-
30-
SCRIPT_DIR="$(dirname -- "$(follow_links "${BASH_SOURCE[0]}")")"
12+
function canonical_path() {
13+
case "$(uname -s)" in
14+
Linux)
15+
readlink -f -- "$1"
16+
;;
17+
Darwin)
18+
realpath -q -- "$1"
19+
;;
20+
*)
21+
echo "The host platform is not supported by this tool"
22+
exit 1
23+
esac
24+
}
25+
26+
SCRIPT_DIR="$(dirname -- "$(canonical_path "${BASH_SOURCE[0]}")")"
3127
ENGINE_DIR="$(cd "$SCRIPT_DIR/.."; pwd -P)"
3228

3329
case "$(uname -s)" in

0 commit comments

Comments
 (0)