Coder Social home page Coder Social logo

yankeexe / git-worktree-switcher Goto Github PK

View Code? Open in Web Editor NEW
124.0 3.0 19.0 17 KB

Switch between git worktrees with speed. :zap:

License: MIT License

Shell 100.00%
git worktree cli cli-app shell shell-script linux workflow-management workflow-tool bash

git-worktree-switcher's Introduction

Git worktree switcher:zap:

Switch between git worktrees with speed. โšก

demo of switching between git worktrees

Installation

Download the script from the Release section.

Make the script executable.

$ chmod +x wt

Copy the executable to any directory in your $PATH

$ sudo cp wt /usr/local/bin

Tab Autocompletion โšก

For Bash

sudo cp completions/wt_completion /etc/bash_completion.d

For ZSH

Note: completion file for zsh has _ prefix

Find paths where you can store your completion

print -rl -- $fpath

Copy completion script to one of that location and restart the shell.

sudo cp completions/_wt_completion <one-of-$fpath>

exec zsh

For Fish

cp completions/wt.fish ~/.config/fish/completions

Tab autocompletion works for switching between your worktrees.

wt <TAB> <TAB>

# OR

wt <completion-characters> <TAB>

Usage

Switch between worktrees. You can do a text search to change to the worktree directory.

$ wt <worktree-name/search-term>

Go to root worktree directory

$ wt -

List out all the worktrees.

$ wt list

Show help message

$ wt help

Update to the latest release

$ wt update

Show the CLI version

$ wt version

git-worktree-switcher's People

Contributors

kamilgryniewicz avatar koolstr avatar yankeexe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

git-worktree-switcher's Issues

fix shellcheck warnings

In wt line 31:
if [ -z $main_worktree ]; then
^------------^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean:
if [ -z "$main_worktree" ]; then

In wt line 34:
echo Changing to worktree at: $main_worktree
^------------^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean:
echo Changing to worktree at: "$main_worktree"

In wt line 35:
cd $main_worktree
^---------------^ SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.
^------------^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean:
cd "$main_worktree" || exit

In wt line 36:
exec $(echo $SHELL)
^------------^ SC2116: Useless echo? Instead of 'cmd $(echo foo)', just use 'cmd foo'.
^----^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean:
exec $(echo "$SHELL")

In wt line 44:
curl -sL -o $TMP_PATH $download_url
^-------^ SC2086: Double quote to prevent globbing and word splitting.
^-----------^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean:
curl -sL -o "$TMP_PATH" "$download_url"

In wt line 47:
chmod +x $TMP_PATH
^-------^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean:
chmod +x "$TMP_PATH"

In wt line 48:
sudo mv $TMP_PATH $BINARY_PATH
^-------^ SC2086: Double quote to prevent globbing and word splitting.
^----------^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean:
sudo mv "$TMP_PATH" "$BINARY_PATH"

In wt line 49:
rm -f $TMP_PATH
^-------^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean:
rm -f "$TMP_PATH"

In wt line 57:
if [ $fetched_tag_name == $VERSION ]; then
^---------------^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean:
if [ "$fetched_tag_name" == $VERSION ]; then

In wt line 66:
if [ -z $(command -v jq) ]; then
^--------------^ SC2046: Quote this to prevent word splitting.

In wt line 80:
if [ -z ${args[0]} ]; then
^--------^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean:
if [ -z "${args[0]}" ]; then

In wt line 82:
elif [ ${args[0]} == "list" ]; then
^--------^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean:
elif [ "${args[0]}" == "list" ]; then

In wt line 84:
elif [ ${args[0]} == "help" ]; then
^--------^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean:
elif [ "${args[0]}" == "help" ]; then

In wt line 86:
elif [ ${args[0]} == "-" ]; then
^--------^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean:
elif [ "${args[0]}" == "-" ]; then

In wt line 88:
elif [ ${args[0]} == "update" ]; then
^--------^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean:
elif [ "${args[0]}" == "update" ]; then

In wt line 96:
echo Changing to worktree at: $directory
^--------^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean:
echo Changing to worktree at: "$directory"

In wt line 97:
cd $directory
^-----------^ SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.
^--------^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean:
cd "$directory" || exit

In wt line 98:
exec $(echo $SHELL)
^------------^ SC2116: Useless echo? Instead of 'cmd $(echo foo)', just use 'cmd foo'.
^----^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean:
exec $(echo "$SHELL")

In wt line 102:
if [ -z $directory ]; then
^--------^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean:
if [ -z "$directory" ]; then

For more information:
https://www.shellcheck.net/wiki/SC2046 -- Quote this to prevent word splitt...
https://www.shellcheck.net/wiki/SC2164 -- Use 'cd ... || exit' or 'cd ... |...
https://www.shellcheck.net/wiki/SC2086 -- Double quote to prevent globbing ...

wt creates a new shell every time it is run

Soon after starting to use wt, I discovered that it was creating a new shell every time it was run. A better way to cd from a command is to make that command a function, as ranger and wcd do. I created such a function and modified the wt script to write the new directory to a file rather than cd to it itself.

Here is the function, which should be in the user's ~/.bashrc or in /etc/profile.d/wt.sh.

type -f wt &> /dev/null &&
wt()
{
    local dirfile="$HOME/.wt.dir"
    rm -f "$dirfile"
    command wt "$@"
    if [ -f "$dirfile" ]; then
	if [ "$(< "$dirfile")" != "$(pwd)" ]; then
	    cd -- "$(< "$dirfile")"
	fi
    fi
}

I also discovered that wt uses grep with Perl regular expressions, which isn't supported by some distributions' greps. Since wt uses awk already, I just moved the processing that grep was doing inside the awk command and removed grep.

The awk pattern used to find the new worktree allowed the match to occur on lines other than those that began with "worktree", which caused odd errors if the user made an error in the worktree name, so I fixed that, too.

Here is a diff of my changes.

diff --git a/wt b/wt
index 0f79eaa..c76c715 100755
--- a/wt
+++ b/wt
@@ -10,6 +10,7 @@ BINARY_PATH=$(which wt)
 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"
+dirfile="$HOME/.wt.dir"


 # Escape forward slash
@@ -31,14 +32,13 @@ help_message() {
 }

 goto_main_worktree() {
-       main_worktree=$(git worktree list --porcelain | awk '{print $0; exit}' | grep -oP '(?<=worktree ).*')
+       main_worktree=$(git worktree list --porcelain | awk '{sub("worktree ", ""); print; exit}')

        if [ -z "$main_worktree" ]; then
                :
        else
                echo Changing to main worktree at: "$main_worktree"
-               cd "$main_worktree"
-               exec $SHELL
+               echo "$main_worktree" > "$dirfile"
        fi
 }

@@ -99,15 +99,14 @@ version)
        goto_main_worktree
        ;;
 *)
-       directory=$(git worktree list --porcelain | awk '/'"$arg"'/ {print; exit}' | grep -oP '(?<=worktree ).*')
+       directory=$(git worktree list --porcelain | awk '/^worktree .*'"$arg"'/ {sub("worktree ", ""); print; exit}')
        ;;
 esac

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

 # If directory variable is not empty then change worktree

Wt is working great now. Thanks for the script and for the article on using worktrees.

Regards,
Gary

Autocomplete

Love the script!
I would also love if I could autocomplete the work-trees names, so I wouldn't have to do wt list.

Breaks under working paths with spaces

Trying to switch to a worktree fails if even a single folder in the working directory includes a space in its name.

For example, for a worktree located here: /Users/koolstr/Documents/Projects/Example Project/example-tree

The following error is produced when trying wt example or wt -:
Changing to worktree at: /Users/koolstr/Documents/Projects/Example
/usr/local/bin/wt: line 105: cd: /Users/koolstr/Documents/Projects/Example: No such file or directory

split update into separate binary

Mixing install and run in scripts is not necessary, because they do not overlap in functionality.

Also package managers are for installing stuff. Otherwise one wants to have a CI script anyway, so it belongs there.

nested worktrees error

Hey ๐Ÿ‘‹
Great tool you've built, congratz ๐ŸŽ‰

It works great for changing directories across worktrees, however, if I try to change to a nested worktree like feature/something-something I get this error:

image

Is this something in my setup, or is it an error with the script itself not being ready to support / separated wt names ?

Any specific reason for executing a new shell upon changing dir?

Title basically. Executing a new shell doesn't let you easily switch back the directory/worktree with cd - if you'd like to. Also, people like me have a lot of things going on in ~/.profile which take about 2 seconds on each shell execution. Not ideal.

Edit- forgot to add my appreciation for sharing the tool. Thanks heaps!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.