当我在 Windows 上克隆带有符号链接的存储库时会发生什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11662868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:43:27  来源:igfitidea点击:

What happens when I clone a repository with symlinks on Windows?

windowsgitsymlinkmsysgit

提问by Andres Riofrio

There's been a lot of questions about adding support for symlinks on Windows. But, what actually happens when I clone a repository with symlinkson Windows?

关于在 Windows 上添加对符号链接的支持存在很多问题。但是,当我在 Windows 上克隆带有符号链接的存储库时,实际会发生什么?

采纳答案by sschuberth

Since version 1.5.3of the native Git client git cloneand git initwill probe the target file system for symlink support and set the local repository configuration for core.symlinksaccordingly, i.e. to falsefor FAT or NTFS. This makes symlinks created and committed e.g. under Linux appear as plain text files that contain the link text under Windows(see the git config documentation on core.symlinksfor details).

从本机 Git 客户端的1.5.3 版开始git clonegit init将探测目标文件系统以获取符号链接支持并相应地设置本地存储库配置core.symlinks,即falseFAT 或 NTFS。这使得在 Linux 下创建和提交的符号链接在 Windows 下显示为包含链接文本的纯文本文件(有关详细信息,请参阅core.symlinks 上git 配置文档)。

Since Git for Windows version 2.10.2the installer has an explicit option to enable symbolic link support.

由于Git for Windows 版本 2.10.2,安装程序有一个显式选项来启用符号链接支持

In older versions of Git for Windows you can manually set core.symlinksto truewhich enabled Git to create symbolic links under the following constraints:

在为Windows旧版本的Git,你可以手动设置core.symlinkstrue这使Git的创建在以下限制符号链接:

  • Symbolic links are only available on Windows Vista and later.
  • Symbolic links will only work on NTFS, not on FAT.
  • You need to be an admin and / or have the SeCreateSymbolicLinkPrivilegeprivilege.
  • Symbolic links on remote filesystems are disabled by default.
  • Windows' symbolic links are typed.
  • Many programs do not understand symbolic links (that includes older version of Windows Explorer).
  • 符号链接仅在 Windows Vista 和更高版本上可用。
  • 符号链接仅适用于 NTFS,不适用于 FAT。
  • 您需要是管理员和/或拥有SeCreateSymbolicLinkPrivilege特权。
  • 默认情况下禁用远程文件系统上的符号链接。
  • 键入 Windows 的符号链接。
  • 许多程序不理解符号链接(包括旧版本的 Windows 资源管理器)。

More detailsare available in the Git for Windows wiki.

Git for Windows wiki 中提供了更多详细信息

In older versions of Git for Windows manually setting core.symlinksmanually to trueafter cloning and reset your working tree, you would get error messages similar to

在旧版本的 Windows 版 Git 中core.symlinkstrue在克隆和重置工作树后手动设置为手动设置,您会收到类似于以下内容的错误消息

$ git reset --hard HEAD
error: unable to create symlink directory (Function not implemented)
error: unable to create symlink linux-links/this_is_a_symbolic_link_to_file (Function not implemented)
fatal: Could not reset index file to revision 'HEAD'.

As a side note, the JGit client did not probe the target file system for symlink support until its version 3.3, so the core.symlinkssetting was falling back to whatever the system / global Git configuration was. Starting with version 3.3JGit probes for symlink support but seems to be too conservative, setting core.symlinks = falsein some cases where symlinks would be in fact supported.

附带说明一下,JGit 客户端直到 3.3 版本才探测目标文件系统以支持符号链接,因此该core.symlinks设置回退到系统/全局 Git 配置的任何内容。从3.3 版开始JGit 探测符号链接支持,但似乎过于保守,core.symlinks = false在某些情况下设置实际上支持符号链接

You can checkout https://github.com/sschuberth/git-playgroundwhich contains a bunch of links created on Linux for testing.

您可以查看https://github.com/sschuberth/git-playground,其中包含在 Linux 上创建的一堆用于测试的链接。

回答by VonC

One solution would have a filter in order to detect symlinks stored by Git and replace them with Windows symlink.
That is detailed in "Git Symlinks in Windows"

一种解决方案是使用过滤器来检测 Git 存储的符号链接并将其替换为 Windows 符号链接。
即在“详细Git的符号连接在Windows

However, true symlink support isn't just for now:
See issue 224and the very recent (July 2012) discussion on GitHub(which you looked at):

但是,真正的符号链接支持不仅仅是现在:
请参阅问题 224和最近(2012 年 7 月)在 GitHub 上的讨论(您查看过):

here are three types of file system links on Windows: hardlinks, junctions, and symlinks.

  • Hardlinks and junctions are available since NT. Hardlinks can only point to files, junctions only to directories (on the same volume).
  • The symlinks available since Vista can point to either files or directories, also on different volumes.
  • mklink, which ships since Vista, can create all of the above. But the way it is called in the script makes it only create symlinks (which is good, IMHO, as they most closely resemble Linux symbolic links).

For pre-Vista, we'd need a fallback that either creates hardlinks for files using "fsutil hardlink" (but probably only if "ln" is called without "-s") and creates junctions for directories using "fsutils reparsepoint", or simply calls the original ln.exe.

In addition to breaking Windows XP setups, a change like this will also break standard Windows 7 setups, because mklinkrequires administrator privileges by default. This can be fixed by checking if it worked or not, and reverting to copying in such cases.

Just for the record: I played around a bit with trying to make symlink support in Git for Windows itself recently, but ended up concluding that the "symlink support" in Windows 7 and up is pretty close to useless for emulating Unix symlinks.

以下是 Windows 上三种类型的文件系统链接:硬链接、联结和符号链接。

  • 自 NT 以来,硬链接和连接可用。硬链接只能指向文件,连接只能指向目录(在同一卷上)。
  • 自 Vista 以来可用的符号链接可以指向不同卷上的文件或目录。
  • mklink,自 Vista 开始提供,可以创建上述所有内容。但是在脚本中调用它的方式使它只创建符号链接(这很好,恕我直言,因为它们最类似于 Linux 符号链接)。

对于Vista之前的版本,我们需要一个回退,要么使用“ fsutil hardlink”为文件创建硬链接(但可能只有在没有“ -s”的情况下调用“ln ”)并使用“ fsutils reparsepoint”为目录创建连接,或者简单地调用原始的ln.exe.

除了破坏 Windows XP 设置之外,这样的更改还会破坏标准的 Windows 7 设置,因为mklink默认情况下需要管理员权限。这可以通过检查它是否有效来解决,并在这种情况下恢复到复制。

只是为了记录:我最近尝试在 Git 中为 Windows 本身提供符号链接支持,但最终得出结论认为,Windows 7 及更高版本中的“符号链接支持”对于模拟 Unix 符号链接几乎毫无用处。

There is a project claiming "Open Source, 100% Compatible ln for Windows (and Junction Point library)", but:

有一个项目声称“开源,100% 兼容 Windows(和连接点库)”,但是:

Unfortunately, normal users doesn't have the required permissions to create symlinks by default on Windows. Combine this with the fact that you cannot change what a symlink points to in the same way that POSIX requires, makes them more or less useless for us. This I already wrote above.

Now, the authors can claim all they want that this is "100% compatible" for all I care, but a quick look at the source code reveals that it's not. They do not provide any fallbacks, they don't even load the CreateSymbolicLinkfunction dynamically. So the result on non-symlink capable Windows versions will be a crash with a missing symbol error.

不幸的是,默认情况下,普通用户没有在 Windows 上创建符号链接所需的权限。将此与您无法以 POSIX 要求的相同方式更改符号链接指向的事实相结合,使它们或多或少对我们无用。这个我上面已经写了。

现在,作者可以声称这是“100% 兼容”的所有我所关心的,但是快速查看源代码会发现它不是。它们不提供任何回退,甚至不CreateSymbolicLink动态加载函数。因此,不支持符号链接的 Windows 版本的结果将是由于缺少符号错误而导致的崩溃。

回答by frogonwheels

I have been working on Symlink support in msysgit here:

我一直在这里研究 msysgit 中的符号链接支持:

https://github.com/frogonwheels/git(branch mrg/symlink-v* .. currently v2)

https://github.com/frogonwheels/git(分支 mrg/symlink-v* .. 目前为 v2)

The tests do notrun to completion yet, and I have limited time in which to work on it, and no real short-term goal to motivate me. It'd be nice to be able to use projects like git-annex under msysgit.

测试还没有完成,我的工作时间有限,也没有真正的短期目标来激励我。能够在 msysgit 下使用像 git-annex 这样的项目会很好。

My work is hampered by the lack of symlink support in the msys shell as well.

我的工作也受到 msys shell 中缺乏符号链接支持的阻碍。

There's a command-line for granting the privileges that is suggested by the cygwin ln command. (You will need to run this as administrator).

有一个命令行用于授予 cygwin ln 命令建议的权限。(您需要以管理员身份运行它)。

editrights -a SeCreateSymbolicLinkPrivilege -a $YOUR_USER

编辑权限 -a SeCreateSymbolicLinkPrivilege -a $YOUR_USER

The whole problem of Directory vs file symlinks is a biggie.

目录与文件符号链接的整个问题是一个大问题。

At the moment I'm of the opinion that as much as we can, we limit ourselves to making File symlinks work... and not allow directory symlinks in msysgit. It's not ideal, but the reality is that any solution is a bit of a cludge, trying to impose possix linking onto the realities of the NTFS incompatibilities with possix linking is just painful.

目前我认为,我们尽可能多地限制自己使文件符号链接工作......并且不允许在 msysgit 中使用目录符号链接。这并不理想,但现实情况是,任何解决方案都有些麻烦,试图将 possix 链接强加到 NTFS 与 possix 链接不兼容的现实中是很痛苦的。

We can try and detect whether the target is file or directory, but I can think of a few issues with that just off the top of my head - especially the whole problem of which order the entities get created.

我们可以尝试检测目标是文件还是目录,但我可以想到一些问题,尤其是实体创建顺序的整个问题。