From 372239fd37dbf9e3a0c012614d99a1e4e8b6eaff Mon Sep 17 00:00:00 2001 From: carlfriedrich Date: Mon, 14 Nov 2022 17:42:44 +0100 Subject: [PATCH] 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. --- conf.d/forgit.plugin.fish | 23 +++++++++++++++++++++-- forgit.plugin.zsh | 23 ++++++++++++++++++++--- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/conf.d/forgit.plugin.fish b/conf.d/forgit.plugin.fish index 3f9cc2d..03843ca 100644 --- a/conf.d/forgit.plugin.fish +++ b/conf.d/forgit.plugin.fish @@ -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 diff --git a/forgit.plugin.zsh b/forgit.plugin.zsh index decd5ea..21bf56c 100755 --- a/forgit.plugin.zsh +++ b/forgit.plugin.zsh @@ -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() {