diff --git a/modules/repofiles/diff_test.go b/modules/repofiles/diff_test.go index d40f8a50fe089..da50284150a28 100644 --- a/modules/repofiles/diff_test.go +++ b/modules/repofiles/diff_test.go @@ -83,6 +83,7 @@ func TestGetDiffPreview(t *testing.T) { { LeftIdx: 3, RightIdx: 0, + Match: 4, Type: 3, Content: "-Description for repo1", Comments: nil, @@ -90,6 +91,7 @@ func TestGetDiffPreview(t *testing.T) { { LeftIdx: 0, RightIdx: 3, + Match: 3, Type: 2, Content: "+Description for repo1", Comments: nil, @@ -97,6 +99,7 @@ func TestGetDiffPreview(t *testing.T) { { LeftIdx: 0, RightIdx: 4, + Match: -1, Type: 2, Content: "+this is a new line", Comments: nil, diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index d50e41eb40279..59da680d48f22 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -75,6 +75,7 @@ const ( type DiffLine struct { LeftIdx int RightIdx int + Match int Type DiffLineType Content string Comments []*models.Comment @@ -943,6 +944,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio curFileLFSPrefix bool ) + lastLeftIdx := -1 leftLine, rightLine := 1, 1 for { @@ -1027,13 +1029,21 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio curFile.IsIncomplete = true continue } - diffLine := &DiffLine{Type: DiffLineAdd, RightIdx: rightLine} + diffLine := &DiffLine{Type: DiffLineAdd, RightIdx: rightLine, Match: -1} rightLine++ if curSection == nil { // Create a new section to represent this hunk curSection = &DiffSection{} curFile.Sections = append(curFile.Sections, curSection) } + if lastLeftIdx > -1 { + diffLine.Match = lastLeftIdx + curSection.Lines[lastLeftIdx].Match = len(curSection.Lines) + lastLeftIdx++ + if lastLeftIdx >= len(curSection.Lines) || curSection.Lines[lastLeftIdx].Type != DiffLineDel { + lastLeftIdx = -1 + } + } curSection.Lines = append(curSection.Lines, diffLine) case '-': curFileLinesCount++ @@ -1042,7 +1052,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio curFile.IsIncomplete = true continue } - diffLine := &DiffLine{Type: DiffLineDel, LeftIdx: leftLine} + diffLine := &DiffLine{Type: DiffLineDel, LeftIdx: leftLine, Match: -1} if leftLine > 0 { leftLine++ } @@ -1051,6 +1061,9 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio curSection = &DiffSection{} curFile.Sections = append(curFile.Sections, curSection) } + if len(curSection.Lines) == 0 || curSection.Lines[len(curSection.Lines)-1].Type != DiffLineDel { + lastLeftIdx = len(curSection.Lines) + } curSection.Lines = append(curSection.Lines, diffLine) case ' ': curFileLinesCount++ @@ -1061,6 +1074,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio diffLine := &DiffLine{Type: DiffLinePlain, LeftIdx: leftLine, RightIdx: rightLine} leftLine++ rightLine++ + lastLeftIdx = -1 if curSection == nil { // Create a new section to represent this hunk curSection = &DiffSection{} diff --git a/templates/repo/diff/box.tmpl b/templates/repo/diff/box.tmpl index 1ca2dcc4d8144..4f8f7260976aa 100644 --- a/templates/repo/diff/box.tmpl +++ b/templates/repo/diff/box.tmpl @@ -154,33 +154,5 @@ {{end}} {{template "repo/issue/view_content/reference_issue_dialog" .}} - - {{if .IsSplitStyle}} - - {{end}} {{end}} diff --git a/templates/repo/diff/section_split.tmpl b/templates/repo/diff/section_split.tmpl index 2f959ac2da7e8..aed6d784b307c 100644 --- a/templates/repo/diff/section_split.tmpl +++ b/templates/repo/diff/section_split.tmpl @@ -1,52 +1,97 @@ {{$file := .file}} {{range $j, $section := $file.Sections}} {{range $k, $line := $section.Lines}} -
{{$section.GetComputedInlineDiffFor $line}}
{{if $line.LeftIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}
{{if $line.RightIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}
{{$section.GetComputedInlineDiffFor $line}}
{{if $line.LeftIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}
{{if $match.RightIdx}}{{$section.GetComputedInlineDiffFor $match}}{{end}}
{{if $line.LeftIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}
{{if $line.RightIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}