@@ -99,9 +99,9 @@ func GetRepoRawDiffForFile(repo *Repository, startCommit, endCommit string, diff
99
99
return nil
100
100
}
101
101
102
- // ParseDiffHunkString parse the diffhunk content and return
103
- func ParseDiffHunkString (diffhunk string ) (leftLine , leftHunk , rightLine , righHunk int ) {
104
- ss := strings .Split (diffhunk , "@@" )
102
+ // ParseDiffHunkString parse the diff hunk content and return
103
+ func ParseDiffHunkString (diffHunk string ) (leftLine , leftHunk , rightLine , rightHunk int ) {
104
+ ss := strings .Split (diffHunk , "@@" )
105
105
ranges := strings .Split (ss [1 ][1 :], " " )
106
106
leftRange := strings .Split (ranges [0 ], "," )
107
107
leftLine , _ = strconv .Atoi (leftRange [0 ][1 :])
@@ -112,14 +112,19 @@ func ParseDiffHunkString(diffhunk string) (leftLine, leftHunk, rightLine, righHu
112
112
rightRange := strings .Split (ranges [1 ], "," )
113
113
rightLine , _ = strconv .Atoi (rightRange [0 ])
114
114
if len (rightRange ) > 1 {
115
- righHunk , _ = strconv .Atoi (rightRange [1 ])
115
+ rightHunk , _ = strconv .Atoi (rightRange [1 ])
116
116
}
117
117
} else {
118
- log .Debug ("Parse line number failed: %v" , diffhunk )
118
+ log .Debug ("Parse line number failed: %v" , diffHunk )
119
119
rightLine = leftLine
120
- righHunk = leftHunk
120
+ rightHunk = leftHunk
121
121
}
122
- return leftLine , leftHunk , rightLine , righHunk
122
+ if rightLine == 0 {
123
+ // FIXME: GIT-DIFF-CUT-BUG search this tag to see details
124
+ // this is only a hacky patch, the rightLine&rightHunk might still be incorrect in some cases.
125
+ rightLine ++
126
+ }
127
+ return leftLine , leftHunk , rightLine , rightHunk
123
128
}
124
129
125
130
// Example: @@ -1,8 +1,9 @@ => [..., 1, 8, 1, 9]
@@ -270,6 +275,12 @@ func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLi
270
275
oldNumOfLines ++
271
276
}
272
277
}
278
+
279
+ // "git diff" outputs "@@ -1 +1,3 @@" for "OLD" => "A\nB\nC"
280
+ // FIXME: GIT-DIFF-CUT-BUG But there is a bug in CutDiffAroundLine, then the "Patch" stored in the comment model becomes "@@ -1,1 +0,4 @@"
281
+ // It may generate incorrect results for difference cases, for example: delete 2 line add 1 line, delete 2 line add 2 line etc, need to double check.
282
+ // For example: "L1\nL2" => "A\nB", then the patch shows "L2" as line 1 on the left (deleted part)
283
+
273
284
// construct the new hunk header
274
285
newHunk [headerLines ] = fmt .Sprintf ("@@ -%d,%d +%d,%d @@" ,
275
286
oldBegin , oldNumOfLines , newBegin , newNumOfLines )
0 commit comments