[Ilugc] .a-tip-a-day (patch - apply diffs and act as inverse of diff)

  • From: girishvenkatachalam@xxxxxxxxx (Girish Venkatachalam)
  • Date: Wed Sep 30 05:24:56 2009

Yesterday we saw diff in somewhat detail.

http://ae.iitm.ac.in/pipermail/ilugc/2009-September/051658.html

Today we are going to see the most important tool that every hacker should know:

PATCH(1)                                                              PATCH(1)

NAME
       patch - apply a diff file to an original

SYNOPSIS
       patch [options] [originalfile [patchfile]]

       but usually just

       patch -pnum <patchfile

There ya go. patch applies a diff from the original file to get a modified file.

This is what you do when you send your genius invention to your mother stream
upstream. IOW you need to send a bunch of patch files or even have them in one
single patch file(also known as patch set) to the author/maintainer of
the project.

But applying patch can be painful.

Here is an example.

$ cat file.txt
Slow day.  Practice crawling.
You are farsighted, a good planner, an ardent lover, and a faithful friend.
You will lose your present job and have to become a door to door mayonnaise
salesman.
Don't Worry, Be Happy.
                -- Meher Baba
Q:      Why should you always serve a Southern Carolina football man
        soup in a plate?
A:      'Cause if you give him a bowl, he'll throw it away.

$ cat file-new.txt
You are farsighted, a good planner, an ardent lover, and a faithful friend.
You will lose your present job and have to become a door to door mayonnaise
salesman.
                -- Meher Baba
Q:      Why should you always serve a Southern Carolina football man
        soup in a plate?
A:      'Cause if you give him a bowl, he'll throw it away.
Don't Worry, Be Happy.

You will be winged by an anti-aircraft battery.

You will reach the highest possible point in your business or profession.
Slow day.  Practice crawling.


$  diff -u file.txt file-new.txt
--- file.txt    2009-09-30 05:14:00.000000000 +0530
+++ file-new.txt        2009-09-30 05:14:31.000000000 +0530
@@ -1,9 +1,13 @@
-Slow day.  Practice crawling.
 You are farsighted, a good planner, an ardent lover, and a faithful friend.
 You will lose your present job and have to become a door to door mayonnaise
 salesman.
-Don't Worry, Be Happy.
                -- Meher Baba
 Q:     Why should you always serve a Southern Carolina football man
        soup in a plate?
 A:     'Cause if you give him a bowl, he'll throw it away.
+Don't Worry, Be Happy.
+
+You will be winged by an anti-aircraft battery.
+
+You will reach the highest possible point in your business or profession.
+Slow day.  Practice crawling.

Now the idea is to create file-new.txt from a guy who has file.txt.

How do you do it?

$ cp file.txt /tmp
$ diff -u file.txt file-new.txt > patch.txt
$ cd /tmp
$ patch -p0 < patch.txt

Now look at file.txt.

It has the same contents as file-new.txt

$ diff /tmp/file.txt ~/file-new.txt

Both files are same.

Now why I say patch is complicated.

Try patch again on top of patched file.

$ patch -p0  </home/girish/patch.txt
patching file file.txt
Reversed (or previously applied) patch detected!  Assume -R? [n]

What a question and how confusing?

You should ask Larry Wall, the author of perl and this patch utility
why his brain works
in such a complicated fashion.

Anyway remember these things with patch.

         o patch needs a p0 or p1 argument, typically it is only one
of these two - try to find out
            when to use what switch
         o patch always needs the '<' operator. Without that patch
will never do its job

Once again it shows Larry's complex thought process. Isn't it obvious
that I will only give
a patchfile as argument to patch? Why should I redirect? Anyway I have
nothing personal
against the fellow.

After all I love Perl.

Anyway patch files can contain a lot of gunk both at the top and
bottom. You can apply a patch
from a saved e-mail or in the middle of a document and it will still work.

-Girish
-- 
Gayatri Hitech
web: http://gayatri-hitech.com

SpamCheetah Spam filter:
http://spam-cheetah.com

Other related posts: