O que realmente faz o diff?

5

Parece que -u pode receber um argumento de número extra, mas eu não calo o manual,

Diz,

  -u, -U NUM, --unified[=NUM]
          output NUM (default 3) lines of unified context

Alguém nomeia um exemplo, por favor?

Eu não consegui encontrar um trabalho.

    
por daisy 26.04.2013 / 14:57

2 respostas

7

-u por si só resulta em formato "unificado" o número apenas altera o número de linhas vizinhas incluídas como contexto.

    
por 26.04.2013 / 15:04
2

Diretamente do artigo da Wikipedia sobre diff :

Unified context diffs were originally developed by Wayne Davison in August 1990 (in unidiff which appeared in Volume 14 of comp.sources.misc). Richard Stallman added unified diff support to the GNU Project's diff utility one month later, and the feature debuted in GNU diff 1.15, released in January 1991. GNU diff has since generalized the context format to allow arbitrary formatting of diffs.

The format starts with the same two-line header as the context format, except that the original file is preceded by "---" and the new file is preceded by "+++". Following this are one or more change hunks that contain the line differences in the file. The unchanged, contextual lines are preceded by a space character, addition lines are preceded by a plus sign, and deletion lines are preceded by a minus sign.

A hunk begins with range information and is immediately followed with the line additions, line deletions, and any number of the contextual lines. The range information is surrounded by double-at signs, and combines onto a single line what appears on two lines in the context format (above). The format of the range information line is as follows:

@@ -l,s +l,s @@ optional section heading

Em seguida, em todo o arquivo em que há alterações a serem feitas, você verá linhas como estas:

-check this dokument. On
+check this document. On

NOTA: Um - significa que está sendo removido e um + significa que está sendo adicionado.

O comando diff -U também pode receber um parâmetro adicional, um número, que indica quantas linhas de texto vizinho em torno do ponto em que a diferença entre os dois arquivos está ocorrendo. Isso é útil para entender melhor o contexto das diferenças.

Exemplo

file1

$ cat file1.txt 
The Rain in Spain by
Servants Poor Professor Higgins!
Poor Professor Higgins! Night and day
He slaves away! Oh, poor Professor Higgins!
All day long On his feet; Up and down until he's numb;
Doesn't rest; Doesn't eat;

file2

$ cat file2.txt 
The Rain in Spain by
added extra line here
Servants Poor Professor Higgins!
Poor Professor Higgins! Night and day
He slaves away! Oh, poor Professor Higgins!
All day long On his feat; Up and down untile he's numb;
Doesn't rest; Doesn't eat;

diff

$ diff -U 2 file1.txt file2.txt 
--- file1.txt   2013-04-26 09:39:13.496835363 -0400
+++ file2.txt   2013-04-26 09:38:04.838299195 -0400
@@ -1,6 +1,7 @@
 The Rain in Spain by
+added extra line here
 Servants Poor Professor Higgins!
 Poor Professor Higgins! Night and day
 He slaves away! Oh, poor Professor Higgins!
-All day long On his feet; Up and down until he's numb;
+All day long On his feat; Up and down untile he's numb;
 Doesn't rest; Doesn't eat;

Veja diff na wikipedia para mais detalhes.

    
por 26.04.2013 / 15:11

Tags