@@ -10,6 +10,7 @@ public class BranchTreeNode : ObservableObject
1010 public string Name { get ; private set ; } = string . Empty ;
1111 public string Path { get ; private set ; } = string . Empty ;
1212 public object Backend { get ; private set ; } = null ;
13+ public ulong TimeToSort { get ; private set ; } = 0 ;
1314 public int Depth { get ; set ; } = 0 ;
1415 public bool IsSelected { get ; set ; } = false ;
1516 public List < BranchTreeNode > Children { get ; private set ; } = new List < BranchTreeNode > ( ) ;
@@ -62,6 +63,12 @@ public class Builder
6263 public List < BranchTreeNode > Remotes => _remotes ;
6364 public List < string > InvalidExpandedNodes => _invalidExpandedNodes ;
6465
66+ public Builder ( Models . BranchSortMode localSortMode , Models . BranchSortMode remoteSortMode )
67+ {
68+ _localSortMode = localSortMode ;
69+ _remoteSortMode = remoteSortMode ;
70+ }
71+
6572 public void SetExpandedNodes ( List < string > expanded )
6673 {
6774 foreach ( var node in expanded )
@@ -72,6 +79,7 @@ public void Run(List<Models.Branch> branches, List<Models.Remote> remotes, bool
7279 {
7380 var folders = new Dictionary < string , BranchTreeNode > ( ) ;
7481
82+ var fakeRemoteTime = ( ulong ) remotes . Count ;
7583 foreach ( var remote in remotes )
7684 {
7785 var path = $ "refs/remotes/{ remote . Name } ";
@@ -81,8 +89,10 @@ public void Run(List<Models.Branch> branches, List<Models.Remote> remotes, bool
8189 Path = path ,
8290 Backend = remote ,
8391 IsExpanded = bForceExpanded || _expanded . Contains ( path ) ,
92+ TimeToSort = fakeRemoteTime ,
8493 } ;
8594
95+ fakeRemoteTime -- ;
8696 folders . Add ( path , node ) ;
8797 _remotes . Add ( node ) ;
8898 }
@@ -108,8 +118,26 @@ public void Run(List<Models.Branch> branches, List<Models.Remote> remotes, bool
108118 }
109119
110120 folders . Clear ( ) ;
111- SortNodes ( _locals ) ;
112- SortNodes ( _remotes ) ;
121+
122+ if ( _localSortMode == Models . BranchSortMode . Name )
123+ {
124+ SortNodesByName ( _locals ) ;
125+ }
126+ else
127+ {
128+ SetTimeToSortRecusive ( _locals ) ;
129+ SortNodesByTime ( _locals ) ;
130+ }
131+
132+ if ( _remoteSortMode == Models . BranchSortMode . Name )
133+ {
134+ SortNodesByName ( _remotes ) ;
135+ }
136+ else
137+ {
138+ SetTimeToSortRecusive ( _remotes ) ;
139+ SortNodesByTime ( _remotes ) ;
140+ }
113141 }
114142
115143 private void MakeBranchNode ( Models . Branch branch , List < BranchTreeNode > roots , Dictionary < string , BranchTreeNode > folders , string prefix , bool bForceExpanded )
@@ -124,6 +152,7 @@ private void MakeBranchNode(Models.Branch branch, List<BranchTreeNode> roots, Di
124152 Path = fullpath ,
125153 Backend = branch ,
126154 IsExpanded = false ,
155+ TimeToSort = branch . CommitterDate ,
127156 } ) ;
128157 return ;
129158 }
@@ -175,10 +204,11 @@ private void MakeBranchNode(Models.Branch branch, List<BranchTreeNode> roots, Di
175204 Path = fullpath ,
176205 Backend = branch ,
177206 IsExpanded = false ,
207+ TimeToSort = branch . CommitterDate ,
178208 } ) ;
179209 }
180210
181- private void SortNodes ( List < BranchTreeNode > nodes )
211+ private void SortNodesByName ( List < BranchTreeNode > nodes )
182212 {
183213 nodes . Sort ( ( l , r ) =>
184214 {
@@ -192,9 +222,61 @@ private void SortNodes(List<BranchTreeNode> nodes)
192222 } ) ;
193223
194224 foreach ( var node in nodes )
195- SortNodes ( node . Children ) ;
225+ SortNodesByName ( node . Children ) ;
226+ }
227+
228+ private void SortNodesByTime ( List < BranchTreeNode > nodes )
229+ {
230+ nodes . Sort ( ( l , r ) =>
231+ {
232+ if ( l . Backend is Models . Branch { IsDetachedHead : true } )
233+ return - 1 ;
234+
235+ if ( l . Backend is Models . Branch )
236+ {
237+ if ( r . Backend is Models . Branch )
238+ return r . TimeToSort == l . TimeToSort ? Models . NumericSort . Compare ( l . Name , r . Name ) : r . TimeToSort . CompareTo ( l . TimeToSort ) ;
239+ else
240+ return 1 ;
241+ }
242+
243+ if ( r . Backend is Models . Branch )
244+ return - 1 ;
245+
246+ if ( r . TimeToSort == l . TimeToSort )
247+ return Models . NumericSort . Compare ( l . Name , r . Name ) ;
248+
249+ return r . TimeToSort . CompareTo ( l . TimeToSort ) ;
250+ } ) ;
251+
252+ foreach ( var node in nodes )
253+ SortNodesByTime ( node . Children ) ;
254+ }
255+
256+ private ulong SetTimeToSortRecusive ( List < BranchTreeNode > nodes )
257+ {
258+ var recent = ( ulong ) 0 ;
259+
260+ foreach ( var node in nodes )
261+ {
262+ if ( node . Backend is Models . Branch )
263+ {
264+ recent = Math . Max ( recent , node . TimeToSort ) ;
265+ continue ;
266+ }
267+
268+ var time = SetTimeToSortRecusive ( node . Children ) ;
269+ recent = Math . Max ( recent , time ) ;
270+
271+ if ( node . Backend is not Models . Remote )
272+ node . TimeToSort = time ;
273+ }
274+
275+ return recent ;
196276 }
197277
278+ private readonly Models . BranchSortMode _localSortMode = Models . BranchSortMode . Name ;
279+ private readonly Models . BranchSortMode _remoteSortMode = Models . BranchSortMode . Name ;
198280 private readonly List < BranchTreeNode > _locals = new List < BranchTreeNode > ( ) ;
199281 private readonly List < BranchTreeNode > _remotes = new List < BranchTreeNode > ( ) ;
200282 private readonly List < string > _invalidExpandedNodes = new List < string > ( ) ;
0 commit comments