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

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

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

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

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


当前回答

下面是一个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
}

其他回答

对于那些在Windows Vista、Windows 7或更高版本上使用Cygwin的人来说,原生git命令可以创建“适当的”符号链接,这些符号链接可以被Windows应用程序(如Android Studio)识别。你只需要设置CYGWIN环境变量来包含winsymlinks:native或winsymlinks:nativestrict,就像这样:

export CYGWIN="$CYGWIN winsymlinks:native"

这样做的缺点(也是一个很重要的缺点)是Cygwin shell必须是“以管理员身份运行”,这样它才能拥有创建这些符号链接所需的操作系统权限。但是,一旦创建了它们,就不需要特殊的权限来使用它们。只要它们没有在存储库中被另一个开发人员更改,git就可以在正常的用户权限下正常运行。

就我个人而言,我只对Windows应用程序(即非cygwin)导航的符号链接使用这种方法,因为这增加了难度。

有关此选项的更多信息,请参阅堆栈溢出问题:如何在Windows 7中使用Cygwin创建符号链接

我一直在文档根目录和Git存储库目录之间使用符号链接。我喜欢把它们分开。在Windows上,我使用mklink /j选项。这个连接似乎可以让Git正常运行:

>mklink /j <链路>的位置(路径)<链路>的来源

例如:

>梅子/j . c

我们使用的一个简单的技巧是调用git add——连续两次。

例如,我们的Windows 7提交脚本调用:

git add --all
git add --all

第一个add将链接视为文本,并添加用于删除的文件夹。

第二个add正确地遍历链接,并通过恢复文件来撤销删除操作。

它没有其他一些提议的解决方案那么优雅,但它是对添加了符号链接的一些遗留环境的简单修复。

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

看看维基吧。

下面是一个批处理脚本,用于转换存储库中的符号链接,仅用于文件,基于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