Skip to content

Commit

Permalink
refactor: use more readable empty-string tests
Browse files Browse the repository at this point in the history
replace:
- `test -n "${variable}"` with `test "${variable}" != ''`, and
- `test -z "${variable}"` with `test "${variable}" = ''`

https://github.com/anordal/shellharden/blob/35411af8d466fb8b75f9769c7bf4bcc0cb2a2920/how_to_do_things_safely_in_bash.md#are-empty-string-comparisons-any-special

Signed-off-by: Lucas Larson <[email protected]>
  • Loading branch information
LucasLarson committed May 3, 2024
1 parent 6a236e9 commit dc5be00
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
8 changes: 4 additions & 4 deletions .zshrc
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ EDITOR="$(
command -v -- vim ||
command -v -- vi
)"
if test -n "${EDITOR-}"; then
if test "${EDITOR-}" != ''; then
export EDITOR
alias e='command "${EDITOR:-vi}"'
export VISUAL="${VISUAL:-${EDITOR:-vi}}"
Expand Down Expand Up @@ -210,7 +210,7 @@ set -o share_history
set -o interactive_comments

## C, C++
if test -n "$(command xcrun --show-sdk-path 2>/dev/null)"; then
if test "$(command xcrun --show-sdk-path 2>/dev/null)" != ''; then
CPATH="$(command xcrun --show-sdk-path)"'/usr/include'"${CPATH:+:${CPATH-}}"
export CPATH
fi
Expand All @@ -223,7 +223,7 @@ if test -d '/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib'; then
fi

## Go
test -n "${GOPATH-}" &&
test "${GOPATH-}" != '' &&
test -d "${GOPATH-}"'/bin' &&
PATH="${GOPATH-}"'/bin'"${PATH:+:${PATH-}}"

Expand All @@ -237,7 +237,7 @@ rbenv() {

## PATHs
# prevent duplicate entries
test -n "${ZSH-}" &&
test "${ZSH-}" != '' &&
export -U \
PATH path \
CDPATH cdpath \
Expand Down
20 changes: 10 additions & 10 deletions custom/aliases.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -320,17 +320,17 @@ cleanup() {
# delete crufty Zsh files
# if `$ZSH_COMPDUMP` always generates a crufty file then skip
# https://stackoverflow.com/a/8811800
if test -n "${ZSH_COMPDUMP-}" &&
if test "${ZSH_COMPDUMP-}" != '' &&
test ! "${ZSH_COMPDUMP-}" != "${ZSH_COMPDUMP#*'zcompdump-'}" &&
test ! "${ZSH_COMPDUMP-}" != "${ZSH_COMPDUMP#*'zcompdump.'}"; then
while test -n "$(
while test "$(
command find -- "${HOME%/}" \
-maxdepth 1 \
-type f \
! -name "$(printf -- "*\n*")" \
! -name '.zcompdump' \
-name '.zcompdump*'
)"; do
)" != ''; do
command find -- "${HOME%/}" \
-maxdepth 1 \
-type f \
Expand Down Expand Up @@ -421,8 +421,8 @@ alias cp='cp -R'
alias cpplint_r='command cpplint --counting=detailed --verbose=0 --filter=-legal/copyright --recursive -- .'

cy() {
test -n "${DOTFILES-}" &&
test -n "${TEMPLATE-}" ||
test "${DOTFILES-}" != '' &&
test "${TEMPLATE-}" != '' ||
return 1

target="$(command git rev-parse --show-toplevel 2>/dev/null || command pwd -L)" ||
Expand Down Expand Up @@ -850,7 +850,7 @@ git_add() {
shift
;;
-o | --others | --untracked)
while test -n "$(command git ls-files --others --exclude-standard)"; do
while test "$(command git ls-files --others --exclude-standard)" != ''; do
command git ls-files -z --others --exclude-standard |
command xargs -0 git add --verbose 2>/dev/null
done &&
Expand Down Expand Up @@ -949,7 +949,7 @@ alias gca='git_commit --amend'
git_config_file_locations() {
for scope in global system local worktree; do
# do not return `.git/config` if called from outside a git repository
test -z "$(command git config --list --show-scope --"${scope-}" 2>/dev/null)" ||
test "$(command git config --list --show-scope --"${scope-}" 2>/dev/null)" = '' ||
printf -- '%-10s%s\n' "${scope-}" "$(
command git config --list --show-origin --"${scope-}" |
command sed \
Expand All @@ -965,15 +965,15 @@ git_config_file_locations() {

unalias -- 'gd' 2>/dev/null
gd() {
if test -n "$(command git diff "$@" 2>/dev/null)"; then
if test "$(command git diff "$@" 2>/dev/null)" != ''; then
command git diff "$@"
else
command git diff --cached "$@"
fi
}
unalias -- 'gds' 2>/dev/null
gds() {
if test -n "$(command git diff --cached "$@" 2>/dev/null)"; then
if test "$(command git diff --cached "$@" 2>/dev/null)" != ''; then
command git diff --cached "$@"
else
command git diff "$@"
Expand Down Expand Up @@ -1058,7 +1058,7 @@ git_commit_initial_commit() {
fi
command git commit --allow-empty --signoff --verbose --message="$(printf -- '\360\237\214\263\302\240 root commit')"
# if there are non-repository files present, then add them and commit
if test -n "$(command git ls-files --others --exclude-standard)"; then
if test "$(command git ls-files --others --exclude-standard)" != ''; then
command git add --verbose -- . &&
command git commit --signoff --verbose --message="$(printf -- '\342\234\250\302\240 initial commit')"
fi
Expand Down

0 comments on commit dc5be00

Please sign in to comment.