mirror of
https://github.com/go-gitea/gitea
synced 2025-07-23 02:38:35 +00:00
Mark parent directory as viewed when all files are viewed (#33958)
Fix #25644 --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
@@ -5,6 +5,7 @@ package repo
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
pull_model "code.gitea.io/gitea/models/pull"
|
||||
"code.gitea.io/gitea/modules/base"
|
||||
@@ -57,34 +58,85 @@ func isExcludedEntry(entry *git.TreeEntry) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
type FileDiffFile struct {
|
||||
Name string
|
||||
// WebDiffFileItem is used by frontend, check the field names in frontend before changing
|
||||
type WebDiffFileItem struct {
|
||||
FullName string
|
||||
DisplayName string
|
||||
NameHash string
|
||||
IsSubmodule bool
|
||||
DiffStatus string
|
||||
EntryMode string
|
||||
IsViewed bool
|
||||
Status string
|
||||
Children []*WebDiffFileItem
|
||||
// TODO: add icon support in the future
|
||||
}
|
||||
|
||||
// transformDiffTreeForUI transforms a DiffTree into a slice of FileDiffFile for UI rendering
|
||||
// WebDiffFileTree is used by frontend, check the field names in frontend before changing
|
||||
type WebDiffFileTree struct {
|
||||
TreeRoot WebDiffFileItem
|
||||
}
|
||||
|
||||
// transformDiffTreeForWeb transforms a gitdiff.DiffTree into a WebDiffFileTree for Web 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,
|
||||
})
|
||||
func transformDiffTreeForWeb(diffTree *gitdiff.DiffTree, filesViewedState map[string]pull_model.ViewedState) (dft WebDiffFileTree) {
|
||||
dirNodes := map[string]*WebDiffFileItem{"": &dft.TreeRoot}
|
||||
addItem := func(item *WebDiffFileItem) {
|
||||
var parentPath string
|
||||
pos := strings.LastIndexByte(item.FullName, '/')
|
||||
if pos == -1 {
|
||||
item.DisplayName = item.FullName
|
||||
} else {
|
||||
parentPath = item.FullName[:pos]
|
||||
item.DisplayName = item.FullName[pos+1:]
|
||||
}
|
||||
parentNode, parentExists := dirNodes[parentPath]
|
||||
if !parentExists {
|
||||
parentNode = &dft.TreeRoot
|
||||
fields := strings.Split(parentPath, "/")
|
||||
for idx, field := range fields {
|
||||
nodePath := strings.Join(fields[:idx+1], "/")
|
||||
node, ok := dirNodes[nodePath]
|
||||
if !ok {
|
||||
node = &WebDiffFileItem{EntryMode: "tree", DisplayName: field, FullName: nodePath}
|
||||
dirNodes[nodePath] = node
|
||||
parentNode.Children = append(parentNode.Children, node)
|
||||
}
|
||||
parentNode = node
|
||||
}
|
||||
}
|
||||
parentNode.Children = append(parentNode.Children, item)
|
||||
}
|
||||
|
||||
return files
|
||||
for _, file := range diffTree.Files {
|
||||
item := &WebDiffFileItem{FullName: file.HeadPath, DiffStatus: file.Status}
|
||||
item.IsViewed = filesViewedState[item.FullName] == pull_model.Viewed
|
||||
item.NameHash = git.HashFilePathForWebUI(item.FullName)
|
||||
|
||||
switch file.HeadMode {
|
||||
case git.EntryModeTree:
|
||||
item.EntryMode = "tree"
|
||||
case git.EntryModeCommit:
|
||||
item.EntryMode = "commit" // submodule
|
||||
default:
|
||||
// default to empty, and will be treated as "blob" file because there is no "symlink" support yet
|
||||
}
|
||||
addItem(item)
|
||||
}
|
||||
|
||||
var mergeSingleDir func(node *WebDiffFileItem)
|
||||
mergeSingleDir = func(node *WebDiffFileItem) {
|
||||
if len(node.Children) == 1 {
|
||||
if child := node.Children[0]; child.EntryMode == "tree" {
|
||||
node.FullName = child.FullName
|
||||
node.DisplayName = node.DisplayName + "/" + child.DisplayName
|
||||
node.Children = child.Children
|
||||
mergeSingleDir(node)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, node := range dft.TreeRoot.Children {
|
||||
mergeSingleDir(node)
|
||||
}
|
||||
return dft
|
||||
}
|
||||
|
||||
func TreeViewNodes(ctx *context.Context) {
|
||||
|
Reference in New Issue
Block a user