Skip to content

Commit

Permalink
Fix running in shells with -u/-o nounset option set
Browse files Browse the repository at this point in the history
  • Loading branch information
ntninja committed May 16, 2021
1 parent 125f4dc commit 8ad15bd
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions z.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ _z() {
[ -h "$datafile" ] && datafile=$(readlink "$datafile")

# bail if we don't own ~/.z and $_Z_OWNER not set
[ -z "$_Z_OWNER" -a -f "$datafile" -a ! -O "$datafile" ] && return
[ -z "${_Z_OWNER+set}" -a -f "$datafile" -a ! -O "$datafile" ] && return

_z_dirs () {
local line
Expand Down Expand Up @@ -93,7 +93,7 @@ _z() {
if [ $? -ne 0 -a -f "$datafile" ]; then
env rm -f "$tempfile"
else
[ "$_Z_OWNER" ] && chown $_Z_OWNER:"$(id -ng $_Z_OWNER)" "$tempfile"
[ "${_Z_OWNER+set}" ] && chown "$_Z_OWNER:$(id -ng "$_Z_OWNER")" "$tempfile"
env mv -f "$tempfile" "$datafile" || env rm -f "$tempfile"
fi

Expand Down Expand Up @@ -222,13 +222,14 @@ _z() {

alias ${_Z_CMD:-z}='_z 2>&1'

[ "$_Z_NO_RESOLVE_SYMLINKS" ] || _Z_RESOLVE_SYMLINKS="-P"
[ "${_Z_NO_RESOLVE_SYMLINKS+set}" ] || _Z_RESOLVE_SYMLINKS="-P"

if type compctl >/dev/null 2>&1; then
# zsh
[ "$_Z_NO_PROMPT_COMMAND" ] || {
typeset -a _Z_EXCLUDE_DIRS
[ "${_Z_NO_PROMPT_COMMAND+set}" ] || {
# populate directory list, avoid clobbering any other precmds.
if [ "$_Z_NO_RESOLVE_SYMLINKS" ]; then
if [ "${_Z_NO_RESOLVE_SYMLINKS+set}" ]; then
_z_precmd() {
(_z --add "${PWD:a}" &)
: $RANDOM
Expand All @@ -252,9 +253,10 @@ if type compctl >/dev/null 2>&1; then
compctl -U -K _z_zsh_tab_completion _z
elif type complete >/dev/null 2>&1; then
# bash
typeset -a _Z_EXCLUDE_DIRS
# tab completion
complete -o filenames -C '_z --complete "$COMP_LINE"' ${_Z_CMD:-z}
[ "$_Z_NO_PROMPT_COMMAND" ] || {
[ "${_Z_NO_PROMPT_COMMAND+set}" ] || {
# populate directory list. avoid clobbering other PROMPT_COMMANDs.
grep "_z --add" <<< "$PROMPT_COMMAND" >/dev/null || {
PROMPT_COMMAND="$PROMPT_COMMAND"$'\n''(_z --add "$(command pwd '$_Z_RESOLVE_SYMLINKS' 2>/dev/null)" 2>/dev/null &);'
Expand Down

0 comments on commit 8ad15bd

Please sign in to comment.