Skip to content

Commit 38d74ba

Browse files
authored
Fix: prefix all commands in forgit.plugin.zsh (#439)
* Fix: prefix all commands in forgit.plugin.zsh This prevents the wrong commands getting executed in case a user added a shell alias with the same name. * Fix: prefix built-ins
1 parent 47fd184 commit 38d74ba

File tree

1 file changed

+57
-54
lines changed

1 file changed

+57
-54
lines changed

forgit.plugin.zsh

Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#!/usr/bin/env bash
22
# MIT (c) Wenxuan Zhang
33

4-
forgit::error() { printf "%b[Error]%b %s\n" '\e[0;31m' '\e[0m' "$@" >&2; return 1; }
5-
forgit::warn() { printf "%b[Warn]%b %s\n" '\e[0;33m' '\e[0m' "$@" >&2; }
4+
# all commands are prefixed with command and all built-ins with builtin.
5+
# These shell built-ins prevent the wrong commands getting executed in case a
6+
# user added a shell alias with the same name.
7+
8+
forgit::error() { command printf "%b[Error]%b %s\n" '\e[0;31m' '\e[0m' "$@" >&2; builtin return 1; }
9+
forgit::warn() { command printf "%b[Warn]%b %s\n" '\e[0;33m' '\e[0m' "$@" >&2; }
610

711
# determine installation path
812
if [[ -n "$ZSH_VERSION" ]]; then
@@ -12,33 +16,32 @@ if [[ -n "$ZSH_VERSION" ]]; then
1216
0="${${(M)0:#/*}:-$PWD/$0}"
1317
FORGIT_INSTALL_DIR="${0:h}"
1418
elif [[ -n "$BASH_VERSION" ]]; then
15-
FORGIT_INSTALL_DIR="$(dirname -- "${BASH_SOURCE[0]}")"
19+
FORGIT_INSTALL_DIR="$(command dirname -- "${BASH_SOURCE[0]}")"
1620
else
1721
forgit::error "Only zsh and bash are supported"
1822
fi
1923

20-
export FORGIT_INSTALL_DIR
24+
builtin export FORGIT_INSTALL_DIR
2125
FORGIT="$FORGIT_INSTALL_DIR/bin/git-forgit"
2226

2327
# backwards compatibility:
2428
# export all user-defined FORGIT variables to make them available in git-forgit
2529
unexported_vars=0
2630
# Set posix mode in bash to only get variables, see #256.
27-
[[ -n "$BASH_VERSION" ]] && set -o posix
28-
set | awk -F '=' '{ print $1 }' | grep FORGIT_ | while read -r var; do
29-
if ! export | grep -q "\(^$var=\|^export $var=\)"; then
30-
if [[ $unexported_vars == 0 ]]; then
31+
[[ -n "$BASH_VERSION" ]] && builtin set -o posix
32+
builtin set | command awk -F '=' '{ print $1 }' | command grep FORGIT_ | while builtin read -r var; do
33+
if ! builtin export | command grep -q "\(^$var=\|^export $var=\)"; then if [[ $unexported_vars == 0 ]]; then
3134
forgit::warn "Config options have to be exported in future versions of forgit."
3235
forgit::warn "Please update your config accordingly:"
3336
fi
3437
forgit::warn " export $var"
3538
unexported_vars=$((unexported_vars + 1))
3639
# shellcheck disable=SC2163
37-
export "$var"
40+
builtin export "$var"
3841
fi
3942
done
40-
unset unexported_vars
41-
[[ -n "$BASH_VERSION" ]] && set +o posix
43+
builtin unset unexported_vars
44+
[[ -n "$BASH_VERSION" ]] && builtin set +o posix
4245

4346
# register shell functions
4447
forgit::log() {
@@ -149,48 +152,48 @@ forgit::attributes() {
149152
# shellcheck disable=SC2139
150153
if [[ -z "$FORGIT_NO_ALIASES" ]]; then
151154

152-
export forgit_add="${forgit_add:-ga}"
153-
export forgit_reset_head="${forgit_reset_head:-grh}"
154-
export forgit_log="${forgit_log:-glo}"
155-
export forgit_reflog="${forgit_reflog:-grl}"
156-
export forgit_diff="${forgit_diff:-gd}"
157-
export forgit_show="${forgit_show:-gso}"
158-
export forgit_ignore="${forgit_ignore:-gi}"
159-
export forgit_attributes="${forgit_attributes:-gat}"
160-
export forgit_checkout_file="${forgit_checkout_file:-gcf}"
161-
export forgit_checkout_branch="${forgit_checkout_branch:-gcb}"
162-
export forgit_checkout_commit="${forgit_checkout_commit:-gco}"
163-
export forgit_checkout_tag="${forgit_checkout_tag:-gct}"
164-
export forgit_branch_delete="${forgit_branch_delete:-gbd}"
165-
export forgit_revert_commit="${forgit_revert_commit:-grc}"
166-
export forgit_clean="${forgit_clean:-gclean}"
167-
export forgit_stash_show="${forgit_stash_show:-gss}"
168-
export forgit_stash_push="${forgit_stash_push:-gsp}"
169-
export forgit_cherry_pick="${forgit_cherry_pick:-gcp}"
170-
export forgit_rebase="${forgit_rebase:-grb}"
171-
export forgit_fixup="${forgit_fixup:-gfu}"
172-
export forgit_blame="${forgit_blame:-gbl}"
173-
174-
alias "${forgit_add}"='forgit::add'
175-
alias "${forgit_reset_head}"='forgit::reset::head'
176-
alias "${forgit_log}"='forgit::log'
177-
alias "${forgit_reflog}"='forgit::reflog'
178-
alias "${forgit_diff}"='forgit::diff'
179-
alias "${forgit_show}"='forgit::show'
180-
alias "${forgit_ignore}"='forgit::ignore'
181-
alias "${forgit_attributes}"='forgit::attributes'
182-
alias "${forgit_checkout_file}"='forgit::checkout::file'
183-
alias "${forgit_checkout_branch}"='forgit::checkout::branch'
184-
alias "${forgit_checkout_commit}"='forgit::checkout::commit'
185-
alias "${forgit_checkout_tag}"='forgit::checkout::tag'
186-
alias "${forgit_branch_delete}"='forgit::branch::delete'
187-
alias "${forgit_revert_commit}"='forgit::revert::commit'
188-
alias "${forgit_clean}"='forgit::clean'
189-
alias "${forgit_stash_show}"='forgit::stash::show'
190-
alias "${forgit_stash_push}"='forgit::stash::push'
191-
alias "${forgit_cherry_pick}"='forgit::cherry::pick::from::branch'
192-
alias "${forgit_rebase}"='forgit::rebase'
193-
alias "${forgit_fixup}"='forgit::fixup'
194-
alias "${forgit_blame}"='forgit::blame'
155+
builtin export forgit_add="${forgit_add:-ga}"
156+
builtin export forgit_reset_head="${forgit_reset_head:-grh}"
157+
builtin export forgit_log="${forgit_log:-glo}"
158+
builtin export forgit_reflog="${forgit_reflog:-grl}"
159+
builtin export forgit_diff="${forgit_diff:-gd}"
160+
builtin export forgit_show="${forgit_show:-gso}"
161+
builtin export forgit_ignore="${forgit_ignore:-gi}"
162+
builtin export forgit_attributes="${forgit_attributes:-gat}"
163+
builtin export forgit_checkout_file="${forgit_checkout_file:-gcf}"
164+
builtin export forgit_checkout_branch="${forgit_checkout_branch:-gcb}"
165+
builtin export forgit_checkout_commit="${forgit_checkout_commit:-gco}"
166+
builtin export forgit_checkout_tag="${forgit_checkout_tag:-gct}"
167+
builtin export forgit_branch_delete="${forgit_branch_delete:-gbd}"
168+
builtin export forgit_revert_commit="${forgit_revert_commit:-grc}"
169+
builtin export forgit_clean="${forgit_clean:-gclean}"
170+
builtin export forgit_stash_show="${forgit_stash_show:-gss}"
171+
builtin export forgit_stash_push="${forgit_stash_push:-gsp}"
172+
builtin export forgit_cherry_pick="${forgit_cherry_pick:-gcp}"
173+
builtin export forgit_rebase="${forgit_rebase:-grb}"
174+
builtin export forgit_fixup="${forgit_fixup:-gfu}"
175+
builtin export forgit_blame="${forgit_blame:-gbl}"
176+
177+
builtin alias "${forgit_add}"='forgit::add'
178+
builtin alias "${forgit_reset_head}"='forgit::reset::head'
179+
builtin alias "${forgit_log}"='forgit::log'
180+
builtin alias "${forgit_reflog}"='forgit::reflog'
181+
builtin alias "${forgit_diff}"='forgit::diff'
182+
builtin alias "${forgit_show}"='forgit::show'
183+
builtin alias "${forgit_ignore}"='forgit::ignore'
184+
builtin alias "${forgit_attributes}"='forgit::attributes'
185+
builtin alias "${forgit_checkout_file}"='forgit::checkout::file'
186+
builtin alias "${forgit_checkout_branch}"='forgit::checkout::branch'
187+
builtin alias "${forgit_checkout_commit}"='forgit::checkout::commit'
188+
builtin alias "${forgit_checkout_tag}"='forgit::checkout::tag'
189+
builtin alias "${forgit_branch_delete}"='forgit::branch::delete'
190+
builtin alias "${forgit_revert_commit}"='forgit::revert::commit'
191+
builtin alias "${forgit_clean}"='forgit::clean'
192+
builtin alias "${forgit_stash_show}"='forgit::stash::show'
193+
builtin alias "${forgit_stash_push}"='forgit::stash::push'
194+
builtin alias "${forgit_cherry_pick}"='forgit::cherry::pick::from::branch'
195+
builtin alias "${forgit_rebase}"='forgit::rebase'
196+
builtin alias "${forgit_fixup}"='forgit::fixup'
197+
builtin alias "${forgit_blame}"='forgit::blame'
195198

196199
fi

0 commit comments

Comments
 (0)