Improve git worktree session workflow

This commit is contained in:
2026-08-10 12:42:53 +01:00
parent c97c9cb027
commit 56b5bdb5ac
6 changed files with 67 additions and 4 deletions

35
layouts/session-project Executable file
View File

@@ -0,0 +1,35 @@
#!/usr/bin/env zsh
# usage: session-project <project>
#
# Layout for a session created by `session -p <project>`, where <project> is a
# path in ~/Projects with the prefix removed. The session-created hook passes
# the project along, taking it from the session environment.
project=$1
repo=$HOME/Projects/$project
tmux rename-window home
$(dirname $0)/window-auto
# Without a project there is nothing but the home window, which is all a
# session created directly from this layout's name can get.
if [[ -z "$project" ]] || ! git -C "$repo" rev-parse --git-dir &>/dev/null; then
exit
fi
# The remote's default branch, falling back to whatever HEAD points at for
# repositories without an origin/HEAD, e.g. those with no remote at all.
branch=$(git -C "$repo" symbolic-ref --short refs/remotes/origin/HEAD 2>/dev/null)
branch=${branch#origin/}
if [[ -z "$branch" ]]; then
branch=$(git -C "$repo" symbolic-ref --short HEAD 2>/dev/null)
fi
if [[ -z "$branch" ]]; then
# Detached HEAD, name the window after the commit it sits on.
branch=$(git -C "$repo" rev-parse --short HEAD 2>/dev/null)
fi
tmux new-window -c "$repo"
tmux rename-window "$branch"
$(dirname $0)/window-auto