Thanks and credit to mattn and ferreus on GitHub.
Also check out Developing on WSL and/or wslpath
(Windows 10 Build 17046 or later) if you're using the Windows Subsystem for Linux.
#!/bin/bash | |
# wrapper to convert linux paths to windows | |
# so vscode will work as a git editor with cygwin | |
# editor="/home/this/file.sh" in .gitconfig | |
# extract last argument (the file path) | |
for last; do true; done | |
# get all the initial command arguments | |
all="${@:1:$(($#-1))}" | |
# launch editor with windows path | |
code $all $(cygpath -w $last) |
@echo off | |
REM wrapper to convert linux paths to windows | |
REM so vscode git integration will work with cygwin | |
REM "git.path"="C:\\this\\file.bat" in settings.json | |
setlocal | |
set PATH=C:\cygwin\bin;%PATH% | |
if "%1" equ "rev-parse" goto rev_parse | |
git %* | |
goto :eof | |
:rev_parse | |
for /f %%1 in ('git %*') do cygpath -w %%1 |
Actually this line needs to be set to
code $all $(cygpath -w $last)
and also change
export GIT_EDITOR="$HOME/cygpath-git-editor.sh -w"
works for me! thank you!
Thank you for sharing this! I have provided some step-by-step instructions here: https://stackoverflow.com/a/69340165/61624
Emmm...There may be still a small problom for me. In this way, it's display file status correctly. But when using the source manage
in the VSCode Sidebar, it doesn't work. It still prompt "can't spawn git.bat". So sad :(
I tried the bat wrapper, it worked for basic git status display, but trying to stage/unstage a file resulted in errors.
I.e. either of these commands issued by VS Code failed:
Stage: git add -A -- C:\project\hxcalc\package.json
Unstage: git reset -q HEAD -- C:\project\hxcalc\package.json
Both of them result result in fatal: C:\project\hxcalc\package.json: 'C:\project\hxcalc\package.json' is outside repository at '/mnt/c/project/hxcalc'
I tried the same commands in Cygwin bash, and indeed, Cygwin git doesn't seem to be able to handle Windows paths.
What worked for me is this: github.com/nukata/cyg-git. It's a tiny C wrapper that you compile with gcc (for my use case, regular Cygwin gcc worked), and a Zsh wrapper script that works similar to cygpath-git-vscode.bat
, but it also takes care of converting paths passed to git. I had to make a small change to this script to make it handle rev-parse
commands that produce multiple lines of output (made a PR to the original repo).
sadly not working if I try to use this script as
GIT_EDITOR