Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix running in shells with -u/-o nounset option set #299

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 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 All @@ -115,7 +115,7 @@ _z() {
else
# list/go
local echo fnd last list opt typ
while [ "$1" ]; do case "$1" in
while [ "${1+set}" ]; do case "$1" in
--) while [ "$1" ]; do shift; fnd="$fnd${fnd:+ }$1";done;;
-*) opt=${1:1}; while [ "$opt" ]; do case ${opt:0:1} in
c) fnd="^$PWD $fnd";;
Expand All @@ -127,7 +127,7 @@ _z() {
x) sed -i -e "\:^${PWD}|.*:d" "$datafile";;
esac; opt=${opt:1}; done;;
*) fnd="$fnd${fnd:+ }$1";;
esac; last=$1; [ "$#" -gt 0 ] && shift; done
esac; last="$1"; [ "$#" -gt 0 ] && shift; done
[ "$fnd" -a "$fnd" != "^$PWD " ] || list=1

# if we hit enter on a completion just go there
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