Skip to content

Avoid creating a new shell instance when using wt #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Git worktree switcher:zap:
# Git worktree switcher :zap:
Switch between git worktrees with speed. :zap:

<img src = "https://i.imgur.com/nPdneDT.gif" width="600" alt="demo of switching between git worktrees" />
Expand All @@ -25,6 +25,11 @@ $ sudo cp wt /usr/local/bin
sudo cp completions/wt_completion /etc/bash_completion.d
```

Add this to the end of your `~/.bashrc`:
```bash
eval "$(command wt init bash)"
```

**For ZSH**
> Note: completion file for zsh has `_` prefix

Expand All @@ -42,10 +47,21 @@ sudo cp completions/_wt_completion <one-of-$fpath>
exec zsh
```

Add this to the end of your `~/.zshrc`:
```bash
eval "$(command wt init zsh)"
```

**For Fish**
```bash
cp completions/wt.fish ~/.config/fish/completions
```

Add this to the end of your `~/.config/fish/config.fish`:
```bash
command wt init fish | source
```

---
Tab autocompletion works for switching between your worktrees.
```bash
Expand Down
73 changes: 61 additions & 12 deletions wt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ set -e
args=("$@")
VERSION="0.1.1"
TMP_PATH=$(mktemp)
BINARY_PATH=$(which wt)
BINARY_PATH=$(realpath "$0")
JQ_URL="https://stedolan.github.io/jq/download"
RELEASE_URL="https://github.com/yankeexe/git-worktree-switcher/releases/latest"
RELEASE_API_URL="https://api.github.com/repos/yankeexe/git-worktree-switcher/releases/latest"
CHANGE_DIRECTORY_PREFIX="changedir:"


# Escape forward slash
Expand All @@ -27,6 +28,7 @@ help_message() {
echo -e "\twt list: list out all the git worktrees."
echo -e "\twt update: update to the latest release of worktree switcher."
echo -e "\twt version: show the CLI version."
echo -e "\twt init <shell>: print the init script for <shell>."
echo -e "\twt help: shows this help message."
}

Expand All @@ -36,9 +38,7 @@ goto_main_worktree() {
if [ -z "$main_worktree" ]; then
:
else
echo Changing to main worktree at: "$main_worktree"
cd "$main_worktree"
exec $SHELL
directory=$main_worktree
fi
}

Expand Down Expand Up @@ -77,6 +77,59 @@ update() {
fi
}

get_dest_path="\$(echo \"\$result\" | awk '/^$CHANGE_DIRECTORY_PREFIX.*/ {sub(\"$CHANGE_DIRECTORY_PREFIX\", \"\"); print; exit}')"

init_bash() {
cat <<EOF
wt() {
result="\$($BINARY_PATH "\$@")"
dest_path="$get_dest_path"

if [[ -n "\$dest_path" ]]; then
cd "\$dest_path" || return
elif [[ -n \$result ]]; then
echo "\$result"
fi
}
EOF
}

init_fish() {
cat <<EOF
function wt
set -l result "\$($BINARY_PATH \$argv)"
set -l dest_path "$get_dest_path"

if test -n "\$dest_path"
cd "\$dest_path"
else if test -n "\$result"
echo "\$result"
end
end
EOF
}

init() {
shell="${args[1]}"
if [[ -z $shell ]]; then
echo "Please supply a shell."
echo " eg. wt init bash"
exit 1
fi
case "$shell" in
bash|zsh)
init_bash
;;
fish)
init_fish
;;
*)
echo "Unsupported shell: $shell"
exit 1
;;
esac
}

if [ -z "${args[0]}" ]; then
help_message
exit 0
Expand All @@ -95,6 +148,9 @@ help)
version)
echo Version: $VERSION
;;
init)
init
;;
-)
goto_main_worktree
;;
Expand All @@ -103,16 +159,9 @@ version)
;;
esac

# Change worktree based on user argument.
change_worktree() {
echo Changing to worktree at: "$directory"
cd "$directory"
exec $SHELL
}

# If directory variable is not empty then change worktree
if [ -z "$directory" ]; then
:
else
change_worktree
echo "$CHANGE_DIRECTORY_PREFIX$directory"
fi