简体   繁体   English

如何使用diff创建并将.patch文件正确应用到单个.cpp文件?

[英]How to create and apply properly .patch file to a single .cpp file using diff?

I'm trying to apply a .patch file to a single .cpp file using git diff . 我正在尝试使用git diff.patch文件应用于单个.cpp文件。

These are my files: old.cpp , new.cpp and fix.patch . 这些是我的文件: old.cppnew.cppfix.patch

old.cpp is the original unmodified source code, new.cpp is the modified source and fix.patch is the patch I want to create which when applied to old.cpp should apply the changes from new.cpp to it. old.cpp是原始未修改的源代码,new.cpp是修改源和fix.patch是我想要的,当应用于old.cpp应该适用从new.cpp的更改它来创建补丁。 Both old.cpp and new.cpp are with Windows (CR LF) line endings, both files are 918 KB large, only one line is being modified in the source. old.cppnew.cpp都使用Windows(CR LF)行结尾,两个文件都是918 KB大,在源中只修改了一行。

I create the patch by putting the two files old.cpp and new.cpp in the same folder and using Git Bash prompt with the command: 我通过将两个文件old.cppnew.cpp放在同一个文件夹中并使用Git Bash提示符来创建补丁:

git diff -u old.cpp new.cpp > fix.patch

The fix.patch file successfully appears but when I actually test it and apply it to old.cpp with Git Bash by typing: fix.patch文件成功出现,但是当我实际测试它并通过键入以下命令将其应用于带有Git Bashold.cpp

patch old.cpp fix.patch

the patch is being applied successfully but the size of old.cpp reduces from 918 KB to 894 KB . 补丁正在成功应用,但old.cpp的大小从918 KB 减少894 KB After some research with kdiff3 I found that my newly created fix.patch file is with Unix (LF) line endings and after applying it to old.cpp , the patched old.cpp is adopting Unix (LF) line endings too. 经过对kdiff3的一些研究后,我发现我新创建的fix.patch文件是使用Unix(LF)行结尾,在将它应用到old.cpp之后 ,修补的old.cpp也采用了Unix(LF)行结尾。 I guess this is the reason why the file size of old.cpp reduces too. 我想这就是为什么old.cpp的文件大小减少的原因。

My question is what command should I use in git or what else I have to do so my newly created fix.patch file remains with Windows (CR LF) line endings and after applying the patch to old.cpp the newly patched old.cpp file is with Windows (CR LF) line endings too and the file size does not reduce so drastically. 我的问题是我应该在git中使用什么命令 ,或者我还需要做什么,以便我新创建的fix.patch文件保留在Windows(CR LF)行结尾,并在将修补程序应用于old.cpp之后新修补的old.cpp文件与Windows(CR LF)行结尾也是如此,文件大小不会大幅减少。 I received a suggestion here to use git apply instead of patch but I don't know what exactly to type after git apply so it works the way I want for my situation . 我在这里收到一个建议,使用git apply而不是patch但是我不知道在git apply之后输入到底是什么,所以它按我想要的方式工作。 :( :(

I am using Windows XP SP2 , Git 1.7.6 , Git Extensions 2.24 and Microsoft Visual C++ 2010 . 我使用的是Windows XP SP2Git 1.7.6Git Extensions 2.24Microsoft Visual C ++ 2010

OK, I finally solved the issue and learned my lesson aswell. 好的,我终于解决了这个问题并学到了我的教训。 :) :)

The patch are created with Git Bash command prompt: 使用Git Bash命令提示符创建补丁:

git diff -u old.cpp new.cpp > fix.patch

Then make sure the old.cpp and fix.patch are in the same folder the header of fix.patch reads: 然后确保old.cppfix.patch位于fix.patch标题所在的同一文件夹中:

diff --git a/old.cpp b/old.cpp
index 04784e1..da68766 100644
--- a/old.cpp
+++ b/oldcpp

After that type in Git Bash prompt: Git Bash提示符后输入:

git apply fix.patch

And voila! 瞧! No line messups, errors or problems. 没有线路混乱,错误或问题。 I hope this helps someone who stumbles open the same thing some day Thanks to everyone for the fast and deep replies, they helped me learn something new today. 我希望有一天能帮助那些偶然发现同样事情的人感谢大家快速而深刻的回复,他们帮助我学到了今天的新知识。 :) :)

Try applying the patch using 'git apply' instead of 'patch'. 尝试使用'git apply'而不是'patch'来应用补丁。 git apply will call patch but should obey any other git directives which might sort your crlf issue here. git apply将调用patch但应遵守任何其他git指令,这些指令可能会在这里排序你的crlf问题。 It also accepts patch options (like 'git apply -p1' for pruning path elements). 它还接受补丁选项(例如'git apply -p1'用于修剪路径元素)。 I quite often also use 'git apply' to apply patches from git to CVS trees. 我经常也使用'git apply'将补丁从git应用到CVS树。 'git apply' doesn't require that the target be a git repository. 'git apply'不要求目标是git存储库。

使用--include标志。

git apply patch.patch --include=new.cpp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM