Skip to content

Commit

Permalink
Export user-defined FORGIT variables for backwards compatibility
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
carlfriedrich committed Nov 17, 2022
1 parent 49def1f commit 372239f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
23 changes: 21 additions & 2 deletions conf.d/forgit.plugin.fish
Original file line number Diff line number Diff line change
@@ -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
Expand Down
23 changes: 20 additions & 3 deletions forgit.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,37 @@
# 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
# shellcheck disable=2277,2296,2299
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() {
Expand Down

0 comments on commit 372239f

Please sign in to comment.