這篇文章主要介紹一下git-am 和 format-patch 的使用。 因為在git使用當中, 會有很多時候別人(供應商或者其他的開發人員)發過來一系列的patch,這些patch通常的是類似這樣的名字: 0001--JFFS2-community-fix-with-not-use-OOB.patch0002--Community-patch-for-Fix-mount-error-in.patch0003--partial-low-interrupt-latency-mode-for-ARM113.patch0004--for-the-global-I-cache-invalidation-ARM11.patch0005--1-arm-Add-more-cache-memory-types-macr.patch0006--2-Port-imx-3.3.0-release-to-2.6.28.patch0007--3-Add-MX25-support.patch0008--Move-asm-arch-headers-to-linux-inc-dir.patch0009--1-regulator-allow-search-by-regulator.patch 裡麵包含了提交的日誌,作者,日期等資訊。你想做的是把這些patch引入到你的 程式碼程式庫中,最好是也可以把日誌也引入進來, 方便以後維護用。傳統的打patch方式是 patch -p1 < 0001--JFFS2-community-fix-with-not-use-OOB.patch 這樣來打patch,但是這樣會把這些有用的資訊丟失。 由於這些patch顯然是用git format-patch來產生的,所以用git的工具應該就可以很好的做好。 git-am 就是作這件事情。 在使用git-am之前, 你要首先git am –abort 一次,來放棄掉以前的am資訊,這樣才可以進行一次全新的am。 不然會遇到這樣的錯誤。
.git/rebase-apply still exists but mbox given. git-am 可以一次合并一個檔案,或者一個目錄下所有的patch,或者你的郵箱目錄下的patch. 下面舉兩個例子:
- 你現在有一個code base: small-src, 你的patch檔案放在~/patch/0001-trival-patch.patch
cd small-srcgit-am ~/patch/0001-trival-patch.patch 如果成功patch上去, 你就可以去喝杯茶了。 如果失敗了, git 會提示錯誤, 比如: error: patch failed: android/mediascanner.cpp:452error: android/mediascanner.cpp: patch does not apply 這樣你就需要先看看patch, 然後改改錯誤的這個檔案,讓這個patch能夠patch上去。
- 你有一堆patch, 名字是上面提到的那一堆patch, 你把他們放在~/patch-set/目錄下(路徑隨意)
cd opencoregit am ~/patch-set/*.patch (這裡git就會按照檔案名稱的順序一次am這些patch) 如果一切順利, 你所有的patch都OK了, 你又Lucky了。 不過不順利的時候十有八九,如果git am中間遇到了patch,am就會停到打這個 patch的地方, 告訴你是哪個patch打不上去。 比如我現在有一個檔案file,有兩個patch. file 的內容是 the textmore text 兩個patch分別是: 0001-add-line.patch: From 48869ccbced494e05738090afa5a54f2a261df0f Mon Sep 17 00:00:00 2001From: zhangjiejing <zhangjiejing@zhangjiejing-desktop.(none)>Date: Thu, 22 Apr 2010 13:04:34 +0800Subject: [PATCH 1/2] add line--- file | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)diff --git a/file b/fileindex 067780e..685f0fa 100644--- a/file+++ b/file@@ -3,3 +3,5 @@ file: some text more text++add line--1.6.3.3 0002-change-line.patch: From f756e1b3a87c216b7e0afea9d15badd033171578 Mon Sep 17 00:00:00 2001From: zhangjiejing <zhangjiejing@zhangjiejing-desktop.(none)>Date: Thu, 22 Apr 2010 13:05:19 +0800Subject: [PATCH 2/2] change line--- file | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)diff --git a/file b/fileindex 685f0fa..7af7852 100644--- a/file+++ b/file@@ -1,6 +1,6 @@ file:-some text+Change line text more text--1.6.3.3 運行 git am *.patch 來merge這些patch, 報錯, Patch failed at 0001 add line這樣我們看0001這 個patch,原來patch需要的是some text, 而file裡面是the text, 所以我們用編 輯器把這行改成some text, vi filegit apply 0001-add-line.patchgit add filegit am --resolved 在解決完衝突以後, 比如用git add來讓git知道你已經解決完衝突了。
- 如果你發現這個衝突是無法解決的, 要撤銷整個am的東西。 可以運行git am –abort,
- 如果你想只是忽略這一個patch,可以運行git am –skip來跳過這個patch.
git format-patch經驗 A git log commit c commit b commit a commit init B git log commit init ============= A,B兩人代碼共有commit init 現在A改動到a=>b=>c B想用補丁來升級 A: git format-patch init..c ==>產生三個補丁 001-commit-a.patch 對應從commit init出發升到a 002-commit-b.patch 對應從commit a出發升到b 003-commit-c.patch 對應從commit b出發升到c 即git format-patch x..y 是從共有的x出發一路一個一個升到y (x,y] ================= B git am *.patch git會自動按檔案名稱一級級升上去的 |