Patches to Packages

  1. Patches to Packages

有時候,為了讓Ubuntu能適當的運作,Ubuntu套件的維護者必須改變upstream的程式碼。 舉例來說,到upstream的patches還沒到release版本,或者是這個到upstream建置系統的改變只必須在Ubuntu上建置。

我們也可以直接更改upstream的原始碼,但是這樣接下來會給我們造成很多困擾。所以我們將這些改變保持在分散的patches,形式為diff檔。

在Debian package裡面有許多不同方式來處理patch,幸運的是這邊只使用一個標準的工具 - Quilt ,現在大部分的套件維護都是使用這個工具。

舉例來說,在Trusty的kamoso,

$ bzr branch ubuntu:trusty/kamoso

這個套件被放在debian/patches裡面。裡面有一個patch「 kubuntu_01_fix_qmax_on_armel.diffto 」,這個patch可以修復一個在ARM上面造成編譯失敗的bug。

這個patch可以從名字去知道他是幹嘛用的,數字的部分是為了讓如果有多個patch的話,可以排序用。而在這個案例來說Kubuntu團隊加了他們的prefix來顯示這個patch是從他們那邊來的而不是Debian。patches套用的順序是在debian/patches/series。

8.1. Patches with Quilt

在使用Quilt之前,你必須要跟它說要在哪裡找到patches。在你的「~/.bashrc」裡面加入

export QUILT_PATCHES=debian/patches

並且「source」:

$ . ~/.bashrc

預設所有的patches都已經自動的被UDD的checkout或者是下載的套件所套用了。你可以用如下的命令去檢查:

$ quilt applied
kubuntu_01_fix_qmax_on_armel.diff

如果你想要刪除這些patch的套用,你可以執行「pop」

$ quilt pop
Removing patch kubuntu_01_fix_qmax_on_armel.diff
Restoring src/kamoso.cpp

No patches applied

套用這個patch,你可以使用「push」:

$ quilt push
Applying patch kubuntu_01_fix_qmax_on_armel.diff
patching file src/kamoso.cpp

Now at patch kubuntu_01_fix_qmax_on_armel.diff

8.2. Adding a New Patch

如果想要新增一個patch,你必須要告訴Quilt去建立一個新的patch,然後告訴Quilt還有哪些檔案patch應該要去改動,編輯結束以後在將所有的更動從暫存給寫到patch裡面。

$ quilt new kubuntu_02_program_description.diff
Patch kubuntu_02_program_description.diff is now on top
$ quilt add src/main.cpp
File src/main.cpp added to patch kubuntu_02_program_description.diff
$ sed -i "s,Webcam picture retriever,Webcam snapshot program,"
src/main.cpp
$ quilt refresh
Refreshed patch kubuntu_02_program_description.diff

增加quilt的步驟非常的重要,如果你忘記這一步得話,這個檔案將不會被記錄到patch裡。完成上敘步驟後,這些改變就會在路徑 debian/patches/kubuntu_02_program_description.diff1裡面,接下來你就應該要新增這個新的檔案到打包過程裡:

$ bzr add debian/patches/kubuntu_02_program_description.diff
$ bzr add .pc/*
$ dch -i "Add patch kubuntu_02_program_description.diff to improve the program description"
$ bzr commit

Quilt將它的metadata放在資料夾 「.pc/」裡面,所以目前你必須要將其也加入到打包過程中。 一般來說如果你的patch不是來自upstream的話,你在套用它就要很小心。以上的例子就是改變了一個UI的字串,所以它會造成翻譯出問題。如果增加patch之前有任何疑問的話,就在問一下upstream作者。

8.3. Patch Headers

建議使用 DEP-3檔去標示每個patch,將其放在每個patch的前頭,底下是幾個你可以用的標頭:

Description: 描述這個patch的用途。必須用像是在debian/control的格式:第一行為簡短描述,第一個字元必須為小寫,下一行就為詳細描述,必須縮排。
Author: 這個patch的作者是誰 (i.e. “Jane Doe [email protected]”).
Origin: 這個patch最初是哪裡來的(i.e. “upstream”), 如果沒有Author的話
Bug-Ubuntu: 到Launchpad bug的連結,越簡短越好, (eg. https://bugs.launchpad.net/bugs/XXXXXXX). 如果在upstream或是 Debian bugtrackers也有bug的話,就增加 Bug 或是 Bug-Debian headers。
Forwarded: 這個patch是否是從upstream轉過來的,狀態有 “yes”, “no” 或是 “not-needed”.
Last-Update: 最後一次校定的日期(格式為 “YYYY-MM-DD”).

8.4. Upgrading to New Upstream Versions

想要升級成新版,你可以使用命令:「 bzr merge-upstream 」:

$ bzr merge-upstream --version 2.0.2 https://launchpad.net/ubuntu/+archive/primary/+files/kamoso_2.0.2.orig.tar.bz2

當你執行完這個指令後,所有的patch都將被取消,因為他們有可能會過期。會必須要更新成跟最新的upstream一樣或者是就要一起被刪掉,為了要確認這個問題,套用這個patch:

$ quilt push
Applying patch kubuntu_01_fix_qmax_on_armel.diff
patching file src/kamoso.cpp
Hunk #1 FAILED at 398.
1 out of 1 hunk FAILED -- rejects in file src/kamoso.cpp
Patch kubuntu_01_fix_qmax_on_armel.diff can be reverse-applied

如果它可以被「reverse-applied」代表這個patch已經被upstream套用了,所以我們就可以刪除這個patch。

$ quilt delete kubuntu_01_fix_qmax_on_armel
Removed patch kubuntu_01_fix_qmax_on_armel.diff

然後繼續下一步:

$ quilt push
Applied kubuntu_02_program_description.diff

接下來最好執行一下「refresh」,這將會更新patch相關到已經改變的upstream source:

$ quilt refresh
Refreshed patch kubuntu_02_program_description.diff

然後commit:

$ bzr commit -m "new upstream version"

8.5. Making A Package Use Quilt

現在的package預設都是使用Quilt, 檢查一下在 debian/source/formatto確定是否為3.0 (quilt). 比較舊的版本使用source format 1.0,這邊就需要明確的使用Quilt,通常必須在debian/rules裡面包含一個makefile。

8.6. Configuring Quilt

你可以使用「 ~/.quiltrc 」來組態quilt。底下有幾個選項在debian/packages底下使用quilt非常的有用:

# Set the patches directory
QUILT_PATCHES="debian/patches"
# Remove all useless formatting from the patches
QUILT_REFRESH_ARGS="-p ab --no-timestamps --no-index"
# The same for quilt diff command, and use colored output
QUILT_DIFF_ARGS="-p ab --no-timestamps --no-index --color=auto"

8.7. Other Patch Systems

其他的patch系統包含了「dpatch」和「cdbs simple-patchsys」,這些都跟Quilt一樣,將patch放在debian/patches裡面,但是使用不一樣的指令去套用(apply),取消套用(un-apply)或是創建patches。

你可以在一個package裡面藉由使用「what-patch」(從ubuntu-dev-tools package裡面)去找出他是使用哪一個patch系統。當然你也可以使用之前用過的「 edit-patch 」。在比較舊的package change將會直接在source,放在diff.gz裡面。這將會讓他很難升級到新的upstream 版本或是在patch之間切換,所以最好避免這種用法。

千萬不要在還沒有跟Debian維護者或是Ubuntu相關團隊討論之下就改變package的patch系統,但是如果本來就沒有的話,就直接使用Quilt吧。


Ref:
http://packaging.ubuntu.com/html/patches-to-packages.html

results matching ""

    No results matching ""