Skip to content

Commit a470aad

Browse files
committed
Do not modify the first subscript of array PROMPT_COMMAND
With Bash 5.1, PROMPT_COMMAND can be an array. Respect the users choice and support array PROMPT_COMMAND. If PROMPT_COMMAND isn't set, default to array as it doesn't require the use of `make_prompt_command_clean` to function.
1 parent 08a4af2 commit a470aad

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

gitprompt.sh

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,9 +734,25 @@ function add_to_end_of_prompt_command() {
734734
}
735735

736736
function gp_install_prompt {
737-
make_prompt_command_clean
738-
add_to_end_of_prompt_command "setGitPrompt"
739-
add_to_beginning_of_prompt_command "setLastCommandState"
737+
# 5.1 supports PROMPT_COMMAND as an array
738+
if ((BASH_VERSINFO[0] > 5 || BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >= 1)); then
739+
if [[ $(declare -p PROMPT_COMMAND 2>/dev/null) == "declare --"* ]]; then
740+
make_prompt_command_clean
741+
add_to_end_of_prompt_command "setGitPrompt"
742+
add_to_beginning_of_prompt_command "setLastCommandState"
743+
else
744+
if [[ "${PROMPT_COMMAND[*]}" != *setGitPrompt* ]]; then
745+
PROMPT_COMMAND+=(setGitPrompt)
746+
fi
747+
if [[ "${PROMPT_COMMAND[*]}" != *setLastCommandState* ]]; then
748+
PROMPT_COMMAND=(setLastCommandState "${PROMPT_COMMAND[@]}")
749+
fi
750+
fi
751+
else
752+
make_prompt_command_clean
753+
add_to_end_of_prompt_command "setGitPrompt"
754+
add_to_beginning_of_prompt_command "setLastCommandState"
755+
fi
740756

741757
set_git_prompt_dir
742758
source "${__GIT_PROMPT_DIR}/git-prompt-help.sh"

0 commit comments

Comments
 (0)