如何在Windows上使用Cygwin中的diff和patch?

4
我关注了很多文章,但仍然不知道如何在Cygwin中使用diff和patch。 我在Windows资源管理器中创建了2个文件。 origin.txt
one
two
three

new.txt

one
four
five

然后我执行了 diff -u origin.txt new.txt > file.patch 命令,结果如下。
--- origin.txt  2013-03-21 15:53:20.062956800 +0700
+++ new.txt 2013-03-21 15:53:29.191869600 +0700
@@ -1,3 +1,3 @@
 one
-two
-three
\ No newline at end of file
+four
+five
\ No newline at end of file

然后我执行了patch origin.txt < file.patch,但出现了错误。
patching file origin.txt
Hunk #1 FAILED at 1.
1 out of 1 hunk FAILED -- saving rejects to file origin.txt.rej

如何克服这个问题?这与Unix和Windows文件格式有关吗?
附言:我正在使用CYGWIN_NT-6.1-WOW64和Windows 7 64位。

如果你没有patch安装的话,请使用 apt-cyg install patch(它在自己的包中叫做 patch)。 - not2qubit
1个回答

6

这几乎可以确定是一个行尾问题。您可以首先运行 dos2unix 命令来修复文件:

$ dos2unix origin.txt new.txt
$ patch origin.txt < file.patch
patching file origin.txt

那么,在进行 diff 和 patch 操作之前,我们需要先进行 dos2unix 转换,然后在完成操作后,再进行 unix2dos 转换吗? - onmyway133
2
@entropy:是的。或者最好始终保持Unix格式。 - me_and

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接