使diff忽略符号链接

时间:2011-03-28 08:24:15

标签: linux diff patch

我的项目具有以下结构

/project/config.mk
/project/dir1/config.mk -> ../config.mk
/project/dir2/config.mk -> ../config.mk

当我使用diff创建补丁文件时,/project/config.mk已正确完成,但这两个符号链接出现了一些问题。它们都被视为新文件,差异部分是config.mk文件的全部内容。我试图找到一个diff选项来禁用以下符号链接,但没有这样的选项可用。任何建议都表示赞赏。

作为Overbose的建议,我创建了这个脚本。有用。感谢大家抽出时间回答。

#!/bin/sh -v
ori_dir=$1
new_dir=$2
patch_file=./patch_file
if [ -f ${patch_file} ]
then
        rm ${patch_file}
fi
ori_files=`cd ${ori_dir} ; find ./ -type f ! -type l`
for i in ${ori_files} ; do 
        if [ -f ${ori_dir}/$i ] 
        then
                if [ -f ${new_dir}/$i ]
                then
                        diff -rup ${ori_dir}/$i ${new_dir}/$i >> ${patch_file}
                fi
        fi
done

4 个答案:

答案 0 :(得分:7)

在GNU diff v3.3中,现在有一个选项--no-dereference可以解决这个问题

答案 1 :(得分:4)

如果添加以下行:

dir1/config.mk
dir2/config.mk

到文件.ignore-diff

然后你可以像这样执行diff(1)

diff -ur -X .ignore-diff

答案 2 :(得分:1)

以下列方式使用find:

find . ! -type l

此选项应跳过以下符号链接。使用该命令在运行diff之前找到您的文件。

答案 3 :(得分:0)

作为我尝试过的总结。工作解决方案是使用--no-dereference for my case。

对于-X选项,您可以通过以下链接获取帮助:stackoverflow.com : how-do-you-diff-a-directory-for-only-files-of-a-specific-type

# max diff
diff -ruN ~/xxx-web/ ~/www/xxx/xxx-web/ > ~/xxx-web/20180523-diff.patch
# but may get too much symbolic link following, so in GNU diff v3.3 use :
diff --no-dereference -ruN ~/xxx-web/ ~/www/xxx/xxx-web/ > ~/xxx-web/20180523-diff.patch

## if no options available, you can try a solution that do not seem to work under 3.3 :
# find . -type l > .diffignore
## passing direct file will not work, it require file pattern ?
# diff -ruN -x .diffignore 
## passing with xargs do not seem to work the way below on 3.3 ...
# diff -ruN $(cat .diffignore | xargs -L1 echo "-x ")
# diff -ruN $(cat .diffignore | xargs -i echo "-x '{}'" | sed 's,\./vivaoresto-web/,*,g') \
# inFolder outFolder > diff.patch
# if you wanna apply your path in the source directory :
cd ~/www/xxx/xxx-web/
patch -p1 < ~/xxx-web/20180523-diff.patch