Export user-defined FORGIT variables for backwards compatibility

Before forgit became a standalone script, users defined their FORGIT
variables (e.g. FORGIT_FZF_DEFAULT_OPTS) in the shell environment before
sourcing the forgit shell plugin. This worked because the forgit code
was executed within the currently running shell.
With forgit being an executable script, these variables are only
available to forgit when being exported. This patch adds an automatism
for this so that users do not have to change their configuration.
A warning is issued in this case so that users get notified to update
their configuration.
This commit is contained in:
carlfriedrich 2022-11-14 17:42:44 +01:00
parent 49def1fe91
commit 372239fd37
2 changed files with 41 additions and 5 deletions

View file

@ -1,7 +1,26 @@
# MIT (c) Chris Apple
set -g FORGIT_INSTALL_DIR (dirname (dirname (status -f)))
set -g FORGIT "$FORGIT_INSTALL_DIR/conf.d/bin/git-forgit"
set INSTALL_DIR (dirname (dirname (status -f)))
set FORGIT "$INSTALL_DIR/conf.d/bin/git-forgit"
function forgit::warn
printf "%b[Warn]%b %s\n" '\e[0;33m' '\e[0m' "$argv" >&2;
end
# backwards compatibility:
# export all user-defined FORGIT variables to make them available in git-forgit
set unexported_vars 0
set | awk -F ' ' '{ print $1 }' | grep FORGIT_ | while read var
if ! set -x | grep -q "^$var "
if test $unexported_vars = 0
forgit::warn "Config options have to be exported in future versions of forgit."
forgit::warn "Please update your config accordingly:"
end
forgit::warn " set -x $var \"$$var\""
set unexported_vars (math $unexported_vars + 1)
set -x $var $$var
end
end
function forgit::log -d "git commit viewer"
"$FORGIT" log $argv

View file

@ -2,6 +2,7 @@
# MIT (c) Wenxuan Zhang
forgit::error() { printf "%b[Error]%b %s\n" '\e[0;31m' '\e[0m' "$@" >&2; return 1; }
forgit::warn() { printf "%b[Warn]%b %s\n" '\e[0;33m' '\e[0m' "$@" >&2; }
# determine installation path
if [[ -n "$ZSH_VERSION" ]]; then
@ -9,13 +10,29 @@ if [[ -n "$ZSH_VERSION" ]]; then
0="${ZERO:-${${0:#$ZSH_ARGZERO}:-${(%):-%N}}}"
# shellcheck disable=2277,2296,2298
0="${${(M)0:#/*}:-$PWD/$0}"
FORGIT_INSTALL_DIR="${0:h}"
INSTALL_DIR="${0:h}"
elif [[ -n "$BASH_VERSION" ]]; then
FORGIT_INSTALL_DIR="$(dirname -- "${BASH_SOURCE[0]}")"
INSTALL_DIR="$(dirname -- "${BASH_SOURCE[0]}")"
else
forgit::error "Only zsh and bash are supported"
fi
export FORGIT="$FORGIT_INSTALL_DIR/bin/git-forgit"
FORGIT="$INSTALL_DIR/bin/git-forgit"
# backwards compatibility:
# export all user-defined FORGIT variables to make them available in git-forgit
unexported_vars=0
set | awk -F '=' '{ print $1 }' | grep FORGIT_ | while read -r var; do
if ! export | grep -q "^$var="; then
if [[ $unexported_vars == 0 ]]; then
forgit::warn "Config options have to be exported in future versions of forgit."
forgit::warn "Please update your config accordingly:"
fi
forgit::warn " export $var"
unexported_vars=$((unexported_vars + 1))
# shellcheck disable=SC2163
export "$var"
fi
done
# register shell functions
forgit::log() {