Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vscode && git #17

Closed
GeekaholicLin opened this issue Dec 12, 2017 · 1 comment
Closed

vscode && git #17

GeekaholicLin opened this issue Dec 12, 2017 · 1 comment

Comments

@GeekaholicLin
Copy link
Owner

GeekaholicLin commented Dec 12, 2017

问题来源和解决方案

被大佬安利了许久的vscode,在前几天终于想尝试着换掉webstorm,用上vscode。vscode确实很强大,强大之处就不列举了,不是今天需要讲的,我们先略过。

在安装好常见的拓展,并且将快捷键设置为自己在webstorm常用的之后,发觉有一些特性并不可以使用。特别是source control部分,再加上前几天一直郁闷的bug(gitlens一直无法工作),现在终于定位到了问题所在!

重点:先说解决方案,下载git-for-windows,然后安装,在vscode的用户设置中,将git.path指向git-for-windows安装目录中的git.exe。

来龙去脉

最开始的问题来自于,旧版本的代码中有这么一段代码:

function findGitWin32(): Promise<IGit> {
	return findSystemGitWin32(process.env['ProgramW6432'])
		.then(void 0, () => findSystemGitWin32(process.env['ProgramFiles(x86)']))
		.then(void 0, () => findSystemGitWin32(process.env['ProgramFiles']))
		.then(void 0, () => findSpecificGit('git'));
}

When git is not installed, that code will trickle down to which will simply do cp.spawn('git'...).
当git没有安装在上述的两个目录中的时候,会执行到findSpecificGit('git'),这个代码会进行执行cp.spawn('git'...),这个会使得node.js遍历PATH中的路径,寻找git相关的命令,而这会导致性能问题,关于问题的描述和相关官方人员的解答看下面的链接:

https://github.com/Microsoft/vscode/issues/32739#issuecomment-341177471

并且在内测版的时候,对vscode的代码进行了一定的修改,让Windows系统中的vscode不再去spawn git,从而不会有crash相关的性能问题。

https://github.com/Microsoft/vscode/commit/fe20886713798fe2acbd8946350edfaa52ac4e5c#diff-e64cb2761ab71b6db409f7e3fb804b14L126

让我们看看目前(2017.12.12)为止,源代码都做了一些什么。代码很长,而且个人不是很懂ts,所以,直接在代码中搜索find,稍微理了一下相关的思路。

有几个相关的方法

  function findSpecificGit(path: string, onLookup: (path: string) => void): Promise<IGit>{}
  function findGitDarwin(onLookup: (path: string) => void): Promise<IGit>{}
  function findSystemGitWin32(base: string, onLookup: (path: string) => void): Promise<IGit>{}
  function findGitWin32(onLookup: (path: string) => void): Promise<IGit>{}
  function findGit(hint: string | undefined, onLookup: (path: string) => void): Promise<IGit>{}

可以知道,findGit是父函数(export可看出。其实也可以在该repo中搜索findGit,发现在同目录的man.ts调用了findGit)。

const pathHint = workspace.getConfiguration('git').get<string>('path');
const info = await findGit(pathHint, path => outputChannel.appendLine(localize('looking', "Looking for git in: {0}", path)));

其余为子函数。可以发现这些函数的签名都有onLookup,结合在main.ts调用的findGit实参和形参命名来看,onLookup是寻找git过程中的一个回调方法,而这些方法也都是返回的Promise对象。

从findGit的调用开始看起,可以发现和我们的用户配置(settings.json)文件相关(git.path)。如果git.path没有配置,则会根据process.platform执行相关的方法,比如在windows中会执行findGitWin32(onLookup),会执行findSystemGitWin32查找系统的git.exe(ProgramW6432=C:\ProgramFiles)。

//环境变量的解释见:https://stackoverflow.com/questions/17688758/using-programfilesx86-on-windows-os-32bit
{
   ProgramFiles: 'C:\\Program Files (x86)',
  'ProgramFiles(x86)': 'C:\\Program Files (x86)',
  ProgramW6432: 'C:\\Program Files',
}

综合来看,vscode默认windows系统常见的git路径有这2-3个(在上述路径中拼接上Git/cmd/git.exe
(不知道是出于什么考虑,估计是常见的软件?原本是有findGitHubGitWin32的,但是由于一些bug,代码中移除了,所以基于这个考虑),如果找不到,则会抛出not found异常。
下面的第一张图可以看出,注释掉设置中的git.path,虽然有输出Git的信息,但是左边并没有活动的源代码管理程序。这是因为输出用的是我MSYS2中的git的(PATH中有该路径,并且在windows git之前),因为git的version是2.5.0,而左边的sidebar是使用的上面所说的查找路径的git。可以见图二的git version比较
图一

图二

而当我们把用户设置中的git移除并重新加载vscode的时候,可以发现,果然生效了!!!
image

可能会有人问,在PATH中加入git的执行路径还是不可以吗?
PATH中的只会影响输出,在新版本中并不会被vscode的git插件使用。在旧版本确实是可以使用的,至于为什么新旧版本不同,原因上面讲过了---就是spawn查找时间过长导致性能问题,vscode换了另外一种方式

两全其美?

虽然知道可以用git.path指向git-for-windows的git执行路径即可解决这个问题。但是又有了新的问题。

比如说我,在Windows中使用MSYS2和vscode,当然希望两者尽量得结合,而尽可能不再安装其他的软件,因为MSYS2中都已经有Git了,命令行那么好用,我再去找安装包那不是没事找事吗。

所以我们来尝试着使用git.path指向MSYS2中的git.exe。不好意思,并不可以。会一直报下面的错误。至于解决办法我目前也无法知道,如果有解决办法希望指出。所以git for windows在目前而言是必不可少的。两全其美至少在现在是,不存在的。

fatal: Not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
@GeekaholicLin GeekaholicLin changed the title 深入研究windows中vscode v18.1新版本中git新特性等无法使用的问题 vscode && git Dec 12, 2017
@xjtunk
Copy link

xjtunk commented Jul 26, 2018

看来只能安装Git for windows了

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

No branches or pull requests

2 participants