Skip to content

Commit 806f915

Browse files
committed
fix getDiffLineSectionInfo
1 parent 8f8ca4e commit 806f915

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

modules/git/diff.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ func GetRepoRawDiffForFile(repo *Repository, startCommit, endCommit string, diff
9999
return nil
100100
}
101101

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, "@@")
105105
ranges := strings.Split(ss[1][1:], " ")
106106
leftRange := strings.Split(ranges[0], ",")
107107
leftLine, _ = strconv.Atoi(leftRange[0][1:])
@@ -112,14 +112,21 @@ func ParseDiffHunkString(diffhunk string) (leftLine, leftHunk, rightLine, righHu
112112
rightRange := strings.Split(ranges[1], ",")
113113
rightLine, _ = strconv.Atoi(rightRange[0])
114114
if len(rightRange) > 1 {
115-
righHunk, _ = strconv.Atoi(rightRange[1])
115+
rightHunk, _ = strconv.Atoi(rightRange[1])
116116
}
117117
} else {
118-
log.Debug("Parse line number failed: %v", diffhunk)
118+
log.Debug("Parse line number failed: %v", diffHunk)
119119
rightLine = leftLine
120-
righHunk = leftHunk
120+
rightHunk = leftHunk
121121
}
122-
return leftLine, leftHunk, rightLine, righHunk
122+
if rightLine == 0 {
123+
// "git diff" outputs 2 different formats for the same change "OLD" => "A\nB\nC"
124+
// * "@@ -1 +1,3 @@": the expected result
125+
// * "@@ -1,1 +0,4 @@": the "0" means "insert before the first line"
126+
rightLine++
127+
rightHunk--
128+
}
129+
return leftLine, leftHunk, rightLine, rightHunk
123130
}
124131

125132
// Example: @@ -1,8 +1,9 @@ => [..., 1, 8, 1, 9]

services/gitdiff/gitdiff.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func (d *DiffLine) GetExpandDirection() DiffLineExpandDirection {
179179
}
180180

181181
func getDiffLineSectionInfo(treePath, line string, lastLeftIdx, lastRightIdx int) *DiffLineSectionInfo {
182-
leftLine, leftHunk, rightLine, righHunk := git.ParseDiffHunkString(line)
182+
leftLine, leftHunk, rightLine, rightHunk := git.ParseDiffHunkString(line)
183183

184184
return &DiffLineSectionInfo{
185185
Path: treePath,
@@ -188,7 +188,7 @@ func getDiffLineSectionInfo(treePath, line string, lastLeftIdx, lastRightIdx int
188188
LeftIdx: leftLine,
189189
RightIdx: rightLine,
190190
LeftHunkSize: leftHunk,
191-
RightHunkSize: righHunk,
191+
RightHunkSize: rightHunk,
192192
}
193193
}
194194

@@ -856,6 +856,7 @@ func parseHunks(ctx context.Context, curFile *DiffFile, maxLines, maxLineCharact
856856
lastLeftIdx = -1
857857
curFile.Sections = append(curFile.Sections, curSection)
858858

859+
// FIXME: the "-1" can't be right, these "line idx" are all 1-based, maybe there are other bugs that covers this bug.
859860
lineSectionInfo := getDiffLineSectionInfo(curFile.Name, line, leftLine-1, rightLine-1)
860861
diffLine := &DiffLine{
861862
Type: DiffLineSection,

0 commit comments

Comments
 (0)