Skip to content

Instantly share code, notes, and snippets.

@jasonboukheir
Last active March 29, 2024 18:12
Show Gist options
  • Save jasonboukheir/3fdab92ece236744528447a4f7f5de00 to your computer and use it in GitHub Desktop.
Save jasonboukheir/3fdab92ece236744528447a4f7f5de00 to your computer and use it in GitHub Desktop.
git when in unix, git.exe when in wsl
#!/bin/sh
if pwd | grep /mnt/c > /dev/null; then
exec git.exe "$@"
else
exec /usr/bin/git "$@"
fi
@MMI
Copy link

MMI commented Mar 4, 2022

First, thanks for all this. You've saved my relationship with zsh and wsl2... however, I did a little measurement and I'm thinking that all this is a bandaid on something that should be avoided.

I have a small firmware project (think a collection of C code that builds with make). If I build using Linux (WSL2 on ext4) then it takes about 26 seconds. The same build on in /mnt/c/path/to/code/on/c/drive takes around 1:30... roughly 3x slower. The same build under Windows build takes 40seconds. The build under Windows, pointing at the WSL2 disk (via \\wsl.localhost\Ubuntu\...) was so incredibly slow that I gave up waiting for it (after 10 minutes!)

The take-away here is that Linux stuff should only point at ext4 (or whatever) and Windows stuff should only look at NTFS. Mixing things is just creates a bunch of annoying problems to solve.

Happy that WSL exists but still waiting for it to be The Answer(tm)

@grumpyjacek
Copy link

@grandemk Your versions were super helpful! With this tweak things seem to be happy thus far (after a short amount of play time):

GIT_WINDOWS="/mnt/c/Program Files/Git/cmd/git.exe"
GIT_LINUX="/usr/bin/git"

case "$(pwd -P)" in
/mnt/?/*)
  case "$@" in
  # Needed to fix prompt, but it breaks things like paging, colours, etc
  rev-parse*)
    # running linux git for rev-parse seems faster, even without translating paths
    exec "$GIT_LINUX" "$@"
    ;;
  *)
    exec "$GIT_WINDOWS" "$@"
    ;;
  esac
  ;;
*)
  exec "$GIT_LINUX" "$@"
  ;;
esac

Hi! Can anyone help me - where exactly this code should go to? To which file? I'm totaly new to Linux/WSL2 and can't faind myself around it :(

@roopjm
Copy link

roopjm commented Sep 28, 2022

It's really helpful in your bashrc file! Which is the file that loads all kinds of settings when bash is first initialized for any instance.

@grumpyjacek
Copy link

@roopjm thank you, I did, but it doesn't seem to work :(
image

@Fleshgrinder
Copy link

Fleshgrinder commented Sep 28, 2022

Having this code in the Bash startup script is not going to be very useful, since you are only going to be able to use it if you are invoking git from within an interactive login shell. What you really want is to overload the git executable that is found in the PATH.

PATH works exactly the same way on Linux as it does on Windows. It contains a list of directories that is searched for executables whenever you enter the name of a program in your terminal. The default path on Linux is something like /usr/local/bin:/usr/bin. What you can see here is that /usr/local/bin is searched before /usr/bin, as such we can place stuff in there to overload other executables.

To know where your current git is installed use command -v git, which should give you /usr/bin/git. Meaning, we can use /usr/local/bin to overload it. So, let us do exactly that.

cat >/tmp/git <<'GIT'
#!/usr/bin/env bash
set -e
if [[ $(pwd -P) =~ ^/mnt/./ ]]
    then exec /mnt/c/Program\ Files/Git/cmd/git.exe "$@"
    else exec /usr/bin/git "$@"
fi
GIT
sudo install /tmp/git /usr/local/bin

That is all, no matter from where you invoke git it will be using your overloaded version going forward. Whenever you really want to us the original just use /usr/bin/git.

You can do exactly the same with many other programs that read things from disk. For instance https://github.com/sharkdp/bat is really awesome, and available cross-platform. Just brew install bat on Linux and choco install bat on Windows and create an overload with exactly the same logic as in the git overload (obviously replacing the paths to the executables), and enjoy a massive speed up.

@MarounMaroun
Copy link

Just use it as an alias. I use fish, I added:

alias git "git.exe"

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