mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
Use git diff-tree
for DiffFileTree
on diff pages (#33514)
Modify Diff View FileTree to show all files ## Changes * removes Show Status button on diff * uses `git diff-tree` to generate the file tree for the diff * doesn't reload the diff tree each time we load more files in the preview * selecting and unloaded file will keep loading until that file is loaded * removes `DiffFileList.vue` and "Show Stats" in diff options ## Open Questions * selecting and unloaded file will keep loading until that file is loaded. Is this behaviour okay? It matches what github does. ### Demo In this demo I set `git.MAX_GIT_DIFF_FILES=1` in my `app.ini` to demonstrate a worst case example. In most cases the behaviour isn't nearly as jarring as we load a bunch of files at a time. https://github.com/user-attachments/assets/72f29663-d6fc-472d-94fa-7fb5950c2836 --------- Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
@@ -344,18 +344,30 @@ func Diff(ctx *context.Context) {
|
||||
ctx.Data["Reponame"] = repoName
|
||||
|
||||
var parentCommit *git.Commit
|
||||
var parentCommitID string
|
||||
if commit.ParentCount() > 0 {
|
||||
parentCommit, err = gitRepo.GetCommit(parents[0])
|
||||
if err != nil {
|
||||
ctx.NotFound(err)
|
||||
return
|
||||
}
|
||||
parentCommitID = parentCommit.ID.String()
|
||||
}
|
||||
setCompareContext(ctx, parentCommit, commit, userName, repoName)
|
||||
ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitID)
|
||||
ctx.Data["Commit"] = commit
|
||||
ctx.Data["Diff"] = diff
|
||||
|
||||
if !fileOnly {
|
||||
diffTree, err := gitdiff.GetDiffTree(ctx, gitRepo, false, parentCommitID, commitID)
|
||||
if err != nil {
|
||||
ctx.ServerError("GetDiffTree", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.PageData["DiffFiles"] = transformDiffTreeForUI(diffTree, nil)
|
||||
}
|
||||
|
||||
statuses, _, err := git_model.GetLatestCommitStatus(ctx, ctx.Repo.Repository.ID, commitID, db.ListOptionsAll)
|
||||
if err != nil {
|
||||
log.Error("GetLatestCommitStatus: %v", err)
|
||||
|
@@ -633,6 +633,16 @@ func PrepareCompareDiff(
|
||||
ctx.Data["Diff"] = diff
|
||||
ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0
|
||||
|
||||
if !fileOnly {
|
||||
diffTree, err := gitdiff.GetDiffTree(ctx, ci.HeadGitRepo, false, beforeCommitID, headCommitID)
|
||||
if err != nil {
|
||||
ctx.ServerError("GetDiffTree", err)
|
||||
return false
|
||||
}
|
||||
|
||||
ctx.PageData["DiffFiles"] = transformDiffTreeForUI(diffTree, nil)
|
||||
}
|
||||
|
||||
headCommit, err := ci.HeadGitRepo.GetCommit(headCommitID)
|
||||
if err != nil {
|
||||
ctx.ServerError("GetCommit", err)
|
||||
|
@@ -761,6 +761,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
|
||||
|
||||
var methodWithError string
|
||||
var diff *gitdiff.Diff
|
||||
shouldGetUserSpecificDiff := false
|
||||
|
||||
// if we're not logged in or only a single commit (or commit range) is shown we
|
||||
// have to load only the diff and not get the viewed information
|
||||
@@ -772,6 +773,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
|
||||
} else {
|
||||
diff, err = gitdiff.SyncAndGetUserSpecificDiff(ctx, ctx.Doer.ID, pull, gitRepo, diffOptions, files...)
|
||||
methodWithError = "SyncAndGetUserSpecificDiff"
|
||||
shouldGetUserSpecificDiff = true
|
||||
}
|
||||
if err != nil {
|
||||
ctx.ServerError(methodWithError, err)
|
||||
@@ -816,6 +818,27 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
|
||||
}
|
||||
}
|
||||
|
||||
if !fileOnly {
|
||||
// note: use mergeBase is set to false because we already have the merge base from the pull request info
|
||||
diffTree, err := gitdiff.GetDiffTree(ctx, gitRepo, false, pull.MergeBase, headCommitID)
|
||||
if err != nil {
|
||||
ctx.ServerError("GetDiffTree", err)
|
||||
return
|
||||
}
|
||||
|
||||
filesViewedState := make(map[string]pull_model.ViewedState)
|
||||
if shouldGetUserSpecificDiff {
|
||||
// This sort of sucks because we already fetch this when getting the diff
|
||||
review, err := pull_model.GetNewestReviewState(ctx, ctx.Doer.ID, issue.ID)
|
||||
if err == nil && review != nil && review.UpdatedFiles != nil {
|
||||
// If there wasn't an error and we have a review with updated files, use that
|
||||
filesViewedState = review.UpdatedFiles
|
||||
}
|
||||
}
|
||||
|
||||
ctx.PageData["DiffFiles"] = transformDiffTreeForUI(diffTree, filesViewedState)
|
||||
}
|
||||
|
||||
ctx.Data["Diff"] = diff
|
||||
ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0
|
||||
|
||||
|
@@ -6,9 +6,11 @@ package repo
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
pull_model "code.gitea.io/gitea/models/pull"
|
||||
"code.gitea.io/gitea/modules/base"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/services/context"
|
||||
"code.gitea.io/gitea/services/gitdiff"
|
||||
|
||||
"github.com/go-enry/go-enry/v2"
|
||||
)
|
||||
@@ -52,3 +54,33 @@ func isExcludedEntry(entry *git.TreeEntry) bool {
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
type FileDiffFile struct {
|
||||
Name string
|
||||
NameHash string
|
||||
IsSubmodule bool
|
||||
IsViewed bool
|
||||
Status string
|
||||
}
|
||||
|
||||
// transformDiffTreeForUI transforms a DiffTree into a slice of FileDiffFile for UI rendering
|
||||
// it also takes a map of file names to their viewed state, which is used to mark files as viewed
|
||||
func transformDiffTreeForUI(diffTree *gitdiff.DiffTree, filesViewedState map[string]pull_model.ViewedState) []FileDiffFile {
|
||||
files := make([]FileDiffFile, 0, len(diffTree.Files))
|
||||
|
||||
for _, file := range diffTree.Files {
|
||||
nameHash := git.HashFilePathForWebUI(file.HeadPath)
|
||||
isSubmodule := file.HeadMode == git.EntryModeCommit
|
||||
isViewed := filesViewedState[file.HeadPath] == pull_model.Viewed
|
||||
|
||||
files = append(files, FileDiffFile{
|
||||
Name: file.HeadPath,
|
||||
NameHash: nameHash,
|
||||
IsSubmodule: isSubmodule,
|
||||
IsViewed: isViewed,
|
||||
Status: file.Status,
|
||||
})
|
||||
}
|
||||
|
||||
return files
|
||||
}
|
||||
|
Reference in New Issue
Block a user