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

14,960

Solution 1

OK, I finally solved the issue and learned my lesson aswell. :)

The patch are created with Git Bash command prompt:

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:

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 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. :)

Solution 2

Try applying the patch using 'git apply' instead of 'patch'. git apply will call patch but should obey any other git directives which might sort your crlf issue here. It also accepts patch options (like 'git apply -p1' for pruning path elements). I quite often also use 'git apply' to apply patches from git to CVS trees. 'git apply' doesn't require that the target be a git repository.

Share:
14,960
Placeholder
Author by

Placeholder

Still fighting for what's real and right. Yeah

Updated on June 04, 2022

Comments

  • Placeholder
    Placeholder almost 2 years

    I'm trying to apply a .patch file to a single .cpp file using git diff.

    These are my files: old.cpp , new.cpp and fix.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. 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.

    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:

    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:

    patch old.cpp fix.patch
    

    the patch is being applied successfully but the size of old.cpp reduces from 918 KB to 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. I guess this is the reason why the file size of old.cpp reduces too.

    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. 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. :(

    I am using Windows XP SP2, Git 1.7.6, Git Extensions 2.24 and Microsoft Visual C++ 2010.

  • Placeholder
    Placeholder over 12 years
    I'm sorry patthoyts, but I'm really unexperienced with git apply. Would you mind pointing me out how exactly should I apply my patch the way I want using git apply. Thank you in advance. :)
  • patthoyts
    patthoyts over 12 years
    Just like you would use 'patch'. The patch file contains information about the file to be modified in the patch header so 'git apply -p0 changes.patch' will apply the patch to the current directory.
  • Placeholder
    Placeholder over 12 years
    Oh, now I got error - fix.patch:8 trailing whitespace and error: patch failed: old.cpp:1419 and error: old.cpp: patch does not apply
  • Philip Oakley
    Philip Oakley over 12 years
    Give us a clue: "Nevermind I fixed it." - It would be useful to know so that I/others avoid the same issues..
  • Placeholder
    Placeholder over 12 years
    @Philip Oakley, I couldn't post the answer at the time, because the website required me to wait 8 hours before replying to myself. I just posted my solution. :)