Skip to content

Commit 34b5c73

Browse files
committed
add tests
1 parent 4219669 commit 34b5c73

File tree

3 files changed

+98
-26
lines changed

3 files changed

+98
-26
lines changed

routers/web/repo/treelist.go

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,13 @@ func isExcludedEntry(entry *git.TreeEntry) bool {
6060

6161
// WebDiffFileItem is used by frontend, check the field names in frontend before changing
6262
type WebDiffFileItem struct {
63-
FullName string
64-
DisplayName string
65-
NameHash string
66-
DiffStatus string
67-
EntryMode string
68-
IsViewed bool
69-
Children []*WebDiffFileItem
70-
ViewedChildrenCount int
63+
FullName string
64+
DisplayName string
65+
NameHash string
66+
DiffStatus string
67+
EntryMode string
68+
IsViewed bool
69+
Children []*WebDiffFileItem
7170
}
7271

7372
// WebDiffFileTree is used by frontend, check the field names in frontend before changing
@@ -77,24 +76,9 @@ type WebDiffFileTree struct {
7776

7877
// transformDiffTreeForWeb transforms a gitdiff.DiffTree into a WebDiffFileTree for Web UI rendering
7978
// it also takes a map of file names to their viewed state, which is used to mark files as viewed
80-
// TODO: add some tests
8179
func transformDiffTreeForWeb(diffTree *gitdiff.DiffTree, filesViewedState map[string]pull_model.ViewedState) (dft WebDiffFileTree) {
8280
dirNodes := map[string]*WebDiffFileItem{"": &dft.TreeRoot}
83-
for _, file := range diffTree.Files {
84-
item := &WebDiffFileItem{FullName: file.HeadPath, DiffStatus: file.Status}
85-
// FIXME: filesViewedState is always nil?
86-
item.IsViewed = filesViewedState[item.FullName] == pull_model.Viewed
87-
item.NameHash = git.HashFilePathForWebUI(item.FullName)
88-
89-
switch file.HeadMode {
90-
case git.EntryModeTree:
91-
item.EntryMode = "tree"
92-
case git.EntryModeCommit:
93-
item.EntryMode = "commit" // submodule
94-
default:
95-
// default to empty, and will be treated as "blob" file because there is no "symlink" support yet
96-
}
97-
81+
addItem := func(item *WebDiffFileItem) {
9882
var parentPath string
9983
pos := strings.LastIndexByte(item.FullName, '/')
10084
if pos == -1 {
@@ -121,8 +105,37 @@ func transformDiffTreeForWeb(diffTree *gitdiff.DiffTree, filesViewedState map[st
121105
parentNode.Children = append(parentNode.Children, item)
122106
}
123107

124-
// TODO: merge into one level if there is only one sub directory
108+
for _, file := range diffTree.Files {
109+
item := &WebDiffFileItem{FullName: file.HeadPath, DiffStatus: file.Status}
110+
// FIXME: filesViewedState is always nil?
111+
item.IsViewed = filesViewedState[item.FullName] == pull_model.Viewed
112+
item.NameHash = git.HashFilePathForWebUI(item.FullName)
113+
114+
switch file.HeadMode {
115+
case git.EntryModeTree:
116+
item.EntryMode = "tree"
117+
case git.EntryModeCommit:
118+
item.EntryMode = "commit" // submodule
119+
default:
120+
// default to empty, and will be treated as "blob" file because there is no "symlink" support yet
121+
}
122+
addItem(item)
123+
}
125124

125+
var mergeSingleDir func(node *WebDiffFileItem)
126+
mergeSingleDir = func(node *WebDiffFileItem) {
127+
if len(node.Children) == 1 {
128+
if child := node.Children[0]; child.EntryMode == "tree" {
129+
node.FullName = child.FullName
130+
node.DisplayName = node.DisplayName + "/" + child.DisplayName
131+
node.Children = child.Children
132+
mergeSingleDir(node)
133+
}
134+
}
135+
}
136+
for _, node := range dft.TreeRoot.Children {
137+
mergeSingleDir(node)
138+
}
126139
return dft
127140
}
128141

routers/web/repo/treelist_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package repo
5+
6+
import (
7+
"testing"
8+
9+
pull_model "code.gitea.io/gitea/models/pull"
10+
"code.gitea.io/gitea/modules/git"
11+
"code.gitea.io/gitea/services/gitdiff"
12+
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestTransformDiffTreeForWeb(t *testing.T) {
17+
ret := transformDiffTreeForWeb(&gitdiff.DiffTree{Files: []*gitdiff.DiffTreeRecord{
18+
{
19+
Status: "changed",
20+
HeadPath: "dir-a/dir-a-x/file-deep",
21+
HeadMode: git.EntryModeBlob,
22+
},
23+
{
24+
Status: "added",
25+
HeadPath: "file1",
26+
HeadMode: git.EntryModeBlob,
27+
},
28+
}}, map[string]pull_model.ViewedState{
29+
"dir-a/dir-a-x/file-deep": pull_model.Viewed,
30+
})
31+
32+
assert.Equal(t, WebDiffFileTree{
33+
TreeRoot: WebDiffFileItem{
34+
Children: []*WebDiffFileItem{
35+
{
36+
EntryMode: "tree",
37+
DisplayName: "dir-a/dir-a-x",
38+
FullName: "dir-a/dir-a-x",
39+
Children: []*WebDiffFileItem{
40+
{
41+
EntryMode: "",
42+
DisplayName: "file-deep",
43+
FullName: "dir-a/dir-a-x/file-deep",
44+
NameHash: "4acf7eef1c943a09e9f754e93ff190db8583236b",
45+
DiffStatus: "changed",
46+
IsViewed: true,
47+
},
48+
},
49+
},
50+
{
51+
EntryMode: "",
52+
DisplayName: "file1",
53+
FullName: "file1",
54+
NameHash: "60b27f004e454aca81b0480209cce5081ec52390",
55+
DiffStatus: "added",
56+
},
57+
},
58+
},
59+
}, ret)
60+
}

web_src/js/modules/diff-file.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export type DiffTreeEntry = {
1313
EntryMode: string,
1414
IsViewed: boolean,
1515
Children: DiffTreeEntry[],
16-
ViewedChildrenCount: number,
1716

1817
ParentEntry?: DiffTreeEntry,
1918
}

0 commit comments

Comments
 (0)