作者 | Locez
摘要
在 Linux 的日常使用中,我們經常需要修改一些配置檔案,然而在軟體升級以後,經常會面臨配置更新後與原配置部分不相容的問題(當然我們更多的可能是來製作軟體升級的補丁)。在這種情況下我們通常有兩種選擇:
sed
、awk
等工具配合改動diff
與 patch
製作增量補丁的方式改動本文主要透過一個升級awesome
配置的例子,對第三種方法進行介紹和講解。
diff 介紹
diff
是一個檔案比較工具,可以逐行比較兩個檔案的不同,其中它有三種輸出方式,分別是 normal
, context
以及 unified
。區別如下:
normal
方式為預設輸出方式,不需要加任何引數context
相較於 normal
樣式的簡單輸出,contetx
樣式會輸出修改過部分的背景關係,預設是前後 3 行。使用引數 -c
unified
合併背景關係樣式則為新的背景關係輸出樣式,同樣為前後 3 行,只不過把背景關係合併了顯示了。使用引數 -u
註:本文主要介紹 unified
樣式
其他常用引數:
-r
遞迴處理目錄-N
將缺失的檔案當作空白檔案處理diff 語法與檔案格式
diff [options] old new
先來看一個簡單的例子:
$ cat test1
linux
linux
linux
linux
$ cat test2
locez
linux
locez
linux
此時輸入 diff -urN test1 test2
會輸出以下資訊:
--- test1 2018-05-12 18:39:41.508375114 +0800
+++ test2 2018-05-12 18:41:00.124031736 +0800
@@ -1,4 +1,4 @@
+locez
linux
-linux
-linux
+locez
linux
先看前面 2 行,這兩行為檔案的基本資訊,---
開頭為改變前的檔案,+++
開頭為更新後的檔案。
--- test1 2018-05-12 18:39:41.508375114 +0800
+++ test2 2018-05-12 18:41:00.124031736 +0800
第三行為背景關係描述塊,其中 -1,4
為舊檔案的 4 行背景關係,+1,4
為新檔案的:
@@ -1,4 +1,4 @@
而具體到塊裡面的內容,前面有 -
號的則為刪除,有 +
號為新增,不帶符號則未做改變,僅僅是背景關係輸出。
patch 介紹
patch
是一個可以將 diff
生成的補丁應用到源檔案,生成一個打過補丁版本的檔案。語法:
patch [oiption] [originalfile [patchfile]]
常用引數:
-i
指定補丁檔案-pNum
在 diff
生成的補丁中,第一二行是檔案資訊,其中檔案名是可以包含路徑的,例如 --- /tmp/test1 2018-05-12 18:39:41.508375114 +0800
其中 -p0
代表完整的路徑 /tmp/test1
,而 -p1
則指 tmp/test1
,-pN
依此類推-E
刪除應用補丁後為空檔案的檔案-o
輸出到一個檔案而不是直接改寫檔案應用
awesome 桌面 3.5 與 4.0 之間的升級是不相容的,所以在升級完 4.0 以後,awesome 桌面部分功能無法使用,因此需要遷移到新配置,接下來則應用 diff
與 patch
實現遷移,當然你也可以單純使用 diff
找出不同,然後手動修改新配置。
現在有以下幾個檔案:
rc.lua.3.5
3.5 版本的預設配置檔案,未修改rc.lua.myconfig
基於 3.5 版本的個人配置檔案rc.lua.4.2
4.2 新預設配置,未修改思路為利用 diff
提取出個人配置與 3.5 預設配置檔案的增量補丁,然後把補丁應用在 4.2 的檔案上實現遷移。
製作補丁
$ diff -urN rc.lua.3.5 rc.lua.myconfig > mypatch.patch
應用補丁
$ patch rc.lua.4.2 -i mypatch.patch -o rc.lua
patching file rc.lua (read from rc.lua.4.2)
Hunk #1 FAILED at 38.
Hunk #2 FAILED at 55.
Hunk #3 succeeded at 101 with fuzz 1 (offset 5 lines).
Hunk #4 succeeded at 276 with fuzz 2 (offset 29 lines).
2 out of 4 hunks FAILED -- saving rejects to file rc.lua.rej
顯然應用沒有完全成功,其中在 38 行以及 55 行應用失敗,並記錄在 rc.lua.rej
裡。
$ cat rc.lua.rej
--- rc.lua.3.5 2018-05-12 19:15:54.922286085 +0800
+++ rc.lua.myconfig 2018-05-12 19:13:35.057911463 +0800
@@ -38,10 +38,10 @@
-- {{{ Variable definitions
-- Themes define colours, icons, font and wallpapers.
-beautiful.init("@AWESOME_THEMES_PATH@/default/theme.lua")
+beautiful.init("~/.config/awesome/default/theme.lua")
-- This is used later as the default terminal and editor to run.
-terminal = "xterm"
+terminal = "xfce4-terminal"
editor = os.getenv("EDITOR") or "nano"
editor_cmd = terminal .. " -e " .. editor
@@ -55,18 +55,18 @@
-- Table of layouts to cover with awful.layout.inc, order matters.
local layouts =
{
- awful.layout.suit.floating,
- awful.layout.suit.tile,
- awful.layout.suit.tile.left,
- awful.layout.suit.tile.bottom,
- awful.layout.suit.tile.top,
+-- awful.layout.suit.floating,
+-- awful.layout.suit.tile,
+-- awful.layout.suit.tile.left,
+-- awful.layout.suit.tile.bottom,
+-- awful.layout.suit.tile.top,
awful.layout.suit.fair,
awful.layout.suit.fair.horizontal,
awful.layout.suit.spiral,
awful.layout.suit.spiral.dwindle,
awful.layout.suit.max,
awful.layout.suit.max.fullscreen,
- awful.layout.suit.magnifier
+-- awful.layout.suit.magnifier
}
-- }}}
這裡是主題,終端,以及常用佈局的個人設定。
修正補丁
再次透過對比補丁檔案與 4.2 檔案,發現 38 行區塊是要刪除的東西不匹配,而 55 行區塊則是背景關係與要刪除的內容均不匹配,導致不能應用補丁,於是手動修改補丁
$ vim mypatch.patch
--- rc.lua.3.5 2018-05-12 19:15:54.922286085 +0800
+++ rc.lua.myconfig 2018-05-12 19:13:35.057911463 +0800
@@ -38,10 +38,10 @@
-- {{{ Variable definitions
-- Themes define colours, icons, font and wallpapers.
-beautiful.init(gears.filesystem.get_themes_dir() .. "default/theme.lua")
+beautiful.init("~/.config/awesome/default/theme.lua")
-- This is used later as the default terminal and editor to run.
-terminal = "xterm"
+terminal = "xfce4-terminal"
editor = os.getenv("EDITOR") or "nano"
editor_cmd = terminal .. " -e " .. editor
@@ -55,18 +55,18 @@
-- Table of layouts to cover with awful.layout.inc, order matters.
awful.layout.layouts = {
- awful.layout.suit.floating,
- awful.layout.suit.tile,
- awful.layout.suit.tile.left,
- awful.layout.suit.tile.bottom,
- awful.layout.suit.tile.top,
+-- awful.layout.suit.floating,
+-- awful.layout.suit.tile,
+-- awful.layout.suit.tile.left,
+-- awful.layout.suit.tile.bottom,
+-- awful.layout.suit.tile.top,
awful.layout.suit.fair,
awful.layout.suit.fair.horizontal,
awful.layout.suit.spiral,
awful.layout.suit.spiral.dwindle,
awful.layout.suit.max,
awful.layout.suit.max.fullscreen,
- awful.layout.suit.magnifier,
+-- awful.layout.suit.magnifier,
awful.layout.suit.corner.nw,
-- awful.layout.suit.corner.ne,
-- awful.layout.suit.corner.sw,
....
....
輸出省略顯示,有興趣的讀者可以仔細與rc.lua.rej
檔案對比,看看筆者是怎樣改的。
再次應用補丁
$ patch rc.lua.4.2 -i mypatch.patch -o rc.lua
patching file rc.lua (read from rc.lua.4.2)
Hunk #1 succeeded at 41 (offset 3 lines).
Hunk #2 succeeded at 57 with fuzz 2 (offset 2 lines).
Hunk #3 succeeded at 101 with fuzz 1 (offset 5 lines).
Hunk #4 succeeded at 276 with fuzz 2 (offset 29 lines).
$ cp rc.lua ~/.config/awesome/rc.lua ### 打完補丁直接使用
總結
diff
與 patch
配合使用,能當增量備份,而且還可以將補丁分發給他人使用,而且在日常的軟體包打補丁也具有重要的意義,特別是核心補丁或者一些驅動補丁,打補丁遇到錯誤時候可以嘗試自己修改,已滿足自身特殊要求,修改的時候一定要抓住 2 個非常重要的要素: