From 1d8f21b5652939aa44f8b0aa966f3575f8541aa3 Mon Sep 17 00:00:00 2001 From: druckdev Date: Mon, 10 May 2021 16:12:23 +0200 Subject: [PATCH] zsh:functions: Add git-checkout-worktree --- .config/zsh/zshrc.d/40-functions.zsh | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/.config/zsh/zshrc.d/40-functions.zsh b/.config/zsh/zshrc.d/40-functions.zsh index af9842b..9d8352c 100644 --- a/.config/zsh/zshrc.d/40-functions.zsh +++ b/.config/zsh/zshrc.d/40-functions.zsh @@ -384,3 +384,22 @@ nvim-man() { command man "$@" fi } + +# Creates a git worktree checking it out the first argument in a temporary +# directory that is deleted again, if the spawned subshell exits. +git-checkout-worktree() { + local GIT_ROOT TEMP_DIR + GIT_ROOT="$(basename "$(git rev-parse --show-toplevel)")" || return + TEMP_DIR="$(mktemp -dp "${TMPDIR:-/tmp}" "$GIT_ROOT.XXXXX")" + + git worktree add "$TEMP_DIR" "$1" + pushd -q "$TEMP_DIR" + + # Start subshell + "$SHELL" + + # Cleanup when exiting + popd -q + git worktree remove "$TEMP_DIR" + git worktree prune +}