From 5650a14d55ce05fa85fc00e37365e847782876e8 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Wed, 29 Apr 2026 13:57:37 -0600 Subject: [PATCH] fix(history): drop jjf entry on cancel/error paths too MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, the original `jjf …` invocation only got dropped from history on successful confirm. A cancelled or errored picker left a useless `jjf …` entry behind. Now bash drops it unconditionally via `_jjf_drop_last_history`; fish arms the postexec hook before the `--emit` call so the deletion happens regardless of exit path. --- src/init/bash.sh | 20 +++++++++++++------- src/init/fish.fish | 7 ++++--- 2 files changed, 17 insertions(+), 10 deletions(-) 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