2013-07-18 27 views
1

我的問題是類似Interactive program to selectively exclude parts of a diff file - 但有點不同......區分感知編輯器,可以處理修補程序塊編輯(包括刪除行)?

說我有以下的「原生態」的文件:

$ cat test-orig.h 
int varA; 
int varB; 
int varC; 
int varD; 
int varE; 

...說我做了這些改變成「新」文件:

$ cat test-newmodified.h 
int varA; 
int varB; 
int varC; 
// a couple of lines 
// of useless comments 
int var_extra1; 
int var_extra2; 
int varD; 
int varE; 

然後兩者之間的差異是:

$ diff -Naur test-orig.h test-newmodified.h | tee test.patch 
--- test-orig.h 2013-07-18 19:21:25.741027138 +0200 
+++ test-newmodified.h 2013-07-18 19:21:19.916998200 +0200 
@@ -1,5 +1,9 @@ 
int varA; 
int varB; 
int varC; 
+// a couple of lines 
+// of useless comments 
+int var_extra1; 
+int var_extra2; 
int varD; 
int varE; 

那麼假設我有一個這樣獲得的補丁文件(有很多人),我想刪除「無用的評論」。從diff生成的補丁文件中刪除整個大塊通常很容易 - 但是如果我只想刪除一些行,那麼diff計數器(上面的@@ -1,5 +1,9 @@)也必須進行修改。再說了,如果註釋行是在文件中刪除,該差異將被:

$ diff -Naur test-orig.h test-newmodified-nc.h 
--- test-orig.h 2013-07-18 19:21:25.741027138 +0200 
+++ test-newmodified-nc.h 2013-07-18 19:26:30.898540270 +0200 
@@ -1,5 +1,7 @@ 
int varA; 
int varB; 
int varC; 
+int var_extra1; 
+int var_extra2; 
int varD; 
int varE; 

...也就是說,有意見,@@ -1,5 +1,9 @@計數器 - 成了現在,沒有註釋,@@ -1,5 +1,7 @@

如果我只是刪除補丁test.patch註釋行(並保存編輯爲test-edit.patch),並且不更新計數器,然後我得到:

$ patch -p0 <test-edit.patch 
patching file test-orig.h 
patch: **** malformed patch at line 10: 

......而這樣的補丁沒有按沒有得到應用。那麼,如果我只是改變了+1,9+1.7test-edit.patch,那麼它適用乾淨:

$ patch -p0 <test-edit.patch 
patching file test-orig.h 

...和預期test-orig.h改變(不註釋行)。

因此,給定一個補丁文件,其中包含與文件有關的所有宏塊都包含在內 - 是否有一個(GUI)(文本)編輯器,它是diff意識到(至少,統一比較),以便:當來自hunk(或整個hunk)被刪除,它會自動更新diff計數器 - 這樣編輯後的補丁文件仍然會乾淨地應用於原始文件?

回答

1

好了,我現在知道我至少可以使用emacs此,感謝這樣的回答:#9740489 How to edit a diff/patch file cleanly ? Are there any patch file editors?

如果你用Emacs打開一個差異文件,並把在「差異」模式下,您可以編輯其實編輯的補丁,它會更新大塊標記在一個巧妙的方法

注意,我的系統(Ubuntu的清醒),就sudo apt-get install emacs會,在安裝後,使emacs命令最終符號鏈接到emacs23-x,它運行在自己的窗口(不在終端) 。所以我可以撥打電話:

$ emacs test.patch 

...和Emacs將在「差異」模式自動啓動:

emacs-diff

然後你可以選擇使用鼠標線,但他們使用DEL/Backspace鍵不能刪除 - 你不得不削減用Ctrl-W選擇(見In Emacs, how do I cut, paste, and copy a block of text? - Knowledge Base)。從截圖中可以看出,標記會自動更新。另外,請勿使用M-x diff(請參閱Comparing Files - GNU Emacs Manual;在我的機器上,Mx爲Alt-X)進入「diff」模式 - 它會開始詢問「原始」和「新」輸入文件路徑,以在其上執行diff,這不是我們想要的(因爲補丁文件已經加載)。最後,圖形用戶界面菜單具有文件/另存爲(和它的快捷鍵C-x C-w) - 點擊圖形用戶界面菜單引發了通常的文件對話框,其中編輯後的文件可以在保存時重命名。

儘管如此,想知道是否有替代這個...

+0

Emacs是操作系統和Linux爲內核。有可能你不會找到更好的解決方案。 – devnull

相關問題