diff options
Diffstat (limited to 'services/gitdiff')
-rw-r--r-- | services/gitdiff/gitdiff.go | 23 | ||||
-rw-r--r-- | services/gitdiff/gitdiff_test.go | 8 |
2 files changed, 20 insertions, 11 deletions
diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index fc55c03595..6632f2d94e 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -149,14 +149,9 @@ func (d *DiffLine) GetExpandDirection() DiffLineExpandDirection { return DiffLineExpandSingle } -func getDiffLineSectionInfo(curFile *DiffFile, line string, lastLeftIdx, lastRightIdx int) *DiffLineSectionInfo { - var ( - leftLine int - leftHunk int - rightLine int - righHunk int - ) - ss := strings.Split(line, "@@") +// ParseDiffHunkString parse the diffhunk content and return +func ParseDiffHunkString(diffhunk string) (leftLine, leftHunk, rightLine, righHunk int) { + ss := strings.Split(diffhunk, "@@") ranges := strings.Split(ss[1][1:], " ") leftRange := strings.Split(ranges[0], ",") leftLine, _ = com.StrTo(leftRange[0][1:]).Int() @@ -170,12 +165,18 @@ func getDiffLineSectionInfo(curFile *DiffFile, line string, lastLeftIdx, lastRig righHunk, _ = com.StrTo(rightRange[1]).Int() } } else { - log.Warn("Parse line number failed: %v", line) + log.Warn("Parse line number failed: %v", diffhunk) rightLine = leftLine righHunk = leftHunk } + return +} + +func getDiffLineSectionInfo(treePath, line string, lastLeftIdx, lastRightIdx int) *DiffLineSectionInfo { + leftLine, leftHunk, rightLine, righHunk := ParseDiffHunkString(line) + return &DiffLineSectionInfo{ - Path: curFile.Name, + Path: treePath, LastLeftIdx: lastLeftIdx, LastRightIdx: lastRightIdx, LeftIdx: leftLine, @@ -651,7 +652,7 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D case line[0] == '@': curSection = &DiffSection{} curFile.Sections = append(curFile.Sections, curSection) - lineSectionInfo := getDiffLineSectionInfo(curFile, line, leftLine-1, rightLine-1) + lineSectionInfo := getDiffLineSectionInfo(curFile.Name, line, leftLine-1, rightLine-1) diffLine := &DiffLine{ Type: DiffLineSection, Content: line, diff --git a/services/gitdiff/gitdiff_test.go b/services/gitdiff/gitdiff_test.go index ea25b38dff..58604d97c4 100644 --- a/services/gitdiff/gitdiff_test.go +++ b/services/gitdiff/gitdiff_test.go @@ -209,3 +209,11 @@ func TestGetDiffRangeWithWhitespaceBehavior(t *testing.T) { } } } + +func TestParseDiffHunkString(t *testing.T) { + leftLine, leftHunk, rightLine, rightHunk := ParseDiffHunkString("@@ -19,3 +19,5 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER") + assert.EqualValues(t, 19, leftLine) + assert.EqualValues(t, 3, leftHunk) + assert.EqualValues(t, 19, rightLine) + assert.EqualValues(t, 5, rightHunk) +} |