2 hours ago · Tech · hide · 0 comments

♠ Previously. Problem statement: make my cdg shortcut jump from a linked Git worktree back to the main checkout when already at the worktree root. The shortcut originally stopped at the current checkout’s root: alias cdg='cd "$(git root)"' I wanted two consecutive calls from a worktree subdirectory to behave like the following: worktree/subdirectory ↓ cdg worktree (at the root) ↓ cdg main checkout (at the root) An alias cannot succinctly express the necessary conditional behavior, whereas an external script cannot change its parent shell’s working directory. A shell function can: cdg() { # This file is sourced by bash and zsh, both of which support local. # shellcheck disable=SC3043 local line main_is_bare main_worktree root worktrees root=$(git rev-parse --path-format=absolute --show-toplevel) || return if [ "$(pwd -P)" != "$root" ]; then cd "$root" || return return fi worktrees=$(git worktree list --porcelain) || return main_worktree= main_is_bare=false while IFS= read -r line; do…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.