Skip to content

Instantly share code, notes, and snippets.

@aiya000
Last active August 24, 2021 06:55
Show Gist options
  • Save aiya000/ec9d2fb7daa8921a23e1519730aa4da2 to your computer and use it in GitHub Desktop.
Save aiya000/ec9d2fb7daa8921a23e1519730aa4da2 to your computer and use it in GitHub Desktop.
#!/bin/bash
DEBUG=0
: git_bridge_wsl2_and_windows
: :
: Currently on NTFS, WSL2\'s git is too heavy.
: This is a workaround for that.
: :
: Recommend: ln -s git_bridge_wsl2_and_windows ~/bin/git
sub_cmd=$1
function main () {
if [[ $(is_needing_auth) = true ]] ; then
git_wsl2 "$@"
elif [[ $(should_use_windows_git) = true ]] ; then
git_windows "$@"
else
# By default
git_wsl2 "$@"
fi
}
function git_wsl2 () {
echo_debug 'Using WSL2 git'
echo_debug "is_needing_auth: $(is_needing_auth)"
echo_debug "should_use_windows_git: $(should_use_windows_git)"
echo_debug "/usr/bin/git $*"
/usr/bin/git "$@"
}
function git_windows () {
local args=()
echo_debug 'Using Windows git'
echo_debug "is_needing_auth: $(is_needing_auth)"
echo_debug "should_use_windows_git: $(should_use_windows_git)"
echo_debug "git.exe $*"
for x in "$@"; do
if [[ $x =~ ^/ ]] ; then
# TODO: Remove this disabling
# shellcheck disable=SC2207
args+=($(wslpath -w "$x" | sed -r 's/\\/\\\\/g' ))
continue
fi
args+=("$x")
done
eval "git.exe ${args[*]}"
}
function should_use_windows_git () {
if \
[[ $PWD =~ ^/mnt ]] \
|| [[ $PWD =~ ^/home/$USER/Windows ]] \
|| [[ $PWD =~ ^/home/$USER/Desktop ]] ; \
then
echo true
else
echo false
fi
}
function is_needing_auth () {
local command_needs_auth=(push pull fetch) cmd
for cmd in "${command_needs_auth[@]}" ; do
if [[ $sub_cmd = "$cmd" ]] ; then
echo true
return
fi
done
echo false
}
function echo_debug () {
if [[ $DEBUG -eq 1 ]] ; then
echo "git_bridge_wsl2_and_windows: $*"
fi
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment