fix(bash): fix handling of the preserved DEBUG trap (used in Bash <= 4.3) (#5908)

* refactor(bash): use `STARSHIP_*` for the internal variable names

The current codebase uses `dbg_trap` to save the original DEBUG trap
in bash <= 4.3.  However, the variable name possibly conflicts a user
variable since it is not prefixed by `_starship` or `starship_` or
`STARSHIP_`.  In this patch, we rename `dbg_trap` to
`STARSHIP_DEBUG_TRAP` following other variables of
`STARSHIP_EXIT_STATUS` and `STARSHIP_PIPE_STATUS`.  We also rename the
variable `_PRESERVED_PROMPT_COMMAND` to `STARSHIP_PROMPT_COMMAND`.

* fix(bash): correctly extract DEBUG trap with spaces

* fix(bash): evaluate DEBUG trap by eval

The current code executes the saved DEBUG trap just by
$_starship_dbg_trap.  This causes various problems.  The content of
the variable `_starship_dbg_trap` is unexpectedly subject to the word
splitting with the pathname expansions. Also, the needed shell
expansions and quote removal are not performed.  With a custom IFS,
the content of the variable will be split in an unexpected way.  The
saved DEBUG trap needs to be executed by eval "$_starship_dbg_trap".
This commit is contained in:
Koichi Murase 2024-04-16 15:16:00 +09:00 committed by GitHub
parent 2716db6d32
commit fe1a8df6fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 8 additions and 7 deletions

View File

@ -65,8 +65,8 @@ starship_precmd() {
# command pipeline, which may rely on it.
_starship_set_return "$STARSHIP_CMD_STATUS"
if [[ -n "${_PRESERVED_PROMPT_COMMAND-}" ]]; then
eval "$_PRESERVED_PROMPT_COMMAND"
if [[ -n "${STARSHIP_PROMPT_COMMAND-}" ]]; then
eval "$STARSHIP_PROMPT_COMMAND"
fi
local -a ARGS=(--terminal-width="${COLUMNS}" --status="${STARSHIP_CMD_STATUS}" --pipestatus="${STARSHIP_PIPE_STATUS[*]}" --jobs="${NUM_JOBS}")
@ -110,12 +110,13 @@ else
# We want to avoid destroying an existing DEBUG hook. If we detect one, create
# a new function that runs both the existing function AND our function, then
# re-trap DEBUG to use this new function. This prevents a trap clobber.
dbg_trap="$(trap -p DEBUG | cut -d' ' -f3 | tr -d \')"
if [[ -z "$dbg_trap" ]]; then
eval "STARSHIP_DEBUG_TRAP=($(trap -p DEBUG))"
STARSHIP_DEBUG_TRAP=("${STARSHIP_DEBUG_TRAP[2]}")
if [[ -z "$STARSHIP_DEBUG_TRAP" ]]; then
trap 'starship_preexec "$_"' DEBUG
elif [[ "$dbg_trap" != 'starship_preexec "$_"' && "$dbg_trap" != 'starship_preexec_all "$_"' ]]; then
elif [[ "$STARSHIP_DEBUG_TRAP" != 'starship_preexec "$_"' && "$STARSHIP_DEBUG_TRAP" != 'starship_preexec_all "$_"' ]]; then
starship_preexec_all() {
local PREV_LAST_ARG=$1 ; $dbg_trap; starship_preexec; : "$PREV_LAST_ARG";
local PREV_LAST_ARG=$1 ; eval -- "$STARSHIP_DEBUG_TRAP"; starship_preexec; : "$PREV_LAST_ARG";
}
trap 'starship_preexec_all "$_"' DEBUG
fi
@ -130,7 +131,7 @@ else
# Prepending to PROMPT_COMMAND breaks "command duration" module.
# So, we are preserving the existing PROMPT_COMMAND
# which will be executed later in the starship_precmd function
_PRESERVED_PROMPT_COMMAND="$PROMPT_COMMAND"
STARSHIP_PROMPT_COMMAND="$PROMPT_COMMAND"
PROMPT_COMMAND="starship_precmd"
fi
fi