From 9968edaf09bd28fe7f84448802d5daa994927d2d Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Thu, 31 Mar 2022 15:19:56 +0200 Subject: [PATCH] zsh: Make `git-checkout-worktree` autoloadable Transform `git-checkout-worktree` into an autoloadable function. --- .../zsh/autoload/git/git-checkout-worktree | 30 +++++++++++++++++++ .config/zsh/zshrc.d/40-functions.zsh | 30 ------------------- 2 files changed, 30 insertions(+), 30 deletions(-) create mode 100755 .config/zsh/autoload/git/git-checkout-worktree diff --git a/.config/zsh/autoload/git/git-checkout-worktree b/.config/zsh/autoload/git/git-checkout-worktree new file mode 100755 index 0000000..f12691a --- /dev/null +++ b/.config/zsh/autoload/git/git-checkout-worktree @@ -0,0 +1,30 @@ +#!/usr/bin/env zsh + +# Creates a git worktree checking it out the first argument in a temporary +# directory that is deleted again, if the spawned subshell exits. + +local GIT_ROOT TEMP_DIR REPO_DIR +GIT_ROOT="$(basename "$(git rev-parse --show-toplevel)")" || return +TEMP_DIR="$(mktemp -d)" +REPO_DIR="$TEMP_DIR/$GIT_ROOT" + +git worktree add "$REPO_DIR" "$1" +[[ -e "$REPO_DIR" ]] || return 1 +pushd -q "$REPO_DIR" + +# Start subshell +"$SHELL" + +# Cleanup when exiting +popd -q + +# Restart the subshell until every issue is resolved and the worktree is +# removed +until [[ ! -e "$REPO_DIR" ]] || git worktree remove "$REPO_DIR"; do + pushd -q "$REPO_DIR" + "$SHELL" + popd -q +done + +git worktree prune +command rm -rf "$TEMP_DIR" diff --git a/.config/zsh/zshrc.d/40-functions.zsh b/.config/zsh/zshrc.d/40-functions.zsh index f0745f3..7299234 100644 --- a/.config/zsh/zshrc.d/40-functions.zsh +++ b/.config/zsh/zshrc.d/40-functions.zsh @@ -416,36 +416,6 @@ nvim-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 REPO_DIR - GIT_ROOT="$(basename "$(git rev-parse --show-toplevel)")" || return - TEMP_DIR="$(mktemp -d)" - REPO_DIR="$TEMP_DIR/$GIT_ROOT" - - git worktree add "$REPO_DIR" "$1" - [[ -e "$REPO_DIR" ]] || return 1 - pushd -q "$REPO_DIR" - - # Start subshell - "$SHELL" - - # Cleanup when exiting - popd -q - - # Restart the subshell until every issue is resolved and the worktree is - # removed - until [[ ! -e "$REPO_DIR" ]] || git worktree remove "$REPO_DIR"; do - pushd -q "$REPO_DIR" - "$SHELL" - popd -q - done - - git worktree prune - command rm -rf "$TEMP_DIR" -} - # This is meant for adding a quick fix to a commit. # It automatically rebases a given commit (defaults to HEAD), applies the given # stash (defaults to last) and finishes the rebase.