以前曾提到過,在發布軟件補丁時很少(可能從不)使用正規和并排的塊格式.但diff產生的上下文(context)或統一(unified)的塊格式是創建補丁所采用的格式.為了產生上下文的差異文件(它們稱為context
diff的原因是它們顯示出了有差別的行的上下文內容),可使用diff的-c或-C[num]選項.
提示:和大多數GNU程序一樣,diff也支持長選項,也就是以兩個兩字符"--"開頭,后面跟著更容易記憶的名字的選項,例如,創建一個上下文diff文件的長選項是"--context=[num]".
上下文輸出舉例
$diff -c hello.c howdy.c
*** hello.c Web Aug 9 21:02:42 2000
--- howdy.c Web Aug 9 21:04:30 2000
*************
*** 1,12 ****
#include <stdio.h>
int main(void)
{
! char msg[ ] = "Hello, Linux programmer!";
puts(msg);
! printf("Here you are, using diff.\n");
! return 0;
}
--- 1,13 ---
#include <stdio.h>
+ #include <stdlib.h>
int main(void)
{
! char msg[] = "Hello, Linux programmer, from howdy.c!");
puts(msg);
! printf("howdy.c says, `Here you are, using diff.`\n");
! exit(EXIT_SUCCESS);
}
$
上下文塊的格式采用以下一般形式:
*** srcfile srcfile_timestamp
--- dstfile dstfile_timestamp
****************
*** srcfile_line_range ***
srcfile line
--- dstfile line_line_range
dstfile line
dstfile line...
. + -----向srcfile添加一行以創建dstfile
. - -----從srcfile刪除一行以創建dstfile
. ! -----在srcfile改變一行以創建dstfile.srcfile中標記"!"的每一行或一段,在dstfile中相應的每一行或一段也標記"!".
每一塊(hunk)都用一長串最多15個星號和下一塊(hunk)分隔開來.
提示:和大多數GNU程序一樣,diff也支持長選項,也就是以兩個兩字符"--"開頭,后面跟著更容易記憶的名字的選項,例如,創建一個上下文diff文件的長選項是"--context=[num]".
上下文輸出舉例
$diff -c hello.c howdy.c
*** hello.c Web Aug 9 21:02:42 2000
--- howdy.c Web Aug 9 21:04:30 2000
*************
*** 1,12 ****
#include <stdio.h>
int main(void)
{
! char msg[ ] = "Hello, Linux programmer!";
puts(msg);
! printf("Here you are, using diff.\n");
! return 0;
}
--- 1,13 ---
#include <stdio.h>
+ #include <stdlib.h>
int main(void)
{
! char msg[] = "Hello, Linux programmer, from howdy.c!");
puts(msg);
! printf("howdy.c says, `Here you are, using diff.`\n");
! exit(EXIT_SUCCESS);
}
$
上下文塊的格式采用以下一般形式:
*** srcfile srcfile_timestamp
--- dstfile dstfile_timestamp
****************
*** srcfile_line_range ***
srcfile line
--- dstfile line_line_range
dstfile line
dstfile line...
. + -----向srcfile添加一行以創建dstfile
. - -----從srcfile刪除一行以創建dstfile
. ! -----在srcfile改變一行以創建dstfile.srcfile中標記"!"的每一行或一段,在dstfile中相應的每一行或一段也標記"!".
每一塊(hunk)都用一長串最多15個星號和下一塊(hunk)分隔開來.