我们的开发人员混合使用Windows和基于unix的操作系统。因此,在Unix机器上创建的符号链接成为Windows开发人员的一个问题。在Windows (MSysGit)中,符号链接被转换为一个文本文件,并带有它所指向的文件的路径。相反,我想将符号链接转换为实际的Windows符号链接。

我对此的(更新的)解决方案是:

编写一个检出后脚本,递归地查找“符号链接”文本文件。 将它们替换为Windows符号链接(使用mklink),具有与虚拟“symbolic link”相同的名称和扩展名 通过在文件.git/info/exclude中添加一个条目来忽略这些Windows符号链接

我还没有实现这个方法,但我相信这是解决这个问题的可靠方法。

如果有的话,你认为这种方法有什么缺点? 这个后签出脚本是可实现的吗?也就是说,我能递归地找到Git创建的虚拟“符号链接”文件吗?


当前回答

我建议您不要在存储库中使用符号链接。将实际内容存储在存储库中,然后在存储库外放置指向内容的符号链接。

因此,假设您正在使用一个存储库来比较在类unix系统上托管站点与在Windows上托管站点。将内容存储在您的存储库中,例如/httpRepoContent和c:\httpRepoContent,这是通过Git、SVN等同步的文件夹。

然后,替换你的web服务器的内容文件夹(/var/www和c:\程序文件\web server\www{名称并不重要,如果你必须编辑}),用一个符号链接到你的存储库中的内容。web服务器将看到内容实际上是在“正确”的位置,但你可以使用你的源代码控制。

但是,如果您需要在存储库中使用符号链接,您将需要研究一些类似于前/后提交脚本的东西。我知道您可以使用它们来做一些事情,例如通过格式化程序解析代码文件,因此应该可以在平台之间转换符号链接。

如果有人知道一个好地方可以学习如何为常见的源代码控制、SVN、Git和MG执行这些脚本,那么请添加评论。

其他回答

您可以通过查找模式为120000的文件来查找符号链接,可能使用以下命令:

git ls-files -s | awk '/120000/{print $4}'

一旦你替换了链接,我建议使用git update-index——assume-unchanged将它们标记为不变,而不是将它们列在.git/info/exclude中。

它应该在MSysGit中实现,但是有两个缺点:

符号链接只在Windows Vista及以后版本中可用(在2011年应该不是问题,但它确实是…),因为旧版本只支持目录连接。 微软认为符号链接存在安全风险,因此默认情况下只有管理员可以创建符号链接。您需要提升Git进程的特权,或者使用fstool在您工作的每台机器上更改此行为。

我做了一个快速搜索,这方面的工作正在积极进行;见第224期。

简单的回答:如果您可以启用开发人员模式,它们现在得到了很好的支持。

来自Windows 10的Symlinks !:

Now in Windows 10 Creators Update, a user (with admin rights) can first enable Developer Mode, and then any user on the machine can run the mklink command without elevating a command-line console. What drove this change? The availability and use of symlinks is a big deal to modern developers: Many popular development tools like git and package managers like npm recognize and persist symlinks when creating repos or packages, respectively. When those repos or packages are then restored elsewhere, the symlinks are also restored, ensuring disk space (and the user’s time) isn’t wasted.

“创建者更新”的所有其他公告很容易被忽略,但如果启用了开发人员模式,则可以在没有提升权限的情况下创建符号链接。您可能必须重新安装Git并确保启用了符号链接支持,因为默认情况下并没有启用符号链接支持。

我刚刚尝试了Git 2.30.0(发布2020-12-28)。

这并不是一个完整的答案,但还是有一些有用的花边新闻。(你可以随意做出自己的回答。)

Git Wiki入口

在安装Git for Windows时,有一个文档链接

这个链接把你带到这里:https://github.com/git-for-windows/git/wiki/Symbolic-Links——这是一个相当长的讨论。

供你参考:至少有三种“链接”。只是为了强调这个维基条目的一个重要方面:我不知道这一点,但有几种方法在表面上都是“某种”符号链接,但在技术层面上是非常不同的:

git bash's "ln -s" Which just copies things. Oh, boy. That was unexpected to me. (FYI: Plain Cygwin does not do this. Mobaxterm does not do this. Instead they both create something that their stat command actually recognizes as "symbolic link".) cmd.exe's builtin "mklink" command with the "/D" parameter Which creates a directory symbolic link. (See the Microsoft documentation) cmd.exe's builtin "mklink" command with the "/J" parameter. Which creates a directory junction AKA soft link AKA reparse point. (See the Microsoft documentation.)

发行说明条目

此外,在发布说明中还会不断弹出符号链接。截至2.30.0,这里仍然被列为“已知问题”:

在1703年以前的Windows 10上,或者当开发人员模式被关闭时,克隆带有符号链接的存储库时需要特殊权限,因此默认情况下禁用对符号链接的支持。使用git clone -c core。symlinks=true <URL>来启用它,参见这里的详细信息。

下面是一个PowerShell脚本,用Windows替换Unix符号链接。

# This fixes permission denied errors you might get when
# there are Git symbolic links being used on repositories that
# you share in both POSIX (usually the host) and Windows (VM).
#
# This is not an issue if you are checking out the same
# repository separately in each platform. This is only an issue
# when it's the same working set (AKA make a change without
# committing on OS X, go to Windows VM and Git status would show
# you that change).
#
# Based on this answer on Stack Overflow: http://stackoverflow.com/a/5930443/18475
#
# No warranties. Good luck.
#
# NOTE: It must be run in elevated PowerShell

$ROOT = $PWD

$symlinks = &git ls-files -s | gawk '/120000/{print $4}'
foreach ($symlink in $symlinks) {
  $content = &Get-Content $symlink
  $content = $content.Replace("/", "\")
  $filename = $symlink.Split("/")[-1]
  cd (dirname $symlink)
  rm $filename
  echo Linking $content -> $filename
  New-Item -ItemType SymbolicLink -Path $filename -Target $content
  &git update-index --assume-unchanged $symlink
  cd $ROOT
}