Monday, February 5, 2007

Creating and applying patches in Linux

Anybody who has ever worked in software development would have many times come across the issue of updating or patching their projects to the latest version. In development environments this task is fairly easy if they are using version-ing systems like CVS, SVN, Visual Source Safe etc. But for a quick work around when no such infrastructure is in place is to use the simple linux command "diff".

For the purpose of demonstration we can create a directory structure that looks like a software project .

[safeer@LinuxBox tmp]$mkdir version1;cd version1
[safeer@LinuxBox tmp]$echo "File1version1" > file1;echo "File2version1" > file2
[safeer@LinuxBox tmp]$mkdir includes1;cd includes1
[safeer@LinuxBox tmp]$echo "Lib1version1" > lib1;echo "Lib2version1" > lib2

The project directory looks like this:

version1
|------includes1
| |-------lib1
| |-------lib2
|-------file1
|-------file2

Now I am going to update this project.

[safeer@LinuxBox tmp]$cp -r version1 version2

Whenever you update your project, keep a copy of the source project untouched and update only a fresh copy of that project.

Updating file1 & lib1
[safeer@LinuxBox tmp]$echo File1version2 >> file1;cd includes1;echo Lib2version2 >> lib2

Add new subdirectory and files to project
[safeer@LinuxBox tmp]$mkdir includes2;cd includes2;echo "Lib3version2" > lib3;echo "Lib4version2" > lib34

The updated project directory looks like this:

version2
|------includes1
| |-------lib1
| |-------lib2 ====> Updated
|------includes2 ====
| |-------lib3 ==== } Newly added
| |-------lib4 ====
|-------file1
|-------file2 ====> Updated

[safeer@LinuxBox tmp]$ls
veersion1 version2

Now create the patch file:

[safeer@LinuxBox tmp]$diff -Naur version1 version2 > version-1-2.patch
[safeer@LinuxBox tmp]$cat version-1-2.patch


diff -Naur version1/file1 version2/file1
--- version1/file1 2007-02-05 11:31:08.000000000 +0000
+++ version2/file1 2007-02-05 11:33:34.000000000 +0000

@@ -1 +1,2 @@
File1version1
+File1version2
diff -Naur version1/includes1/lib2 version2/includes1/lib2
--- version1/includes1/lib2 2007-02-05 11:32:20.000000000 +0000
+++ version2/includes1/lib2 2007-02-05 11:34:06.000000000 +0000
@@ -1 +1,2 @@
Lib2version1
+Lib2version2
diff -Naur version1/includes2/lib3 version2/includes2/lib3
--- version1/includes2/lib3 1970-01-01 00:00:00.000000000 +0000
+++ version2/includes2/lib3 2007-02-05 11:35:16.000000000 +0000
@@ -0,0 +1 @@
+Lib3version2
diff -Naur version1/includes2/lib4 version2/includes2/lib4
--- version1/includes2/lib4 1970-01-01 00:00:00.000000000 +0000
+++ version2/includes2/lib4 2007-02-05 11:35:22.000000000 +0000
@@ -0,0 +1 @@
+Lib4version2

Now suppose that our old project is installed in /usr/project/



[safeer@LinuxBox tmp]$cd /usr/project;ls
version1
Now copy the patch file to this directory
[safeer@LinuxBox tmp]$cp /home/safeer/tmp/version-1-2.patch .
Now apply the patch

[safeer@LinuxBox tmp]$patch -p0 < version-1-2.patch
patching file version1/file1
patching file version1/includes1/lib2
patching file version1/includes2/lib3
patching file version1/includes2/lib4


The patch has updated the modified files file1,lib2 and created new directory includes2 with two files lib3 and lib4 in it.
diff command can record the difference of not only text files, but executables also. So this will work for all sort of files


1 comment:

  1. Đấu tôn, bên trong tinh tạp chính là một ngàn một trăm mười một vạn kim tệ. Từng ấy kim tệ, Nhạc Thành cũng không để trong lòng, bây giờ trên người Nhạc Thành kim tệ có gần ngàn vạn, cũng không biết của ai, cuối cùng Nhạc Thành cũng chỉ để chung một chỗ.

    Tên lục tinh Đấu tôn sau khi kiểm tra tinh tạp xong, sau đó thu vào trong trữ vật giới chỉ, cuối cùng đưa cho Tôn Thi Thi một tấm thẻ khách quý, Tôn Thi Thi tiêu phí ở đây tuyệt đối là khách quý.

    Lúc này mọi người ở đấu giá hội cũng từ từ rời đi, bên trong tầng thứ năm cũng có không ít người đi xuống, mà ở trong đám người, thần thức Nhạc Thành vẫn tập trung vào ba người xen lẫn trong đó, ba người này là một ông già và hai trung niên hán tử, bất quá bây giờ khuôn mặt ba người này một mảnh tái nhợt, dường như không có chút máu vậy.

    Mà ba người này hữu ý đưa ánh mắt về phía nhóm người Nhạc Thành.

    Nhạc Thành cũngdongtam
    mu moi ra hom nay
    tim phong tro
    http://nhatroso.com/
    nhạc sàn
    tổng đài tư vấn luật
    văn phòng luật hà nội
    tổng đài tư vấn luật
    thành lập công ty trọn gói
    http://we-cooking.com/
    chém gió
    trung tâm tiếng anh không có để ý ba người, hắn thu hồi trận bàn vào trong trữ vật giới chỉ, còn áo choàng Tôn Thi Thi đưa cho Yến Hiểu Kỳ, trường kiếm Tôn Thi Thi đưa cho U Cơ.

    Mọi người đi xuống tầng thứ năm, lập tức đi ra khỏi đấu giá hội, sau đó hướng trở về khách điếm.

    - Các ngươi đứng lại cho ta.

    Mọi người đi trở về, đột nhiên có tiếng quát

    ReplyDelete