Skip to content

Commit f088317

Browse files
committed
Add "\ No newline at end of file" parsing. Show when newline is missing using symbols.
Fix section description being printed incorrectly. '@ -1,4...' instead of '@@ -1,4...'. Added a test for this.
1 parent abdd41c commit f088317

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

modules/repofiles/diff_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func TestGetDiffPreview(t *testing.T) {
8686
Type: 3,
8787
Content: "-Description for repo1",
8888
Comments: nil,
89+
NEOF: true,
8990
},
9091
{
9192
LeftIdx: 0,
@@ -100,6 +101,7 @@ func TestGetDiffPreview(t *testing.T) {
100101
Type: 2,
101102
Content: "+this is a new line",
102103
Comments: nil,
104+
NEOF: true,
103105
},
104106
},
105107
},

services/gitdiff/gitdiff.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ type DiffLine struct {
7676
Content string
7777
Comments []*models.Comment
7878
SectionInfo *DiffLineSectionInfo
79+
NEOF bool
7980
}
8081

8182
// DiffLineSectionInfo represents diff line section meta data
@@ -180,6 +181,7 @@ var (
180181
addedCodePrefix = []byte(`<span class="added-code">`)
181182
removedCodePrefix = []byte(`<span class="removed-code">`)
182183
codeTagSuffix = []byte(`</span>`)
184+
neofSuffix = []byte(`⍉⏎`)
183185
)
184186
var trailingSpanRegex = regexp.MustCompile(`<span\s*[[:alpha:]="]*?[>]?$`)
185187

@@ -290,10 +292,28 @@ func init() {
290292
diffMatchPatch.DiffEditCost = 100
291293
}
292294

295+
func getDiffLineContentForDisplay(diffLine *DiffLine) string {
296+
content := getLineContent(diffLine.Content[1:])
297+
if diffLine.NEOF {
298+
content += string(neofSuffix)
299+
}
300+
301+
return content
302+
}
303+
304+
func getDiffLineContentForDiff(diffLine *DiffLine) string {
305+
content := diffLine.Content[1:]
306+
if diffLine.NEOF {
307+
content += string(neofSuffix)
308+
}
309+
310+
return content
311+
}
312+
293313
// GetComputedInlineDiffFor computes inline diff for the given line.
294314
func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) template.HTML {
295315
if setting.Git.DisableDiffHighlight {
296-
return template.HTML(getLineContent(diffLine.Content[1:]))
316+
return template.HTML(getDiffLineContentForDisplay(diffLine))
297317
}
298318

299319
var (
@@ -305,29 +325,29 @@ func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) tem
305325
// try to find equivalent diff line. ignore, otherwise
306326
switch diffLine.Type {
307327
case DiffLineSection:
308-
return template.HTML(getLineContent(diffLine.Content[1:]))
328+
return template.HTML(getLineContent(diffLine.Content))
309329
case DiffLineAdd:
310330
compareDiffLine = diffSection.GetLine(DiffLineDel, diffLine.RightIdx)
311331
if compareDiffLine == nil {
312-
return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content[1:]))
332+
return template.HTML(highlight.Code(diffSection.FileName, getDiffLineContentForDisplay(diffLine)))
313333
}
314-
diff1 = compareDiffLine.Content
315-
diff2 = diffLine.Content
334+
diff1 = getDiffLineContentForDiff(compareDiffLine)
335+
diff2 = getDiffLineContentForDiff(diffLine)
316336
case DiffLineDel:
317337
compareDiffLine = diffSection.GetLine(DiffLineAdd, diffLine.LeftIdx)
318338
if compareDiffLine == nil {
319-
return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content[1:]))
339+
return template.HTML(highlight.Code(diffSection.FileName, getDiffLineContentForDisplay(diffLine)))
320340
}
321-
diff1 = diffLine.Content
322-
diff2 = compareDiffLine.Content
341+
diff1 = getDiffLineContentForDiff(diffLine)
342+
diff2 = getDiffLineContentForDiff(compareDiffLine)
323343
default:
324344
if strings.IndexByte(" +-", diffLine.Content[0]) > -1 {
325-
return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content[1:]))
345+
return template.HTML(highlight.Code(diffSection.FileName, getDiffLineContentForDiff(diffLine)))
326346
}
327347
return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content))
328348
}
329349

330-
diffRecord := diffMatchPatch.DiffMain(highlight.Code(diffSection.FileName, diff1[1:]), highlight.Code(diffSection.FileName, diff2[1:]), true)
350+
diffRecord := diffMatchPatch.DiffMain(highlight.Code(diffSection.FileName, diff1), highlight.Code(diffSection.FileName, diff2), true)
331351
diffRecord = diffMatchPatch.DiffCleanupEfficiency(diffRecord)
332352
return diffToHTML(diffSection.FileName, diffRecord, diffLine.Type)
333353
}
@@ -707,8 +727,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio
707727
err = fmt.Errorf("Unexpected line in hunk: %s", string(lineBytes))
708728
return
709729
}
710-
// Technically this should be the end the file!
711-
// FIXME: we should be putting a marker at the end of the file if there is no terminal new line
730+
curSection.Lines[len(curSection.Lines)-1].NEOF = true
712731
continue
713732
case '+':
714733
curFileLinesCount++

services/gitdiff/gitdiff_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,9 @@ func TestGetDiffRangeWithWhitespaceBehavior(t *testing.T) {
370370
}
371371
}
372372
}
373+
374+
func TestGetComputedInlineDiffForDiffLineSection(t *testing.T) {
375+
difflineSection := DiffLine{Type: DiffLineSection, Content: "@@ -270,6 +271,29 @@"}
376+
var section = DiffSection{Lines: []*DiffLine{&difflineSection}, FileName: "a.txt"}
377+
assert.Equal(t, template.HTML("@@ -270,6 +271,29 @@"), section.GetComputedInlineDiffFor(&difflineSection))
378+
}

0 commit comments

Comments
 (0)