简体   繁体   中英

Using Git with VS Code and Bash on Ubuntu on Windows (WSL)

I couldn't figure out how to integrate WSL with VS Code. I can open the integrated terminal using:

"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\bash.exe"

The integrated terminal works. However, I can't use source control or any of the linting features of VS Code. At the source control menu, it says "There are no active source control providers.".

The problem is probably caused by the path of git but I couldn't figure out how to solve the problem. I would appreciate any help. Thank you.

According to this article you have to write a batch file

@echo off
bash.exe -c "git %*"

And tell VsCode git plugin to target this bat file. (With the terminal set to use bash as you did)

You can do that for all your linters / sniffers / helpers plugins.

Hope this can help ... and work ;-)

None of these options worked for me, so I built my own!

You can find my full solution here: https://gist.github.com/onecrayon/de756538d5331f7592065507c03b1864

In short: you need to proxy through a batch file (as suggested by pitrackster ), but their batch file will fail when used with VSC because it doesn't properly escape the proxied command and fails to convert paths to and from Windows and Unix.


For posterity, here's the scripts I linked above at the time of this posting, sans ReadMe:

git.bat

@echo off

:: %~dp0 is the directory of this batch file
set proxy_path=%~dp0_proxy_cmd.bat
:: %* is the full command passed to this batch file
%proxy_path% git %*

_proxy_cmd.bat

@echo off

:: Properly escape the command
:: Many thanks to wsl-alias for this logic: https://github.com/leongrdic/wsl-alias
set cmd=%*
set cmd=%cmd:\"=\\"%
set cmd=%cmd:\'=\\'%
set cmd=%cmd:\=/%
set cmd=%cmd://=\\%
set cmd=%cmd:"=\"%
set cmd=%cmd:'=\'%
set cmd=%cmd:(=\(%
set cmd=%cmd:)=\)%

:: Grab the path to our proxy Bash script (%~dp0 is the directory of this batch file)
set bash_proxy=%~dp0_proxy_cmd.sh
set bash_proxy=%bash_proxy:\=\\%

:: Pass things off to the Bash script
bash.exe -c "$(wslpath %bash_proxy%) %cmd%"

_proxy_cmd.sh

##
# Evaluates command, parsing paths at both ends (Windows => Unix => Windows)
#
# Benefits to doing this instead of directly invoking the command in the batch file:
# 
# + Easier to convert path arguments to Unix paths
# + sed regex does not require double escaping backslashes (not embedded in double quotes)
##

cmd=()
for arg in "$@"
do
    if [[ $arg =~ ^[a-zA-Z]:/ ]]; then
        cmd+=( $(wslpath "$arg") )
    else
        cmd+=("$arg")
    fi
done

# TODO: Look into ways to convert inline paths via `wslpath` instead of hard-coding `/mnt` search
# Kind of a tricky issue, because our output could be basically anything
eval "${cmd[@]}" | sed \
    -e 's|"/mnt/\([a-zA-Z]\)/\([^"]*\)"|"\1:/\2"|g' \
    -e "s|'/mnt/\\([a-zA-Z]\\)/\\([^']*\\)'|'\\1:/\\2'|g" \
    -e 's|/mnt/\([A-Za-z]\)/\([^ ]*\)|\1:/\2|g' \
    -e 's|/|\\|g'

You need to have Git installed on the host OS, (Windows) as VS Code calls git from cmd, not the Integrated Terminal.

A solution to this issue is to install git for Windows. GitHub Desktop is a good option for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM