Skip to content

Commit

Permalink
fix(bash): rework #1509 to recover from the preexec failure (#1729)
Browse files Browse the repository at this point in the history
In GitHub #1509, we blocked the unintended preexec event caused by the
keybinding of Atuin.  However, with that fix, the preexec event for
the intended user command is still missing.  In this patch, we try to
manually run the preexec hook when we detected the unintended preexec
(which means the missing intended preexec).

References:

#1509
https://forum.atuin.sh/t/atuin-bash-and-ble-sh/175
#1003 (comment)
#1727
#1728
  • Loading branch information
akinomyoga authored Feb 17, 2024
1 parent 07f363b commit 38dfaab
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions atuin/src/shell/atuin.bash
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@ export ATUIN_SESSION
ATUIN_HISTORY_ID=""

__atuin_preexec() {
# Workaround for old versions of bash-preexec
if [[ ! ${BLE_ATTACHED-} ]]; then
# With bash-preexec, preexec may be called even for the command run by
# keybindings. There is no general and robust way to detect the
# command for keybindings, but at least we want to exclude Atuin's
# keybindings.
[[ $BASH_COMMAND == '__atuin_history'* && $BASH_COMMAND != "$1" ]] && return 0
# In older versions of bash-preexec, the preexec hook may be called
# even for the commands run by keybindings. There is no general and
# robust way to detect the command for keybindings, but at least we
# want to exclude Atuin's keybindings. When the preexec hook is called
# for a keybinding, the preexec hook for the user command will not
# fire, so we instead set a fake ATUIN_HISTORY_ID here to notify
# __atuin_precmd of this failure.
if [[ $BASH_COMMAND == '__atuin_history'* && $BASH_COMMAND != "$1" ]]; then
ATUIN_HISTORY_ID=__bash_preexec_failure__
return 0
fi
fi

local id
Expand All @@ -36,6 +43,17 @@ __atuin_precmd() {

[[ ! $ATUIN_HISTORY_ID ]] && return

# If the previous preexec hook failed, we manually call __atuin_preexec
if [[ $ATUIN_HISTORY_ID == __bash_preexec_failure__ ]]; then
# This is the command extraction code taken from bash-preexec
local previous_command
previous_command=$(
export LC_ALL=C HISTTIMEFORMAT=''
builtin history 1 | sed '1 s/^ *[0-9][0-9]*[* ] //'
)
__atuin_preexec "$previous_command"
fi

local duration=""
if ((BASH_VERSINFO[0] >= 5)); then
# We use the high-resolution duration based on EPOCHREALTIME (bash >=
Expand Down

0 comments on commit 38dfaab

Please sign in to comment.