繁体   English   中英

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

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

我无法弄清楚如何将WSL与VS Code集成。 我可以使用以下方式打开集成终端:

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

集成终端工作。 但是,我不能使用源代码控制或VS Code的任何linting功能。 在源控制菜单中,它显示“没有活动的源控制提供程序。”。

问题可能是由git的路径引起的,但我无法弄清楚如何解决问题。 我将不胜感激任何帮助。 谢谢。

根据这篇文章,你必须编写一个批处理文件

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

并告诉VsCode git插件来定位这个bat文件。 (终端设置为像你一样使用bash)

您可以为所有短头/嗅探器/助手插件执行此操作。

希望这可以帮助......并且工作;-)

这些选项都不适用于我,所以我建立了自己的选项!

你可以在这里找到我的完整解决方案: https//gist.github.com/onecrayon/de756538d5331f7592065507c03b1864

简而言之:您需要通过批处理文件进行代理(如pitrackster所建议的那样 ),但是当与VSC一起使用时,它们的批处理文件将会失败,因为它无法正确地转义代理命令,无法将路径转换为Windows和Unix。


对于后代,这是我在上面发布时上面链接的脚本,没有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'

您需要在主机操作系统(Windows)上安装Git,因为VS Code从cmd调用git,而不是集成终端。

这个问题的解决方案是为Windows安装git。 GitHub桌面是一个不错的选择。

暂无
暂无

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

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