You are not logged in.

#1 2005-09-22 17:20:22

xerxes2
Member
From: Malmoe, Sweden
Registered: 2004-04-23
Posts: 1,249
Website

diff and patch

hello guys!

what is the 'right' way to use diff and patch?
i just want the standard simple commands for it,


arch + gentoo + initng + python = enlisy

Offline

#2 2005-09-22 18:01:13

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: diff and patch

I use unified diffs for patch (-u).  Whenever I diff something I do "diff -uNr".  This will do a unified diff, and recursively (over all files).  The N flag will also add new files into the diff (i.e. the whole file will be listed with +++).
When patching, use -u for unified (-n for normal diffs).    The most important flag, however, is -p.  -p will strip off components in the filename, which is important with recursive diffs.

So, let's take some example "src/" directory.  We'll copy it to "src-old/" and then make modifications inside src/.
When we're done:

$ diff -uNr src-old/ src/ > src.patch
rm -rf src/
mv src-old/ src/
cd src/
patch -up1 ../src.patch

The patch file will contain listings like "diff -uNr src-old/myfile.c src/myfile.c" - when you try to patch this, it looks for those dirs (which won't work, because src-old doesn't exist).  So, with -p1 (strip 1 off filename), it converts it to "diff -uNr myfile.c myfile.c" and it patches correctly.

Offline

#3 2005-09-22 18:05:21

xerxes2
Member
From: Malmoe, Sweden
Registered: 2004-04-23
Posts: 1,249
Website

Re: diff and patch

hey thanks phrak, big_smile
that's what i call an explanation!

if i get in trouble again i know where to ask, but this stuff works just as i wanted,


arch + gentoo + initng + python = enlisy

Offline

#4 2005-09-25 02:43:20

Euphoric Nightmare
Member
From: Kentucky
Registered: 2005-05-02
Posts: 283

Re: diff and patch

I just do
diff file file2 > file.diff
patch <file.diff

or something like that...

Offline

#5 2005-09-26 16:06:53

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: diff and patch

Euphoric Nightmare wrote:

I just do
diff file file2 > file.diff
patch <file.diff

or something like that...

unified diffs are easier to read, and give you the ability for a "fuzz" factor (i.e. it uses more than just line numbers).  The other nice thing about unified diffs is that everything above the ---/+++ lines at the top is ignored.  Which is why people will send an email and just put the unified diff at the bottom, because with mutt or something similar, you can just pipe the whole email to patch.

$ diff -u hexout.old hexout.cpp 
--- hexout.old  Mon Sep 26 11:04:22 2005
+++ hexout.cpp  Mon Sep 26 11:03:22 2005
@@ -9,6 +9,8 @@
        int len = 0;
        for(int i = 0; i < 8; ++i)
        {
+               len <<= 4;
+               char c = hdr[13+i];
                cout << "c=" << c << endl;
                if(c >= '0' && c <= '9') len += (c-'0');
                else if(c >= 'A' && c <= 'F') len += (c+10-'A');
$ diff  hexout.old hexout.cpp 
11a12,13
>               len <<= 4;
>               char c = hdr[13+i];

Offline

#6 2005-09-29 14:13:01

sweiss
Member
Registered: 2004-02-16
Posts: 635

Re: diff and patch

Hmm, I've recently submitted a couple of patches for KDE (yay me) and I used Kompare to generate those. The default command used by Kompare is

diff -U 3 -dHrN -- new_file orig_file

I got a quite readable patch using this method.

Offline

#7 2005-11-06 09:37:58

Cam
Member
From: Brisbane, Aus
Registered: 2004-12-21
Posts: 658
Website

Re: diff and patch

phrakture wrote:

I use unified diffs for patch (-u).  Whenever I diff something I do "diff -uNr".  This will do a unified diff, and recursively (over all files).  The N flag will also add new files into the diff (i.e. the whole file will be listed with +++).
When patching, use -u for unified (-n for normal diffs).    The most important flag, however, is -p.  -p will strip off components in the filename, which is important with recursive diffs.

So, let's take some example "src/" directory.  We'll copy it to "src-old/" and then make modifications inside src/.
When we're done:

$ diff -uNr src-old/ src/ > src.patch
rm -rf src/
mv src-old/ src/
cd src/
patch -up1 ../src.patch

The patch file will contain listings like "diff -uNr src-old/myfile.c src/myfile.c" - when you try to patch this, it looks for those dirs (which won't work, because src-old doesn't exist).  So, with -p1 (strip 1 off filename), it converts it to "diff -uNr myfile.c myfile.c" and it patches correctly.

I tried doing that, when I apply the patch it just hangs, nada. Any ideas spring to mind as to why this would be happening?

Offline

#8 2005-11-06 11:41:05

awalk
Member
From: Perth, Western Australia
Registered: 2005-02-14
Posts: 40

Re: diff and patch

Cam wrote:
$ diff -uNr src-old/ src/ > src.patch
rm -rf src/
mv src-old/ src/
cd src/
patch -up1 ../src.patch

I tried doing that, when I apply the patch it just hangs, nada. Any ideas spring to mind as to why this would be happening?

I'm no patch boffin, but I believe that call to patch in the above code is missing a redirection. To apply a patch, you redirect the contents of the file into patch:

patch < patch_file

Offline

#9 2005-11-06 11:43:41

Cam
Member
From: Brisbane, Aus
Registered: 2004-12-21
Posts: 658
Website

Re: diff and patch

awalk wrote:
Cam wrote:
$ diff -uNr src-old/ src/ > src.patch
rm -rf src/
mv src-old/ src/
cd src/
patch -up1 ../src.patch

I tried doing that, when I apply the patch it just hangs, nada. Any ideas spring to mind as to why this would be happening?

I'm no patch boffin, but I believe that call to patch in the above code is missing a redirection. To apply a patch, you redirect the contents of the file into patch:

patch < patch_file

Actually, I think you're quite right, I never thought of that! You can also use the -i flag to pass it a file I'm pretty sure big_smile

Offline

#10 2005-11-06 16:11:45

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: diff and patch

I always use this howto:

http://www.kegel.com/academy/opensource.html

I have to look up the command every time, can never remember it. sad

Dusty

Offline

Board footer

Powered by FluxBB