Streamline worktree jump with minimal shell wrappers (Bash/Fish)

This commit is contained in:
Paul Irish 2026-03-29 12:15:23 -07:00
parent 641f39a97d
commit 8dfca7811a
No known key found for this signature in database
3 changed files with 21 additions and 23 deletions

View file

@ -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

View file

@ -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.

View file

@ -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`