Skip to content

Commit

Permalink
fix: pure zsh impl of realpath due to old macOS BSD realpath
Browse files Browse the repository at this point in the history
Signed-off-by: Vladislav Doster <mvdoster@gmail.com>
  • Loading branch information
vladdoster committed Dec 16, 2023
1 parent 0ba778a commit c94dd72
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 35 deletions.
25 changes: 19 additions & 6 deletions doc/zsdoc/zinit-install.zsh.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Documentation automatically generated with `zshelldoc'
.zinit-json-get-value
.zinit-json-to-array
.zinit-mirror-using-svn
.zinit-realpath
.zinit-setup-plugin-dir
.zinit-single-line
.zinit-update-snippet
Expand Down Expand Up @@ -193,7 +194,7 @@ ____
____

Has 377 line(s). Calls functions:
Has 372 line(s). Calls functions:

.zinit-download-snippet
|-- .zinit-download-file-stdout
Expand All @@ -211,10 +212,9 @@ Has 377 line(s). Calls functions:
|   `-- zinit.zsh/.zinit-any-to-user-plugin
|-- .zinit-mirror-using-svn
|-- zinit-side.zsh/.zinit-store-ices
|-- zinit.zsh/+zi-log
`-- zinit.zsh/is-at-least
`-- zinit.zsh/+zi-log

Uses feature(s): _is-at-least_, _setopt_, _trap_, _zcompile_
Uses feature(s): _setopt_, _trap_, _zcompile_

Called by:

Expand Down Expand Up @@ -457,6 +457,20 @@ Called by:

.zinit-download-snippet

==== .zinit-realpath

____
$1: Initial path
$2: Target path
____

Has 16 line(s). Doesn't call other functions.

Not called by script or any function (may be e.g. a hook, a Zle widget, etc.).

==== .zinit-setup-plugin-dir

____
Expand Down Expand Up @@ -540,8 +554,7 @@ Has 76 line(s). Calls functions:
|   |   `-- zinit.zsh/.zinit-any-to-user-plugin
|   |-- .zinit-mirror-using-svn
|   |-- zinit-side.zsh/.zinit-store-ices
|   |-- zinit.zsh/+zi-log
|   `-- zinit.zsh/is-at-least
|   `-- zinit.zsh/+zi-log
|-- zinit.zsh/+zi-log
|-- zinit.zsh/.zinit-get-object-path
`-- zinit.zsh/.zinit-pack-ice
Expand Down
1 change: 0 additions & 1 deletion doc/zsdoc/zinit.zsh.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1965,5 +1965,4 @@ Called by:
:zinit-tmp-subst-autoload
:zinit-tmp-subst-bindkey
Script-Body
zinit-install.zsh/.zinit-download-snippet

2 changes: 1 addition & 1 deletion tests/commands.zunit
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@
assert $state equals 0
}

# vim:ft=zsh:sw=2:sts=2:et:foldmarker={,}:foldmethod=marker
# vim:ft=zsh:sw=2:sts=2:et:foldmarker={,}:foldmethod=marker
79 changes: 52 additions & 27 deletions zinit-install.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,28 @@ builtin source "${ZINIT[BIN_DIR]}/zinit-side.zsh" || {
builtin print -P "${ZINIT[col-error]}ERROR:%f%b Couldn't find ${ZINIT[col-obj]}zinit-side.zsh%f%b."
return 1
}

# FUNCTION: .zinit-realpath [[[
#
# $1: Initial path
# $2: Target path
.zinit-realpath () {
[[ $# -ge 1 ]] && [[ $# -le 2 ]] || return 1
local target=${${2:-$1}:a} current=${${${2:+$1}:-$PWD}:a} relative=''
local appendix="${target#/}"
while appendix="${target#${current}/}"
[[ $current != '/' ]] && [[ $appendix = $target ]]
do
if [[ $current = $appendix ]]; then
relative="${relative:-.}"
builtin print -- "${relative#/}"
return 0
fi
current=${current%/*}
relative="$relative${relative:+/}.."
done
relative+=${relative:+${appendix:+/}}${appendix#/}
builtin print -- "$relative"
} # ]]]
# FUNCTION: .zinit-jq-check [[[
# Check if jq is available and outputs an error message with instructions if
# that's not the case
Expand Down Expand Up @@ -1194,34 +1215,38 @@ builtin source "${ZINIT[BIN_DIR]}/zinit-side.zsh" || {
"accessible (wrong permissions).{rst}"
retval=4
}
if ! (( ${+ICE[link] )) {
if (( !OPTS[opt_-q,--quiet] )) && [[ $url != /dev/null ]] {


if ! (( ${+ICE[link]} )); then
if (( !OPTS[opt_-q,--quiet] )) && [[ $url != /dev/null ]]; then
+zi-log "{msg}Copying {file}$filename{msg}{…}{rst}"
command cp -vf "$url" "$local_dir/$dirname/$filename" || \
{ +zi-log "{ehi}ERROR:{error} The file copying has been unsuccessful.{rst}"; retval=4; }
} else {
command cp -f "$url" "$local_dir/$dirname/$filename" &>/dev/null || \
{ +zi-log "{ehi}ERROR:{error} The copying of {file}$filename{error} has been unsuccessful"\
"${${(M)OPTS[opt_-q,--quiet]:#1}:+, skip the -q/--quiet option for more information}.{rst}"; retval=4; }
}
} else {
if (( $+commands[realpath] )) {
local rpv="$(realpath --version | head -n1 | sed -E 's/realpath (\(.*\))?//g')"
if is-at-least 8.23 $rpv; then
rel_url="$(realpath --relative-to="$local_dir/$dirname" "$url")" && \
{ url="$rel_url" }
fi
}
if (( !OPTS[opt_-q,--quiet] )) && [[ $url != /dev/null ]] {
command cp -vf "$url" "$local_dir/$dirname/$filename" || {
+zi-log "{ehi}ERROR:{error} The file copying has been unsuccessful.{rst}"
retval=4
}
else
command cp -f "$url" "$local_dir/$dirname/$filename" &> /dev/null || {
+zi-log "{ehi}ERROR:{error} The copying of {file}$filename{error} has been unsuccessful" "${${(M)OPTS[opt_-q,--quiet]:#1}:+, skip the -q/--quiet option for more information}.{rst}"
retval=4
}
fi
else
rel_url="$(.zinit-realpath "$local_dir/$dirname" "$url")"
url="${rel_url}"
if (( !OPTS[opt_-q,--quiet] )) && [[ $url != /dev/null ]]; then
+zi-log "{msg}Linking {file}$filename{msg}{…}{rst}"
command ln -svf "$url" "$local_dir/$dirname/$filename" || \
{ +zi-log "{ehi}ERROR:{error} The file linking has been unsuccessful.{rst}"; retval=4; }
} else {
command ln -sf "$url" "$local_dir/$dirname/$filename" &>/dev/null || \
{ +zi-log "{ehi}ERROR:{error} The link of {file}$filename{error} has been unsuccessful"\
"${${(M)OPTS[opt_-q,--quiet]:#1}:+, skip the -q/--quiet option for more information}.{rst}"; retval=4; }
}
}
command ln -svf "$url" "$local_dir/$dirname/$filename" || {
+zi-log "{ehi}ERROR:{error} The file linking has been unsuccessful.{rst}"
retval=4
}
else
command ln -sf "$url" "$local_dir/$dirname/$filename" &> /dev/null || {
+zi-log "{ehi}ERROR:{error} The link of {file}$filename{error} has been unsuccessful" "${${(M)OPTS[opt_-q,--quiet]:#1}:+, skip the -q/--quiet option for more information}.{rst}"
retval=4
}
fi
fi

}

(( retval == 4 )) && { command rmdir "$local_dir/$dirname" 2>/dev/null; return $retval; }
Expand Down

0 comments on commit c94dd72

Please sign in to comment.