【已解决】用diff制作patch时,如何只针对特定文件或文件夹

【背景】

折腾:

【记录】尝试为了Buildroot编译期间涉及到的m4-1.4.16的fpending在Cygwin下的问题去制作diff和patch补丁包

期间,需要针对特定的文件(或文件夹)去用diff制作出patch文件,

即,

忽略掉

host-m4-1.4.16/ChangeLog

host-m4-1.4.16/lib/fpending.h

host-m4-1.4.16/m4/fpending.m4

的修改,

只针对于:

host-m4-1.4.16/lib/stdio.in.h

的修改,去做出一个patch文件。

【折腾过程】

1.参考:

How do you diff a directory for only files of a specific type?

去看看,diff的-x参数的含义。

【整理】Linux命令:diff用法和参数含义

看到:

-x, –exclude=PAT               exclude files that match PAT
-X, –exclude-from=FILE         exclude files that match any pattern in FILE

即,用-x,去排除,某一类的文件或某个文件。

2.但是我此处,其实只需要处理特定的:

host-m4-1.4.16/lib/stdio.in.h

文件,所以,貌似没法写个排除的pattern。

3.所以搜:

diff specific files

再去参考:

Only include files that match a given pattern in a recursive diff

去试试:

find host-m4-1.4.16.orig/ host-m4-1.4.16/ -type f ! -name ‘stdio.in.h’ -printf ‘%f\n’ | diff -purN host-m4-1.4.16.orig/ host-m4-1.4.16/ -X –

但是先去看看:

find host-m4-1.4.16.orig/ host-m4-1.4.16/ -type f ! -name ‘stdio.in.h’ -printf ‘%f\n’

的输出,发现是可以输出,除了该特定文件的,所有的其他文件的。

所以,再去试试:

CLi@PC-CLI-1 ~/develop/buildroot/diff_patch
$ find host-m4-1.4.16.orig/ host-m4-1.4.16/ -type f ! -name 'stdio.in.h' -printf '%f\n' | diff -purN host-m4-1.4.16.orig/ host-m4-1.4.16/ -X -

好像没有生成patch文件。

重新试试:

CLi@PC-CLI-1 ~/develop/buildroot/diff_patch
$ find host-m4-1.4.16.orig/ host-m4-1.4.16/ -type f ! -name 'stdio.in.h' -printf '%f\n' | diff -purN host-m4-1.4.16.orig/ host-m4-1.4.16/ -X - > m4-1.4.16-no-gets-and-missing-binary-operator.patch

结果生成的:

m4-1.4.16-no-gets-and-missing-binary-operator.patch

也是个0KB的空文件。

4.突然想起来了,之前是对于:

host-m4-1.4.16/lib/stdio.in.h

和:

host-m4-1.4.16.orig/lib/stdio.in.h

是忘了修改,所以没有任何改动。

5.所以,先去修改:

host-m4-1.4.16/lib/stdio.in.h

改为:

/* It is very rare that the developer ever has full control of stdin,
   so any use of gets warrants an unconditional warning.  Assume it is
   always declared, since it is required by C89.  */
#undef gets
#if defined(__GLIBC__) && !defined(__UCLIBC__)
#if !((__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16))
_GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");
#endif
#endif

然后再去执行上述的命令:

CLi@PC-CLI-1 ~/develop/buildroot/diff_patch
$ find host-m4-1.4.16.orig/ host-m4-1.4.16/ -type f ! -name 'stdio.in.h' -printf '%f\n' | diff -purN host-m4-1.4.16.orig/ host-m4-1.4.16/ -X - > m4-1.4.16-no-gets-and-missing-binary-operator.patch

的确也是可以生成对应的:

m4-1.4.16-no-gets-and-missing-binary-operator.patch

的,内容为:

diff -purN -X - host-m4-1.4.16.orig/lib/stdio.in.h host-m4-1.4.16/lib/stdio.in.h
--- host-m4-1.4.16.orig/lib/stdio.in.h	2011-03-02 00:39:29.000000000 +0800
+++ host-m4-1.4.16/lib/stdio.in.h	2013-08-28 16:53:28.693212500 +0800
@@ -162,7 +162,11 @@ _GL_WARN_ON_USE (fflush, "fflush is not
    so any use of gets warrants an unconditional warning.  Assume it is
    always declared, since it is required by C89.  */
 #undef gets
+#if defined(__GLIBC__) && !defined(__UCLIBC__)
+#if !((__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16))
 _GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");
+#endif
+#endif
 
 #if @GNULIB_FOPEN@
 # if @REPLACE_FOPEN@

如图:

m4-1.4.16-no-gets-and-missing-binary-operator.patch content

5.另外,也想起来了:

此处是处理单个文件,所以,貌似可以直接只对比此单个文件,所以去试试:

(试之前,先去修改文件)

diff -purN host-m4-1.4.16.orig/lib/stdio.in.h host-m4-1.4.16/lib/stdio.in.h > m4-1.4.16-no-gets-and-missing-binary-operator-diffSingleFile.patch

果然也是可以的:

CLi@PC-CLI-1 ~/develop/buildroot/diff_patch
$ diff -purN host-m4-1.4.16.orig/lib/stdio.in.h host-m4-1.4.16/lib/stdio.in.h > m4-1.4.16-no-gets-and-missing-binary-operator-diffSingleFile.patch

生成的patch内容为:

--- host-m4-1.4.16.orig/lib/stdio.in.h	2011-03-02 00:39:29.000000000 +0800
+++ host-m4-1.4.16/lib/stdio.in.h	2013-08-28 16:53:28.693212500 +0800
@@ -162,7 +162,11 @@ _GL_WARN_ON_USE (fflush, "fflush is not
    so any use of gets warrants an unconditional warning.  Assume it is
    always declared, since it is required by C89.  */
 #undef gets
+#if defined(__GLIBC__) && !defined(__UCLIBC__)
+#if !((__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16))
 _GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");
+#endif
+#endif
 
 #if @GNULIB_FOPEN@
 # if @REPLACE_FOPEN@

和上面的命令,出来的结果,查了第一行的:

diff -purN -X - host-m4-1.4.16.orig/lib/stdio.in.h host-m4-1.4.16/lib/stdio.in.h

有点奇怪。

先不管了。

至少,此处,可以实现了,针对特定的文件(此处为单个文件)或文件夹,去执行对应的diff命令,生成对应的patch的。

 

【总结】

此处,通过:

find host-m4-1.4.16.orig/ host-m4-1.4.16/ -type f ! -name 'stdio.in.h' -printf '%f\n' | diff -purN host-m4-1.4.16.orig/ host-m4-1.4.16/ -X - > m4-1.4.16-no-gets-and-missing-binary-operator.patch

的方式,可以生成,针对特定文件,此处为单个的:

host-m4-1.4.16/lib/stdio.in.h

的patch文件:

m4-1.4.16-no-gets-and-missing-binary-operator.patch

的,其内容为:

diff -purN -X - host-m4-1.4.16.orig/lib/stdio.in.h host-m4-1.4.16/lib/stdio.in.h
--- host-m4-1.4.16.orig/lib/stdio.in.h	2011-03-02 00:39:29.000000000 +0800
+++ host-m4-1.4.16/lib/stdio.in.h	2013-08-28 16:53:28.693212500 +0800
@@ -162,7 +162,11 @@ _GL_WARN_ON_USE (fflush, "fflush is not
    so any use of gets warrants an unconditional warning.  Assume it is
    always declared, since it is required by C89.  */
 #undef gets
+#if defined(__GLIBC__) && !defined(__UCLIBC__)
+#if !((__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16))
 _GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");
+#endif
+#endif
 
 #if @GNULIB_FOPEN@
 # if @REPLACE_FOPEN@

效果还不错。



发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量