Skip to content

Instantly share code, notes, and snippets.

@mterrel
Created July 3, 2018 21:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mterrel/0effeb433213e5b014324c34d43a33bc to your computer and use it in GitHub Desktop.
Save mterrel/0effeb433213e5b014324c34d43a33bc to your computer and use it in GitHub Desktop.
Use WSL git with VS Code
# Run WSL git from Windows, mapping some paths between unix <-> Win
# To use with wslgit.cmd, place them in the same directory
Function ToUnix {
# If the arg has a \, then it's probably a path. Convert it.
if ($args[0] -match "\\") {
$mapped = wsl wslpath -u `'$($args[0])`'
} else {
$mapped = $args[0]
}
# Add single quotes around each arg for bash
"`'$mapped`'"
}
# Convert each arg with ToUnix
$mappedargs = $args | % { ToUnix $_ }
$out = wsl git $mappedargs
$gitExit = $LASTEXITCODE
# Mapping paths in the output is extremely difficult to get right
# in the general case. Luckily, VS Code seems happy with most of the
# output paths in unix format. However, it does depend on some
# responses mapping to Windows paths. Transforming single-word
# responses that look like paths seems to be sufficient for most
# common VS Code operations.
if ($out -is [string]) {
$words = -split $out
# Only map single words that have a /, but aren't a ref (which also has slashes)
if (($words.Length -eq 1) -and ($words[0] -match "/") -and !($words[0] -match "^refs/")) {
$out = wsl wslpath -w $words[0]
}
}
$out
exit $gitExit
{
"git.path": "C:\\Tools\\wslgit.cmd"
}
@echo off
:: Place in same directory as git.ps1
SET "psgit=%~dp0git.ps1"
PowerShell -NoProfile -ExecutionPolicy Bypass -File "%psgit%" %*
@mattoni
Copy link

mattoni commented Jul 10, 2018

Getting the following error when vscode attempts a commit:

git commit --quiet --allow-empty-message --file - --all
C:\Users\matto\Documents\Development\vscode\git.ps1 : Cannot process argument because the value of argument "name" is
not valid. Change the value of the "name" argument and run the operation again.
+ CategoryInfo : InvalidArgument: (:) [git.ps1], PSArgumentException
+ FullyQualifiedErrorId : Argument,git.ps1

Any ideas? Other than that, it seems to be working.

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