Using the diff and patch utilities.

Today, came across a nice and simple to use way of updating code.  This is achieved by the diff and patch utility on Linux. (Sorry, windows users.) These are tools that i am sure every programmer will find worth using.

Here is a small lecture on it.

You will often find that patches are submitted to bug
tracking systems and project mailing lists for review. The reason is
that if the code is very large, it would be a waste of space and
bandwidth to attach the whole original file.

The diff command is used to check the difference between two programs.
Try this. Edit a text file and call it first. Add the following:

Apple
Pear
Banana

Next, copy the file to another file, second:

cp first second

Now you have two identical files. Type:

% diff first second

That should not produce any output. Edit second and add Orange somewhere
in the file. Save and try:

% diff first second again

Now you should get some output. How it looks will depend on where you
placed the word Orange. I placed it last in the list and my diff output
looks like this:

3a4
> Orange

If you use a revision control tool it can normally do diffs between
revisions for you. For instance bazaar, which I use allows you to:

The difference between the latest commit and the current files
% bzr diff

The difference between the current files and revision 5
% bzr diff -r5

The difference between revisions 67 and 68
bzr diff -r67..68

We can do something really useful with diff: we can send the diff output
to a file. That file is normally referred to as a patch. Try this out:

% diff first second > myproject.001.patch

Next we can use patch to import the changes to first:

% patch first < myproject.001.patch

Check the content of first. Notice how Orange is there now. The file
first has been patched with the changes from the file second.


About this entry