github.com/haya14busa/reviewdog@v0.0.0-20180723114510-ffb00ef78fd3/diff/diff.go (about)

     1  // Package diff provides a utility to parse unified diff.
     2  // https://en.wikipedia.org/wiki/Diff_utility#Unified_format
     3  package diff
     4  
     5  // FileDiff represents a unified diff for a single file.
     6  //
     7  // Example:
     8  //   --- oldname	2009-10-11 15:12:20.000000000 -0700
     9  //   +++ newname	2009-10-11 15:12:30.000000000 -0700
    10  type FileDiff struct {
    11  	// the old path of the file
    12  	PathOld string
    13  	// the new path of the file
    14  	PathNew string
    15  
    16  	// the old timestamp (empty if not present)
    17  	TimeOld string
    18  	// the new timestamp (empty if not present)
    19  	TimeNew string
    20  
    21  	Hunks []*Hunk
    22  
    23  	// extended header lines (e.g., git's "new mode <mode>", "rename from <path>", index fb14f33..c19311b 100644, etc.)
    24  	Extended []string
    25  
    26  	// TODO: we may want `\ No newline at end of file` information for both the
    27  	// old and new file.
    28  }
    29  
    30  // Hunk represents change hunks that contain the line differences in the file.
    31  //
    32  // Example:
    33  //   @@ -1,3 +1,4 @@ optional section heading
    34  //    unchanged, contextual line
    35  //   -deleted line
    36  //   +added line
    37  //   +added line
    38  //    unchanged, contextual line
    39  //
    40  //  -1 -> the starting line number of the old file
    41  //  3  -> the number of lines the change hunk applies to for the old file
    42  //  +1 -> the starting line number of the new file
    43  //  4  -> the number of lines the change hunk applies to for the new file
    44  type Hunk struct {
    45  	// the starting line number of the old file
    46  	StartLineOld int
    47  	// the number of lines the change hunk applies to for the old file
    48  	LineLengthOld int
    49  
    50  	// the starting line number of the new file
    51  	StartLineNew int
    52  	// the number of lines the change hunk applies to for the new file
    53  	LineLengthNew int
    54  
    55  	// optional section heading
    56  	Section string
    57  
    58  	// the body lines of the hunk
    59  	Lines []*Line
    60  }
    61  
    62  // LineType represents the type of diff line.
    63  type LineType int
    64  
    65  const (
    66  	// LineUnchanged represents unchanged, contextual line preceded by ' '
    67  	LineUnchanged LineType = iota
    68  	// LineAdded represents added line preceded by '+'
    69  	LineAdded
    70  	// LineDeleted represents deleted line preceded by '-'
    71  	LineDeleted
    72  )
    73  
    74  // Line represents a diff line.
    75  type Line struct {
    76  	// type of this line
    77  	Type LineType
    78  	// the line content without a preceded character (' ', '+', '-')
    79  	Content string
    80  
    81  	// the line in the file to a position in the diff.
    82  	// the number of lines down from the first "@@" hunk header in the file.
    83  	// e.g. The line just below the "@@" line is position 1, the next line is
    84  	// position 2, and so on. The position in the file's diff continues to
    85  	// increase through lines of whitespace and additional hunks until a new file
    86  	// is reached. It's equivalent to the `position` field of input for cooment
    87  	// API of GitHub https://developer.github.com/v3/pulls/comments/#input
    88  	LnumDiff int
    89  
    90  	// the line number of the old file for LineUnchanged and LineDeleted
    91  	// type. 0 for LineAdded type.
    92  	LnumOld int
    93  
    94  	// the line number of the new file for LineUnchanged and LineAdded type.
    95  	// 0 for LineDeleted type.
    96  	LnumNew int
    97  }