May 17, 2015

Linux Patches

Patches are a useful way to keep our changes of some vanilla/original code isolated, and then just apply the desired ones, enabling for example, to insert, remove or change some feature. Here you can see how to create a patch using "diff" and "patch" Unix/Linux tools.

Let's start creating two files, "original.txt":

original line 1
original line 2
original line 3

And "modified.txt" (modified original.txt):

original line 1
original modified line 2
original line 3
new line

Note that just the line 2 was modified and a new line inserted in the end. Now let's check the difference between them:

diff -u original.txt modified.txt 
--- original.txt 2015-05-17 10:44:26.454062645 -0300
+++ modified.txt 2015-05-17 10:45:40.598064350 -0300
@@ -1,3 +1,4 @@
 original line 1
-original line 2
+original modified line 2
 original line 3
+new line

Well, and this is the patch content! just place this inside a .patch file:

diff -u original.txt modified.txt > test.patch

Now let's apply the "test.patch" on "original.txt", look:

cat original.txt 
original line 1
original line 2
original line 3

patch -p0 -i test.patch 
patching file original.txt

cat original.txt 
original line 1
original modified line 2
original line 3
new line

Done! and you can remove the patch too, look:

cat original.txt 
original line 1
original modified line 2
original line 3
new line

patch -R -i test.patch 
patching file original.txt

cat original.txt 
original line 1
original line 2
original line 3

That's all!

0 comentários :

Post a Comment