简体   繁体   English

在Windows上的Ubuntu上使用Git和VS代码以及Bash(WSL)

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

I couldn't figure out how to integrate WSL with VS Code. 我无法弄清楚如何将WSL与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. 但是,我不能使用源代码控制或VS Code的任何linting功能。 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. 问题可能是由git的路径引起的,但我无法弄清楚如何解决问题。 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. 并告诉VsCode git插件来定位这个bat文件。 (With the terminal set to use bash as you did) (终端设置为像你一样使用bash)

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 你可以在这里找到我的完整解决方案: 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. 简而言之:您需要通过批处理文件进行代理(如pitrackster所建议的那样 ),但是当与VSC一起使用时,它们的批处理文件将会失败,因为它无法正确地转义代理命令,无法将路径转换为Windows和Unix。


For posterity, here's the scripts I linked above at the time of this posting, sans ReadMe: 对于后代,这是我在上面发布时上面链接的脚本,没有ReadMe:

git.bat 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 _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 _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. 您需要在主机操作系统(Windows)上安装Git,因为VS Code从cmd调用git,而不是集成终端。

A solution to this issue is to install git for Windows. 这个问题的解决方案是为Windows安装git。 GitHub Desktop is a good option for that. GitHub桌面是一个不错的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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