diff --git a/README.md b/README.md index 3d35ee3..bed10ef 100644 --- a/README.md +++ b/README.md @@ -318,6 +318,7 @@ source /path/to/git-wt/aliases/git-wt.sh | `wto` | `cd $(git wt origin)` | cd into the origin (main) repo | | `wtn [name]` | `git wt new` + `cd` | Create worktree and cd into it | | `wtco ` | `git wt checkout` + `cd` | Checkout existing branch and cd into it | +| `wtbye` | `git wt rm` + `cd origin` | Remove current worktree and cd to origin | | `wtls` | `git wt list` | List worktrees | | `wtla` | `git wt list-all` | List all worktrees | | `wtrm ` | `git wt rm` | Remove a worktree | diff --git a/aliases/git-wt.sh b/aliases/git-wt.sh index 0123112..e2d38b2 100644 --- a/aliases/git-wt.sh +++ b/aliases/git-wt.sh @@ -53,6 +53,37 @@ wtco() { [[ -n "${wt_path:-}" ]] && cd "$wt_path" || return 1 } +# Remove current worktree and cd back to origin +# wtbye +wtbye() { + local origin + origin=$(git wt origin 2>/dev/null) || { echo "error: not in a git repo" >&2; return 1; } + + local cwd + cwd=$(pwd -P) + + if [[ "$cwd" == "$origin" || "$cwd" == "$origin/"* ]]; then + echo "error: already in origin repo, not in a worktree" >&2 + return 1 + fi + + local wt_home="${GIT_WT_HOME:-${HOME}/.git-wt}" + local repo_name + repo_name=$(basename "$origin") + local wt_root="${wt_home}/${repo_name}" + + if [[ "$cwd" != "${wt_root}/"* ]]; then + echo "error: not in a git-wt managed worktree" >&2 + return 1 + fi + + local wt_name="${cwd#"${wt_root}/"}" + wt_name="${wt_name%%/*}" + + cd "$origin" || return 1 + git wt rm "$wt_name" +} + # List worktrees (current repo) alias wtls='git wt list'