Friday, November 23, 2007

建立 patch 檔的方法

玩 Linux 這麼久,常常會使用 patch 這個指令,今天我終於了解要怎麼產生一個 patch 檔了。

1. 準備修改前及修改後的程式碼 file.cpp , file.cpp.ori (檔名在這邊不重要)

2. 使用 diff 指令來產生 2 個檔案之間的差異︰
LC_ALL=C TZ=UTC0 diff -Naur file.cpp.ori file.cpp >> my_patch_file
前面加上的 LC_ALL=C TZ=UTC0 是根據 man patch 所提到的以下句子,並直接取用其範例指令 :
The diff command’s headers should have dates and times in Universal Time using traditional Unix format, so that patch recipients can use the -Z or --set-utc option.
這個指令會在目前的目錄下產生一個 my_patch_file 的純文字檔,大概會長這個樣子︰
--- file.cpp.ori 2007-11-23 10:07:59.737150770 +0000
+++ file.cpp 2007-11-23 10:08:06.477122545 +0000
@@ -2,4 +2,5 @@
void main()
{
std::cout << "Hello World!!\n";
+ std::cout << "This is my patch example.\n";
}

3. man patch 有提到,在釋出的 patch 檔中,應該避免使用 file.cpp.ori 這樣的檔名,這時候我們可以直接修改這個 patch 檔如下︰(注意紅字部份的修改)
--- old/file.cpp 2007-11-23 10:07:59.737150770 +0000
+++ new/file.cpp 2007-11-23 10:08:06.477122545 +0000
@@ -2,4 +2,5 @@
void main()
{
std::cout << "Hello World!!\n";
+ std::cout << "This is my patch example.\n";
}

透過這樣的修改,可以讓 patch 程式知道應該去哪裡找要修改的程式。這邊的 old, new 目錄名稱,patch 程式似乎是直接忽略。

4. 要做 patch 的時候,就進入 file.cpp (未修改過的)所在的目錄,並執行以下指令︰
patch -p1 < my_patch_file

5. 假設我有 2 個 cpp 檔要 patch ,分別位於 cpp/data1/file.cpp 及 cpp/data2/file2.cpp ,則可以將 diff 的結果放在同一個檔案內,如下︰
--- old/data1/file1.cpp 2007-11-23 10:07:59.737150770 +0000
+++ new/data1/file1.cpp 2007-11-23 10:21:07.780929751 +0000
@@ -2,4 +2,5 @@
void main()
{
std::cout << "Hello World!!\n";
+ std::cout << "This is my patch example.\n";
}
--- old/data2/file2.cpp 2007-11-23 10:07:59.737150770 +0000
+++ new/data2/file2.cpp 2007-11-23 10:21:54.503801884 +0000
@@ -2,4 +2,5 @@
void main()
{
std::cout << "Hello World!!\n";
+ std::cout << "This is 2nd patch example.\n";
}

使用這個 patch 的方法就是要先進入 cpp 這個目錄下再執行 patch 指令︰
cd cpp
patch -p1 < my_patch_file
patch 程式會根據 new/data1/file1.cpp , new/data2/file2.cpp 的指定,忽略掉前面的 new 後,分別進入 data1 及 data2 目錄 patch file1.cpp 及 file2.cpp 。

6. 一般看到的 patch 檔,都還會在前面加上 diff 指令,如下︰
diff -Naur old/file.cpp new/file.cpp
--- old/file.cpp 2007-11-23 10:07:59.737150770 +0000
+++ new/file.cpp 2007-11-23 10:08:06.477122545 +0000
@@ -2,4 +2,5 @@
void main()
{
std::cout << "Hello World!!\n";
+ std::cout << "This is my patch example.\n";
}

產生 patch 檔的方式其實相當的簡單,有機會可以多嘗試嘗試,哈。

2 comments:

Stella said...

A small team always participate and a good no of feedback can be collected from all the team members which may not happen in large teams. So, the project manager should be a scrum certified, who can better handle the team size. Get yourself scrum certified from http://www.scrumstudy.com which is a globally recognised certifying body for various certifcations such as scrum certification and Agile Certification

Craige said...

Organizations need to be assured the individuals that manage their projects can integrate methods to achieve sustainability goals and still achieve project specific objectives. Project Managers need credentials that validate their proficiency with these specialized qualities. PMP Certified and scrum certified project managers can learn, apply, and validate mastery of sustainability based project methods to meet these demands.