@@ -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
6262type 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
8179func 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
0 commit comments