Skip to content

Full-file syntax highlighting for diff pages #33766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
Mar 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
1955475
initial pass at fixing multiline comment highlighting in git diffs
dfirebaugh Mar 1, 2025
0452d27
updating tests since diffwithhighlight is now only responsible for pl…
dfirebaugh Mar 1, 2025
60898a6
adding language var back
dfirebaugh Mar 1, 2025
a180f0f
Merge remote-tracking branch 'upstream/main' into git-diff-highlights
dfirebaugh Mar 1, 2025
aac3ec4
updating tests
dfirebaugh Mar 2, 2025
68dd510
- making highlighted code members private
dfirebaugh Mar 2, 2025
3607a25
after make fmt
dfirebaugh Mar 2, 2025
843cb3d
implementing lint suggestion
dfirebaugh Mar 2, 2025
0eaefbc
- removing filename and language from `diffWithHighlight` func
dfirebaugh Mar 2, 2025
9a59d9a
reverting span close logic
dfirebaugh Mar 2, 2025
5b0adf2
updating `highlightdiff_test.go` test data
dfirebaugh Mar 2, 2025
447236e
- readding call to `highligh.Code` in `diffWithHighlight`
dfirebaugh Mar 2, 2025
09dc97b
- calling `template.HTMLEscapeString` directly in `diffWithHighlight`…
dfirebaugh Mar 2, 2025
14d6c0e
reverting htmlescape because we already escape it when we highlight t…
dfirebaugh Mar 2, 2025
d42de12
- reverting original hunk-based syntax highlighting
dfirebaugh Mar 2, 2025
6aa9099
temp: simplify code
wxiaoguang Mar 3, 2025
8bb9de4
temp: add highlight fallback and FIXME comment
wxiaoguang Mar 3, 2025
44c2766
Merge branch 'main' into git-diff-highlights
wxiaoguang Mar 5, 2025
1e6c8dd
add FIXME for abused GitDiff
wxiaoguang Mar 5, 2025
e424efe
Merge branch 'main' into git-diff-highlights
wxiaoguang Mar 8, 2025
493e32b
fix merge
wxiaoguang Mar 8, 2025
abc63e9
fix DiffFile struct
wxiaoguang Mar 8, 2025
7de148a
split GetDiff
wxiaoguang Mar 8, 2025
0a29d4d
only save the highlighted lines we need, but not the whole file, to s…
wxiaoguang Mar 8, 2025
d786956
fix tests
wxiaoguang Mar 8, 2025
ba96e9f
fix test & lint
wxiaoguang Mar 8, 2025
7fffde1
Update services/gitdiff/gitdiff.go
wxiaoguang Mar 8, 2025
632ea32
improve test
wxiaoguang Mar 8, 2025
a07ca6a
Merge branch 'main' into git-diff-highlights
wxiaoguang Mar 8, 2025
7939850
Merge branch 'main' into git-diff-highlights
wxiaoguang Mar 9, 2025
b2dbc78
fix 500 in blob_excerpt
wxiaoguang Mar 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions modules/git/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package git
import (
"bytes"
"encoding/base64"
"errors"
"io"

"code.gitea.io/gitea/modules/typesniffer"
Expand Down Expand Up @@ -34,8 +35,9 @@ func (b *Blob) GetBlobContent(limit int64) (string, error) {
return string(buf), err
}

// GetBlobLineCount gets line count of the blob
func (b *Blob) GetBlobLineCount() (int, error) {
// GetBlobLineCount gets line count of the blob.
// It will also try to write the content to w if it's not nil, then we could pre-fetch the content without reading it again.
func (b *Blob) GetBlobLineCount(w io.Writer) (int, error) {
reader, err := b.DataAsync()
if err != nil {
return 0, err
Expand All @@ -44,20 +46,20 @@ func (b *Blob) GetBlobLineCount() (int, error) {
buf := make([]byte, 32*1024)
count := 1
lineSep := []byte{'\n'}

c, err := reader.Read(buf)
if c == 0 && err == io.EOF {
return 0, nil
}
for {
c, err := reader.Read(buf)
if w != nil {
if _, err := w.Write(buf[:c]); err != nil {
return count, err
}
}
count += bytes.Count(buf[:c], lineSep)
switch {
case err == io.EOF:
case errors.Is(err, io.EOF):
return count, nil
case err != nil:
return count, err
}
c, err = reader.Read(buf)
}
}

Expand Down
3 changes: 2 additions & 1 deletion modules/highlight/highlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
gohtml "html"
"html/template"
"io"
"path"
"path/filepath"
"strings"
"sync"
Expand Down Expand Up @@ -83,7 +84,7 @@ func Code(fileName, language, code string) (output template.HTML, lexerName stri
}

if lexer == nil {
if val, ok := highlightMapping[filepath.Ext(fileName)]; ok {
if val, ok := highlightMapping[path.Ext(fileName)]; ok {
// use mapped value to find lexer
lexer = lexers.Get(val)
}
Expand Down
3 changes: 1 addition & 2 deletions routers/api/v1/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -1591,8 +1591,7 @@ func GetPullRequestFiles(ctx *context.APIContext) {
maxLines := setting.Git.MaxGitDiffLines

// FIXME: If there are too many files in the repo, may cause some unpredictable issues.
// FIXME: it doesn't need to call "GetDiff" to do various parsing and highlighting
diff, err := gitdiff.GetDiff(ctx, baseGitRepo,
diff, err := gitdiff.GetDiffForAPI(ctx, baseGitRepo,
&gitdiff.DiffOptions{
BeforeCommitID: startCommitID,
AfterCommitID: endCommitID,
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/blame.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func RefBlame(ctx *context.Context) {
return
}

ctx.Data["NumLines"], err = blob.GetBlobLineCount()
ctx.Data["NumLines"], err = blob.GetBlobLineCount(nil)
if err != nil {
ctx.NotFound(err)
return
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func Diff(ctx *context.Context) {
maxLines, maxFiles = -1, -1
}

diff, err := gitdiff.GetDiff(ctx, gitRepo, &gitdiff.DiffOptions{
diff, err := gitdiff.GetDiffForRender(ctx, gitRepo, &gitdiff.DiffOptions{
AfterCommitID: commitID,
SkipTo: ctx.FormString("skip-to"),
MaxLines: maxLines,
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ func PrepareCompareDiff(

fileOnly := ctx.FormBool("file-only")

diff, err := gitdiff.GetDiff(ctx, ci.HeadGitRepo,
diff, err := gitdiff.GetDiffForRender(ctx, ci.HeadGitRepo,
&gitdiff.DiffOptions{
BeforeCommitID: beforeCommitID,
AfterCommitID: headCommitID,
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
diffOptions.BeforeCommitID = startCommitID
}

diff, err := gitdiff.GetDiff(ctx, gitRepo, diffOptions, files...)
diff, err := gitdiff.GetDiffForRender(ctx, gitRepo, diffOptions, files...)
if err != nil {
ctx.ServerError("GetDiff", err)
return
Expand Down
Loading