Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Diff file syntax

Tags:

parsing

diff

I am currently playing around with parsing diff files, and have yet to come across a solid documentation on diff files.

I am especially interested in specifications. E.g. I don't really understand the lines that look like this (at the beginning of each changed code block):

@@ -296,7 +296,8 @@

I know they have to do with line numbers, and how much lines have changed, but I wasn't really able to figure out the details so far.

What is the syntax of the output diff files (at least, the main parts)?

like image 719
Franz Avatar asked Oct 31 '10 00:10

Franz


2 Answers

Check out the documentation for GNU diffutils. There you'll find this section:

Next come one or more hunks of differences; each hunk shows one area where the files differ. Unified format hunks look like this:

@@ from-file-line-numbers to-file-line-numbers @@
  line-from-either-file
  line-from-either-file...

If a hunk contains just one line, only its start line number appears. Otherwise its line numbers look like ‘start,count’. An empty hunk is considered to start at the line that follows the hunk.

If a hunk and its context contain two or more lines, its line numbers look like ‘start,count’. Otherwise only its end line number appears. An empty hunk is considered to end at the line that precedes the hunk.

The lines common to both files begin with a space character. The lines that actually differ between the two files have one of the following indicator characters in the left print column:

‘+’ A line was added here to the first file.

‘-’ A line was removed here from the first file.

like image 124
steinar Avatar answered Oct 20 '22 09:10

steinar


The Wikipedia page on the diff utility describes the format pretty well.

like image 30
You Avatar answered Oct 20 '22 11:10

You