Skip to content

Fix condition where pre-exec hooks would not trigger #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions bash-preexec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ __bp_set_ret_value() {
return ${1:-}
}

# Sets the last command we ran. This is used to check if we're still executing
# the same command at the next run of preexec (e.g. for `ls ; ls` we will
# trigger through the DEBUG trap twice.
__bp_set_last_command() {
__bp_last_command="$1"
}

__bp_in_prompt_command() {

local prompt_command_array
Expand Down Expand Up @@ -180,9 +187,18 @@ __bp_preexec_invoke_exec() {
# an interactively issued command.
return
fi
if [[ -z "${__bp_preexec_interactive_mode:-}" ]]; then
# We're doing something related to displaying the prompt. Let the
# prompt set the title instead of me.

local this_command
this_command=$(
export LC_ALL=C
HISTTIMEFORMAT= builtin history 1 | sed '1 s/^ *[0-9][0-9]*[* ] //'
)

if [[ -z "${__bp_preexec_interactive_mode:-}" && \
"$this_command" != "$__bp_last_command" ]]; then
# We're doing something related to displaying the prompt and this
# isn't a follow-up to our last command, let the prompt set the
# title instead of me.
return
else
# If we're in a subshell, then the prompt won't be re-displayed to put
Expand All @@ -202,12 +218,6 @@ __bp_preexec_invoke_exec() {
return
fi

local this_command
this_command=$(
export LC_ALL=C
HISTTIMEFORMAT= builtin history 1 | sed '1 s/^ *[0-9][0-9]*[* ] //'
)

# Sanity check to make sure we have something to invoke our function with.
if [[ -z "$this_command" ]]; then
return
Expand All @@ -226,6 +236,7 @@ __bp_preexec_invoke_exec() {
# Only execute each function if it actually exists.
# Test existence of function with: declare -[fF]
if type -t "$preexec_function" 1>/dev/null; then
__bp_set_last_command "$this_command"
__bp_set_ret_value ${__bp_last_ret_value:-}
# Quote our function invocation to prevent issues with IFS
"$preexec_function" "$this_command"
Expand All @@ -242,6 +253,7 @@ __bp_preexec_invoke_exec() {
# If `extdebug` is enabled a non-zero return value from any preexec function
# will cause the user's command not to execute.
# Run `shopt -s extdebug` to enable
__bp_set_last_command "$this_command"
__bp_set_ret_value "$preexec_ret_value" "$__bp_last_argument_prev_command"
}

Expand Down
23 changes: 23 additions & 0 deletions test/bash-preexec.bats
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ test_preexec_echo() {
printf "%s\n" "$1"
}

# This function is used when you need a non-zero return, as zero is the default
test_preexec_ret_error() {
return 1
}

@test "__bp_install_after_session_init should exit with 1 if we're not using bash" {
unset BASH_VERSION
run '__bp_install_after_session_init'
Expand Down Expand Up @@ -292,3 +297,21 @@ a multiline string'" ]
[ $status -eq 0 ]
[ "$output" == '-n' ]
}

@test "preexec should trigger when several processes are executed, from the same interactive CLI command" {
preexec_functions+=(test_preexec_ret_error)
history -s -- 'ls;ls'
__bp_interactive_mode
run '__bp_preexec_invoke_exec'
# __bp_interactive_mode is now set to empty string, but history still matches
# so we should still be running the preexec hook when the second `ls` runs.
run '__bp_preexec_invoke_exec'
[ $status -eq 1 ]

# We should now exit without running the preexec hook
# which results in the return defaulting to 0.
__bp_preexec_interactive_mode=""
__bp_last_command=""
run '__bp_preexec_invoke_exec'
[ $status -eq 0 ]
}