diff --git a/src/init/bash.sh b/src/init/bash.sh index ccc4eed..7dfaf27 100644 --- a/src/init/bash.sh +++ b/src/init/bash.sh @@ -1,3 +1,9 @@ +_jjf_drop_last_history() { + local _off + _off=$(history 1 2>/dev/null | awk '{print $1}') + [ -n "$_off" ] && history -d "$_off" 2>/dev/null || true +} + jjf() { # No args → default to `show` so `jjf` alone is a rev browser. if [ $# -eq 0 ]; then @@ -9,13 +15,13 @@ jjf() { return $? fi local _jjf_cmd - _jjf_cmd="$(command jjf --emit "$@")" || return $? - [ -n "$_jjf_cmd" ] || return 130 - # Replace the original `jjf …` history entry with the resolved jj command. - local _jjf_last_offset - _jjf_last_offset=$(history 1 2>/dev/null | awk '{print $1}') - if [ -n "$_jjf_last_offset" ]; then - history -d "$_jjf_last_offset" 2>/dev/null || true + _jjf_cmd="$(command jjf --emit "$@")" + local _jjf_rc=$? + # Always drop the `jjf …` entry from history; on cancel/error there's + # nothing to replace it with, on success we add the resolved command below. + _jjf_drop_last_history + if [ $_jjf_rc -ne 0 ] || [ -z "$_jjf_cmd" ]; then + return $_jjf_rc fi history -s -- "$_jjf_cmd" sh -c "$_jjf_cmd" diff --git a/src/init/fish.fish b/src/init/fish.fish index 02e8a05..827b53a 100644 --- a/src/init/fish.fish +++ b/src/init/fish.fish @@ -9,6 +9,10 @@ function jjf command jjf $argv return $status end + # Arm the postexec hook *before* invoking the picker so the original + # `jjf …` entry is removed from history on any exit path (cancel, error, + # or success). Fish records function invocations *after* return. + set -g _jjf_pending_swap 1 set -l _jjf_cmd (command jjf --emit $argv) set -l _jjf_status $status if test $_jjf_status -ne 0 @@ -18,9 +22,6 @@ function jjf return 130 end builtin history append -- $_jjf_cmd - # Signal the postexec hook to remove the literal `jjf …` entry that fish - # is about to record (fish records function invocations *after* return). - set -g _jjf_pending_swap 1 sh -c $_jjf_cmd end