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

Commit b77f183

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 b77f183

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

bin/et

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,22 @@ 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+
# Returns the canonical path for its argument, with any symlinks resolved.
13+
function canonical_path() {
14+
case "$(uname -s)" in
15+
Linux)
16+
readlink -f -- "$1"
17+
;;
18+
Darwin)
19+
realpath -q -- "$1"
20+
;;
21+
*)
22+
echo "The host platform is not supported by this tool"
23+
exit 1
24+
esac
25+
}
26+
27+
SCRIPT_DIR="$(dirname -- "$(canonical_path "${BASH_SOURCE[0]}")")"
3128
ENGINE_DIR="$(cd "$SCRIPT_DIR/.."; pwd -P)"
3229

3330
case "$(uname -s)" in

0 commit comments

Comments
 (0)