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

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

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

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

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


当前回答

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

来自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并确保启用了符号链接支持,因为默认情况下并没有启用符号链接支持。

其他回答

我一直在寻找一种简单的解决方案来处理Windows上的Unix符号链接。非常感谢您在之前的回答中提供Git别名。

可以对rm-符号链接进行一个小小的优化,以便在别名意外第二次运行时它不会删除目标文件夹中的文件。请观察循环中的新if条件,以确保在逻辑运行之前文件不是到目录的链接。

git config --global alias.rm-symlinks '!__git_rm_symlinks(){
for symlink in $(git ls-files -s | egrep "^120000" | cut -f2); do
    *if [ -d "$symlink" ]; then
      continue
    fi*
    git rm-symlink "$symlink"
    git update-index --assume-unchanged "$symlink"
done
}; __git_rm_symlinksenter

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

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

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

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

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

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

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

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

下面是一个批处理脚本,用于转换存储库中的符号链接,仅用于文件,基于Josh Lee的回答。https://gist.github.com/Quazistax/8daf09080bf54b4c7641上有一个带有管理员权限检查功能的脚本。

@echo off
pushd "%~dp0"
setlocal EnableDelayedExpansion

for /f "tokens=3,*" %%e in ('git ls-files -s ^| findstr /R /C:"^120000"') do (
     call :processFirstLine %%f
)
REM pause
goto :eof

:processFirstLine
@echo.
@echo FILE:    %1

dir "%~f1" | find "<SYMLINK>" >NUL && (
  @echo FILE already is a symlink
  goto :eof
)

for /f "usebackq tokens=*" %%l in ("%~f1") do (
  @echo LINK TO: %%l

  del "%~f1"
  if not !ERRORLEVEL! == 0 (
    @echo FAILED: del
    goto :eof
  )

  setlocal
  call :expandRelative linkto "%1" "%%l"
  mklink "%~f1" "!linkto!"
  endlocal
  if not !ERRORLEVEL! == 0 (
    @echo FAILED: mklink
    @echo reverting deletion...
    git checkout -- "%~f1"
    goto :eof
  )

  git update-index --assume-unchanged "%1"
  if not !ERRORLEVEL! == 0 (
    @echo FAILED: git update-index --assume-unchanged
    goto :eof
  )
  @echo SUCCESS
  goto :eof
)
goto :eof

:: param1 = result variable
:: param2 = reference path from which relative will be resolved
:: param3 = relative path
:expandRelative
  pushd .
  cd "%~dp2"
  set %1=%~f3
  popd
goto :eof

Git SCM的最新版本(在版本2.11.1上测试)允许启用符号链接。但是你必须再次用符号链接克隆存储库git clone -c core。符号链接= true > < URL。该命令需要使用管理员权限执行。也可以在Windows上使用mklink创建符号链接。

看看维基吧。