Skip to content

Instantly share code, notes, and snippets.

@carlolars
Last active February 10, 2023 21:08
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save carlolars/ed976078a66e7d57bb04f959270bdee3 to your computer and use it in GitHub Desktop.
Save carlolars/ed976078a66e7d57bb04f959270bdee3 to your computer and use it in GitHub Desktop.
Use bash from WSL as sh.exe for GitKraken (5.0.4) for Windows
@echo off
REM Make sure that the path to the script is correct!
@bash -l -c "~/bin/gitkraken-wsl-bash.sh %*"
#!/bin/bash
# Mount root for windows drives, default is /mnt/ but can be changed in /etc/wsl.conf
MOUNT_ROOT=/mnt/
# Read GitKraken's hook script and arguments from STDIN
read -r GITKRAKEN_CMD
# Cleanup the path to the script:
# - Replace two or more '\' in a row with a single '/'.
# - Replace the windows drive letter (C:) with the mount point for the drive (/mnt/c)
FIXED_CMD=$(echo $GITKRAKEN_CMD | sed 's_\\\{2,\}_/_g' | sed "s_\([A-Z]\)\:/_$MOUNT_ROOT\L\1/_g")
# Run the hook script
bash -c "$FIXED_CMD"
  1. Copy gitkraken-wsl-bash.bat and gitkraken-wsl-bash.sh, I put them in ~/bin.
  2. Make sure that the script path in the bat-file and MOUNT_ROOT in the sh-file are correct.
  3. Set gitkraken-wsl-bash.bat as 'Path to sh.exe' in GitKraken.
@Ridder90
Copy link

Works! Thanks for sharing!

Is it me or is GitKraken a bit slower right now?

@RBrNx
Copy link

RBrNx commented Mar 16, 2020

Hey @carlolars, thanks for creating this! Do you still use this with GitKraken v6.5.x?

I'm having some trouble with it (partly to do with there being a space in my Windows User Name) but the .sh file doesn't seem to receive the GitKraken Command correctly.

gitkraken-wsl-bash.bat
This is unchanged from yours, except I had to wrap the %* in double quotes to deal with the space in my Windows Username

@echo off
REM Make sure that the path to the script is correct!
@bash -c "~/.bin/gitkraken-wsl-bash.sh "%*""

gitkraken-wsl-bash.sh
Not too much has changed here, added some debug echos, but the main difference is that read doesn't seem to work for me, so I attempted to replace it with "$@".

#!/bin/bash

# Mount root for windows drives, default is /mnt/ but can be changed in /etc/wsl.conf
MOUNT_ROOT=/mnt/

# Read GitKraken's hook script and arguments from STDIN
# read -r GITKRAKEN_CMD

GITKRAKEN_CMD="$@"

echo "CMD:"
echo $GITKRAKEN_CMD

# Cleanup the path to the script:
# - Replace two or more '\' in a row with a single '/'.
# - Replace the windows drive letter (C:) with the mount point for the drive (/mnt/c)
FIXED_CMD=$(echo $GITKRAKEN_CMD | sed 's_\\\{2,\}_/_g' | sed "s_\([A-Z]\)\:/_$MOUNT_ROOT\L\1/_g")

echo "Fixed:"
echo $FIXED_CMD

# Run the hook script
bash -c "$FIXED_CMD"

And this is the output I am currently getting.
image

@carlolars
Copy link
Author

Hi @RBrNx

Sorry but I don't use GitKraken anymore, but if I recall correctly from when I created these scripts the problem was that GitKraken didn't send the hook script and its arguments as arguments to the shell, but it rather created a new shell and then called the hook script with argument by writing to stdin of the new shell process. That is why the read had to be there.

But, since you see -lc in the argument then it might be that they have changed it, since the -c argument expects a string with the command and its arguments. Print the arguments to the .bat-script and see what it gets.

@Ridder90
Copy link

Hi Carlolars, just wondering.... are you using another Windows Git Gui for WSL now? Im still using MoxaXterm with gitkraken running on WSL itself. Not very happy about it haha. Alternatively i use the git plugins VSC provides.

@carlolars
Copy link
Author

Hi @Ridder90, I've been using Fork for some time now. It was a lot faster than kraken back when I switched and it's possible to have it use the git-client from within WSL by using wslgit (currently full support only in the develop branch). They recently started charging $50 for a lifetime license but imo it was definitely worth it.

@Ridder90
Copy link

Ridder90 commented Mar 25, 2020

Wow! Thanks for the tip and your work on the wslgit pull request! This is a major pain killer for me.

PS: the updated version of wslgit also works with GitKraken.

@Niksac
Copy link

Niksac commented Apr 27, 2020

@Ridder90 how did you make wslgit work with GitKraken?

@rlnt
Copy link

rlnt commented Oct 31, 2021

If someone found out how to make wslgit work with GitKraken, please let me know.

@Niksac
Copy link

Niksac commented Oct 31, 2021

This does not work for new versions of gitkraken.

In windows 11 you can just run the Linux version.
https://github.com/microsoft/wslg

On windows 10 I used this
https://opticos.github.io/gwsl/

@rlnt
Copy link

rlnt commented Nov 2, 2021

It actually works fine for me on Windows 11 with latest GitKraken.
I've tried a lot of xservers previously and also tried wslg. It looks promising but still has too many issues. I am going with this workaround for now.

@Niksac
Copy link

Niksac commented Nov 2, 2021

@DAmNRelentless how did you get this to work with later versions of gitkraken?
I thought they used an internal git implementation instead of external commands?

@rlnt
Copy link

rlnt commented Nov 2, 2021

I put both files into the same directory, adjusted the path in the gitkraken-wsl-bash.bat and set my GitKraken path for the sh.exe to this batch file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment