我最好是用一个shell脚本替换符号链接的副本,还是有另一种方式告诉Git遵循符号链接?
PS:我知道这不是很安全,但我只是想在一些特定的情况下这样做。
我最好是用一个shell脚本替换符号链接的副本,还是有另一种方式告诉Git遵循符号链接?
PS:我知道这不是很安全,但我只是想在一些特定的情况下这样做。
当前回答
我使用的是Git 1.5.4.3,如果传递的符号链接后面有斜杠,它会跟着它。如。
# Adds the symlink itself
$ git add symlink
# Follows symlink and adds the denoted directory's contents
$ git add symlink/
其他回答
在macOS上(我有Mojave/ 10.14, git版本2.7.1),使用bindfs。
酿造安装bindfs
cd / path / to / git_controlled_dir
mkdir local_copy_dir
bindfs </full/path/ source_dir> </full/path/ local_dir >
其他评论暗示了这一点,但在其他答案中没有明确提供。希望这能为大家节省一些时间。
@user252400建议的另一种实现是使用bwrap,这是一个小型setuid沙盒,可以在所有主要发行版中找到——通常默认安装。Bwrap允许你无需sudo就可以绑定挂载目录,并在git或shell退出时自动解除绑定。
假设你的开发过程不是很疯狂(见下文),在一个私有的命名空间中启动bash,并在git目录下绑定外部目录:
bwrap --ro-bind / / \
--bind {EXTERNAL-DIR} {MOUNTPOINT-IN-GIT-DIR} \
--dev /dev \
bash
然后做你通常做的所有事情,比如git添加、git提交等等。当你完成时,退出bash。干净简单。
注意:为了防止沙盒转义,bwrap不允许执行其他setuid二进制文件。详见man bwrap。
这是一个预提交钩子,它用这些符号链接的内容替换索引中的符号链接blobs。
把它放在.git/hooks/pre-commit中,并使其可执行:
#!/bin/sh
# (replace "find ." with "find ./<path>" below, to work with only specific paths)
# (these lines are really all one line, on multiple lines for clarity)
# ...find symlinks which do not dereference to directories...
find . -type l -exec test '!' -d {} ';' -print -exec sh -c \
# ...remove the symlink blob, and add the content diff, to the index/cache
'git rm --cached "$1"; diff -au /dev/null "$1" | git apply --cached -p1 -' \
# ...and call out to "sh".
"process_links_to_nondir" {} ';'
# the end
笔记
我们尽可能多地使用POSIX兼容功能;然而,diff -a不符合POSIX,可能还有其他原因。
这段代码中可能有一些错误/错误,即使它经过了一些测试。
注意:从Git 1.6.1开始,这个通知就已经过时了。Git过去是这样的,现在不再是这样了。
Git默认情况下尝试存储符号链接,而不是遵循它们(为了紧凑,这通常是人们想要的)。
但是,当符号链接是一个目录时,我意外地设法让它在符号链接之外添加文件。
例如:
/foo/
/foo/baz
/bar/foo --> /foo
/bar/foo/baz
通过做
git add /bar/foo/baz
当我尝试时,它似乎起作用了。然而,这种行为在当时是我不想要的,所以我不能给你更多的信息。
嗯,mount -bind似乎对达尔文不起作用。
有人有什么诀窍吗?
(编辑)
好吧,我在Mac OS X上找到了答案,那就是做一个硬链接。只是这个API不是通过ln公开的,所以你必须用你自己的小程序来做这件事。以下是该程序的链接:
在Mac OS X中创建目录硬链接
享受吧!