What does “@@ -1 +1 @@” mean in Git's diff output?

两盒软妹~` 提交于 2019-11-27 06:11:07
Todd A. Jacobs

It's a unified diff hunk identifier. This is documented by GNU Diffutils.

The unified output format starts with a two-line header, which looks like this:

--- from-file from-file-modification-time
+++ to-file to-file-modification-time

The time stamp looks like 2002-02-21 23:30:39.942229878 -0800 to indicate the date, time with fractional seconds, and time zone. The fractional seconds are omitted on hosts that do not support fractional time stamps.

You can change the header's content with the --label=label option; see See Alternate Names.

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.
Ciro Santilli 新疆改造中心996ICU六四事件

Simple example analysis

The format is basically the same the diff -u unified diff.

For instance:

diff -u <(seq -w 16) <(seq -w 16 | grep -Ev '^(02|03|14|15)$')

Here we removed lines 2, 3, 14 and 15. Output:

@@ -1,6 +1,4 @@
 01
-02
-03
 04
 05
 06
@@ -11,6 +9,4 @@
 11
 12
 13
-14
-15
 16

@@ -1,6 +1,4 @@ means:

  • -1,6: this piece corresponds to line 1 to 6 of the first file:

    01
    02
    03
    04
    05
    06
    

    - means "old", as we usually invoke it as diff -u old new.

  • +1,4 says that this piece corresponds to line 1 to 4 of the second file.

    + means "new".

    We only have 4 lines instead of 6 because 2 lines were removed! The new hunk is just:

    01
    04
    05
    06
    

@@ -11,6 +9,4 @@ for the second hunk is analogous:

  • on the old file, we have 6 lines, starting at line 11 of the old file:

    11
    12
    13
    14
    15
    16
    
  • on the new file, we have 4 lines, starting at line 9 of the new file:

    11
    12
    13
    16
    

    Note that line 11 is the 9th line of the new file because we have already removed 2 lines on the previous hunk: 2 and 3.

Hunk header

Depending on your git version and configuration, you can also get a code line next to the @@ line, e.g. the func1() { in:

@@ -4,7 +4,6 @@ func1() {

This can also be obtained with the -p flag of plain diff.

Example: old file:

func1() {
    1;
    2;
    3;
    4;
    5;
    6;
    7;
    8;
    9;
}

If we remove line 6, the diff shows:

@@ -4,7 +4,6 @@ func1() {
     3;
     4;
     5;
-    6;
     7;
     8;
     9;

Note that this is not the correct line for func1: it skipped lines 1 and 2.

This awesome feature often tells exactly to which function or class each hunk belongs, which is very useful to interpret the diff.

How the algorithm to choose the header works exactly is discussed at: Where does the excerpt in the git diff hunk header come from?

It's the current hunk range information stating on which line numbers this diff hunk starts and ends.

Read http://en.wikipedia.org/wiki/Diff#Unified_format for an in-depth explanation.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!