From 8dfca7811a1bc0448a3b573cc69545c5224fd4f7 Mon Sep 17 00:00:00 2001 From: Paul Irish Date: Sun, 29 Mar 2026 12:15:23 -0700 Subject: [PATCH] Streamline worktree jump with minimal shell wrappers (Bash/Fish) --- git-recent | 10 +++------- man/man1/git-recent.1 | 2 +- readme.md | 32 +++++++++++++++++--------------- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/git-recent b/git-recent index 9513c1e..f1e3b0c 100755 --- a/git-recent +++ b/git-recent @@ -128,14 +128,10 @@ if [[ -n "$output" ]] && (( line_count == 0 )); then wt_path=$(_get_worktree_for_branch "$chosen_branch") if [[ -n "$wt_path" ]]; then - echo "Branch '$chosen_branch' is open in worktree: $wt_path" - if [[ "$GIT_RECENT_AUTO_CD" == "1" ]]; then - echo "__CD__:$wt_path" - else - echo "To jump there, run: cd \"$wt_path\"" - fi + echo "Branch '$chosen_branch' is open in worktree: $wt_path" >&2 + echo "$wt_path" else - echo git checkout "$chosen_branch" + echo "git checkout $chosen_branch" >&2 git checkout "$chosen_branch" fi else diff --git a/man/man1/git-recent.1 b/man/man1/git-recent.1 index fb9bccd..ab9c2c9 100644 --- a/man/man1/git-recent.1 +++ b/man/man1/git-recent.1 @@ -17,7 +17,7 @@ Display a help message. .SH KEYBINDINGS .TP .B Enter -Checkout the selected branch. If the branch is already open in a separate git worktree, the script will provide instructions to jump there. (See README for automatic switching via a shell wrapper). +Checkout the selected branch. If the branch is already open in a separate git worktree, the script will output the path to jump there. (See README for automatic switching via a small shell wrapper). .TP .B Up/Down arrow keys Navigate the branch list. diff --git a/readme.md b/readme.md index bf897ce..a657d2e 100644 --- a/readme.md +++ b/readme.md @@ -39,26 +39,28 @@ Hit `ctrl-o` to see the branch diff. #### Worktree Support -If a branch is already open in a separate git worktree, `git recent` will detect it and provide instructions to `cd` there. To enable **automatic** directory switching, add this wrapper function to your `.bashrc` or `.zshrc`: +If a branch is already open in a separate git worktree, `git recent` will detect it and provide instructions to `cd` there. To enable **automatic** directory switching, add one of these wrapper functions to your shell config: +##### Bash / Zsh (in `.bashrc` or `.zshrc`) ```bash -# Optional wrapper to enable automatic worktree jumping -git-recent() { - local output - # Set the flag to let the script know we can handle the CD - output=$(GIT_RECENT_AUTO_CD=1 command git-recent "$@") - - if echo "$output" | grep -q "^__CD__:"; then - local target=$(echo "$output" | grep "^__CD__:" | cut -d: -f2-) - cd "$target" - # Print the output but hide the control line - echo "$output" | grep -v "^__CD__:" - else - echo "$output" - fi +recent() { + local o=$(command git-recent "$@") + [ -d "$o" ] && cd "$o" || [ -n "$o" ] && echo "$o" } ``` +##### Fish (in `~/.config/fish/config.fish`) +```fish +function recent + set -l o (command git-recent $argv) + if test -d "$o" + cd $o + else if test -n "$o" + echo $o + end +end +``` + -------------------------------- # `git recent-og`