Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the output of diff to create the patch

Tags:

diff

patch

I have something like this

src/sim/simulate.cc
41d40
< #include "mem/mem-interface.h"
90,91d88
<             dram_print_stats_common(curTick/500);
<
src/mem/physical.hh
52d51
<   public:
55,56d53
<       public:
<
58a56,57
>       public:
>
61,62c60,61
<         virtual bool recvTiming(PacketPtr pkt); //baoyg
<
---

I believe this was created using the diff command in a source tree. What I want is to create the patch using that output, and to apply the same changes to my source tree.

like image 630
Eduardo Avatar asked Jan 12 '09 22:01

Eduardo


People also ask

What is difference between diff and patch?

Diff catalogs changes between two files, and patch uses those changes, puts them into a file and updates older versions of files with those changes. For example, consider the following two files: original-code contains the phrase Here are a few words. updated-code contains the phrase Here are a few more words.


3 Answers

I believe that diff -u oldfile newfile > a.patch is used to create patch files, although some other switched may be thrown in as well (-N?).

Edit: OK, 4 years later and finally going to explain what the switches mean:

-u creates a Unified diff. Unified diffs are the kind of diffs that the patch program expects to get as input. You can also specify a number after the u (min 3, default 3) to increase the number of lines output. This is in case 3 lines isn't unique enough to pinpoint just one spot in the program.

-N treats absent files as being empty, which means it will produce a lot of additional content if one of the files is empty (or see next point).

Also, newfile and oldfile can both be directories instead of single files. You'll likely want the -r argument for this to recurse any subdirectories.

like image 65
Powerlord Avatar answered Oct 07 '22 16:10

Powerlord


If you want to get the same patch output as SVN or git diff, given two different files or folders:

diff -Naur file1.cpp file2.cpp 
like image 22
Andrei Taranchenko Avatar answered Oct 07 '22 17:10

Andrei Taranchenko


What you have there is a non-unified diff. patch can read it, but will be unable to make context matches and is more likely to make mistakes.

like image 38
Sparr Avatar answered Oct 07 '22 16:10

Sparr