Skip to content

Instantly share code, notes, and snippets.

@victor-perez
Last active August 4, 2021 14:16
Show Gist options
  • Star 51 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save victor-perez/8ba25540394ec68b11f4b2699fb4d4ce to your computer and use it in GitHub Desktop.
Save victor-perez/8ba25540394ec68b11f4b2699fb4d4ce to your computer and use it in GitHub Desktop.
Use WSL git inside VS Code from Windows 10 17046
@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
::this also support calls that contains a absolute windows path
::check of one of the params contain a absolute windows path
echo.%* | findstr /r /c:"[a-z]:[\\/]" > nul
if %errorlevel% == 1 (
::if not just git with the given parameters
call :git %*
exit /b
)
::loop though the params and replace the path with a wslpath
:param_loop
set param_check=%1
if defined param_check (
call :get_wslpath %param_check% R_PATH
if defined params (
set "params=%params% !R_PATH!"
) else (
set "params=!R_PATH!"
)
) else (
goto :param_continue
)
shift
goto :param_loop
:param_continue
::last call git with the new params
call :git %params%
exit /b
::git label
:git
set params=
::first try the call with wslpath
::needed for calls that return a wslpath like: git rev-parse --show-toplevel
wsl wslpath -w $(git %*) 2> nul
if not %errorlevel% == 0 (
::if the call didn't return a wslpath try again without wslpath
wsl git %*
)
exit /b
::get wslpath label
:get_wslpath
set wslpath_param=%1
::check of current param has windows path
echo %wslpath_param% | findstr /r /b /c:"[a-z]:[\\/]" > nul
if %errorlevel% == 0 (
::get wslpath
for /f "tokens=* USEBACKQ" %%F IN (`wsl wslpath "%wslpath_param%"`) do (
set wslpath_result=%%F
)
) else (
set wslpath_result=%wslpath_param%
)
set %2=%wslpath_result%
exit /b
@echo on
{
"git.path": "C:\\Tools\\git.bat"
}
@mike-zipit
Copy link

mike-zipit commented Nov 19, 2018

👍

@kavdev
Copy link

kavdev commented Nov 19, 2018

@victor-perez Thanks! Works great, except for some commands that seem to help with sidebar rendering:

Output from wslgit.exe

Looking for git in: C:\Users\me\projects\wslgit.exe
Using git 2.18.0 from C:\Users\me\projects\wslgit.exe
> git rev-parse --show-toplevel
> git config --get commit.template
Open repository: c:\Users\me\projects\demo_repo
> git status -z -u
> git show :Pipfile
> git check-ignore -z --stdin
> git symbolic-ref --short HEAD
> git rev-parse development
> git rev-parse --symbolic-full-name development@{u}
> git rev-list --left-right development...refs/remotes/origin/development
> git for-each-ref --format %(refname) %(objectname) --sort -committerdate
> git remote --verbose

Output from your git.bat

Looking for git in: C:\Users\me\projects\git.bat
Using git Usage:
    -a    force result to absolute path format
    -u    translate from a Windows path to a WSL path (default)
    -w    translate from a WSL path to a Windows path
    -m    translate from a WSL path to a Windows path, with '/' instead of '\'

EX: wslpath 'c:\users'
git version 2.18.0 from C:\Users\me\projects\git.bat
> git rev-parse --show-toplevel
> git config --get commit.template
Open repository: c:\Users\me\projects\demo_repo
> git status -z -u
> git rev-parse --show-toplevel
> git check-ignore -z --stdin
fatal: not a git repository (or any parent up to mount point /mnt)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
> git config --get commit.template
Open repository: \Usage:
    -a    force result to absolute path format
    -u    translate from a Windows path to a WSL path (default)
    -w    translate from a WSL path to a Windows path
    -m    translate from a WSL path to a Windows path, with '\' instead of '\'

EX: wslpath 'c:\users'
> git status -z -u
> git check-ignore -z --stdin
> git symbolic-ref --short HEAD
> git rev-parse development
> git rev-parse --symbolic-full-name development@{u}
> git for-each-ref --format %(refname) %(objectname) --sort -committerdate
> git remote --verbose

@jasamour
Copy link

You are very close. I am getting this error when I try to undo the last commit from within VS Code Source Control

> git show -s --format=%H
%P
%B HEAD
fatal: invalid --pretty format: H

I am assuming this might be because of the FOR /F loop command you have. Not sure though.

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