Merge pull request #12422 from tangcong/fix-realpath

scripts: fix realpath command not found in mac os
release-3.5
Gyuho Lee 2020-10-26 10:42:26 -07:00 committed by GitHub
commit 7da5182f1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 42 additions and 1 deletions

View File

@ -48,6 +48,47 @@ function log_success {
>&2 echo -n -e "${COLOR_NONE}"
}
# From http://stackoverflow.com/a/12498485
function relativePath {
# both $1 and $2 are absolute paths beginning with /
# returns relative path to $2 from $1
local source=$1
local target=$2
local commonPart=$source
local result=""
while [[ "${target#$commonPart}" == "${target}" ]]; do
# no match, means that candidate common part is not correct
# go up one level (reduce common part)
commonPart="$(dirname "$commonPart")"
# and record that we went back, with correct / handling
if [[ -z $result ]]; then
result=".."
else
result="../$result"
fi
done
if [[ $commonPart == "/" ]]; then
# special case for root (no common path)
result="$result/"
fi
# since we now have identified the common part,
# compute the non-common part
local forwardPart="${target#$commonPart}"
# and now stick all parts together
if [[ -n $result ]] && [[ -n $forwardPart ]]; then
result="$result$forwardPart"
elif [[ -n $forwardPart ]]; then
# extra slash removal
result="${forwardPart:1}"
fi
echo "$result"
}
#### Discovery of files/packages within a go module #####
@ -73,7 +114,7 @@ function pkgs_in_module {
function run {
local rpath
local command
rpath=$(realpath "--relative-to=${ETCD_ROOT_DIR}" "${PWD}")
rpath=$(relativePath "${ETCD_ROOT_DIR}" "${PWD}")
# Quoting all components as the commands are fully copy-parsable:
command=("${@}")
command=("${command[@]@Q}")