Terminal Friday: Patch and Diff

Two essential tools in software development are Patch and Diff . How they work isn't a mystery, but I think this will be an interesting post. 😉

Both are very powerful, and this is just like touching the surface, they have more utilities and other functions. Basically with these two tools we can create version control,


Diff

We refer to comparison, it compares an "original" file with the "new" one, and informs us of the differences that exist between them. This tool also allows us to create .patch files that we use to create patches for our programs.


Patch

It is the command with which we literally "patch" our original file, adding and / or removing lines according to the instructions contained in the .patch file


There's also vimdiff , a visual tool for applying patches without needing a .patch file, as it compares the "original" and "new" versions, and allows you to edit line by line or the entire document within the same file. I won't explain this, but I think it's worth mentioning.


Example

Now the fun is. Example!

This is the case, we have a great script that asks for your name and your age, if you are over 18 it tells you that you can vote, otherwise it tells you that you cannot vote.

original.sh

#! / bin / bash echo "Enter your name:" read name echo "Enter your age:" read age if [[18 -lt $ age]] then echo "Hello $ name, you are $ old and you can vote!" else echo "Hello $ name, you are $ old and cannot vote ..." fi
Image of the code in Vim

Image of the code in Vim

Done, here is the running script:

Everything seems to be working fine

Everything seems to be working fine

So, as good users we are, we share our script with a friend :), but we get a message saying that it has a flaw, that when it is 18 it says that it cannot vote when it should.

Logic error :(

Logic error 

Now we begin to correct the small error and make a few modifications ...

new.sh

#! / bin / bash maxAge = 18 echo "Enter your name:"; read name echo "Write your age:"; read age if [[$ maxAge -le $ age]]; then echo "Hello $ name, you are $ years old and you can vote!" else echo "Hello $ name, you are $ years old and you cannot vote ..." fi exit 0
New code written in Vim

New code written in Vim

Let's suppose that the script is very heavy. So, in order not to send the whole script again, we create a .patch 

$ diff -u original.sh new.sh> patch.patch

And now we have our patch. Here's a view in Vim :

This is what the instructions for a .patch look like. In white the lines that were not modified, in blue those removed, in orange those added.

This is what the instructions for a .patch look like. In white the lines that were not modified, in blue those removed, in orange those added.

To apply it, we simply use the .patch file in the script to be patched. Here it's called originalAmigo.sh , which is an exact copy of the original.sh script.

Friend script

Friend script

$ patch originalFriend.sh <patch.patch

And this leaves our originalAmigo.sh file like this:

Friend script after applying patch

Friend script after applying patch

As you can see it is very easy to get the diffs and apply the patches. It's all from me.

Greetings people, we will read next Friday.


Add as preferred source in Google