77
88namespace SourceGit . ViewModels
99{
10+ public record TargetGroup ( string Id , string Name , string Description ) ;
11+
1012 public class Clone : Popup
1113 {
1214 [ Required ( ErrorMessage = "Remote URL is required" ) ]
@@ -47,15 +49,21 @@ public string Local
4749 set => SetProperty ( ref _local , value ) ;
4850 }
4951
50- public List < RepositoryNode > Groups
52+ public List < TargetGroup > Groups
5153 {
5254 get ;
5355 }
5456
55- public RepositoryNode SelectedGroup
57+ public int SelectedGroupIndex
58+ {
59+ get => _selectedGroupIndex ;
60+ set => SetProperty ( ref _selectedGroupIndex , value ) ;
61+ }
62+
63+ public bool CanAutoSelectGroup
5664 {
57- get => _selectedGroup ;
58- set => SetProperty ( ref _selectedGroup , value ) ;
65+ get => _canAutoSelectGroup ;
66+ private set => SetProperty ( ref _canAutoSelectGroup , value ) ;
5967 }
6068
6169 public int Bookmark
@@ -81,15 +89,17 @@ public Clone(string pageId)
8189 _pageId = pageId ;
8290 CanTerminate = true ;
8391
84- Groups = new List < RepositoryNode > ( ) ;
85- Groups . Add ( new RepositoryNode { Name = "No Group (Uncategorized) ", Id = string . Empty } ) ;
86- SelectedGroup = Groups [ 0 ] ;
92+ Groups = new List < TargetGroup > ( ) ;
93+ Groups . Add ( new TargetGroup ( string . Empty , "Auto ", "Based-on Default Clone Dir" ) ) ;
94+ Groups . Add ( new TargetGroup ( string . Empty , "No Group" , "Uncategorized" ) ) ;
8795 CollectGroups ( Groups , Preferences . Instance . RepositoryNodes ) ;
8896
8997 var activeWorkspace = Preferences . Instance . GetActiveWorkspace ( ) ;
90- _parentFolder = activeWorkspace ? . DefaultCloneDir ;
91- if ( string . IsNullOrEmpty ( ParentFolder ) )
92- _parentFolder = Preferences . Instance . GitDefaultCloneDir ;
98+ _defaultCloneDir = activeWorkspace ? . DefaultCloneDir ;
99+ if ( string . IsNullOrEmpty ( _defaultCloneDir ) )
100+ _defaultCloneDir = Preferences . Instance . GitDefaultCloneDir ;
101+
102+ ParentFolder = _defaultCloneDir ;
93103 }
94104
95105 public static ValidationResult ValidateRemote ( string remote , ValidationContext _ )
@@ -164,7 +174,26 @@ public override async Task<bool> Sure()
164174
165175 log . Complete ( ) ;
166176
167- var parent = _selectedGroup is { Id : not "" } ? _selectedGroup : null ;
177+ RepositoryNode parent = null ;
178+ if ( _selectedGroupIndex == 0 ) // Auto (Based-on Default Clone Dir)
179+ {
180+ if ( ! string . IsNullOrEmpty ( _defaultCloneDir ) )
181+ {
182+ var normalizedDefaultCloneDir = _defaultCloneDir . Replace ( '\\ ' , '/' ) . TrimEnd ( '/' ) + "/" ;
183+ var normalizedParentFolder = _parentFolder . Replace ( '\\ ' , '/' ) . TrimEnd ( '/' ) + "/" ;
184+ if ( normalizedParentFolder . Length > normalizedDefaultCloneDir . Length &&
185+ normalizedParentFolder . StartsWith ( normalizedDefaultCloneDir , StringComparison . Ordinal ) )
186+ {
187+ var relativePath = normalizedParentFolder . Substring ( normalizedDefaultCloneDir . Length ) ;
188+ parent = Preferences . Instance . FindOrCreateGroupRecursive ( relativePath . TrimEnd ( '/' ) ) ;
189+ }
190+ }
191+ }
192+ else if ( _selectedGroupIndex > 0 && _selectedGroupIndex < Groups . Count ) // Existing group
193+ {
194+ parent = Preferences . Instance . FindNode ( Groups [ _selectedGroupIndex ] . Id ) ;
195+ }
196+
168197 var node = Preferences . Instance . FindOrAddNodeByRepositoryPath ( path , parent , true ) ;
169198 node . Bookmark = _bookmark ;
170199 await node . UpdateStatusAsync ( false , null ) ;
@@ -193,14 +222,15 @@ public override void Terminate()
193222 var _ = _cancellation ? . CancelAsync ( ) ;
194223 }
195224
196- private void CollectGroups ( List < RepositoryNode > outs , List < RepositoryNode > collections )
225+ private void CollectGroups ( List < TargetGroup > outs , List < RepositoryNode > collections , string prefix = null )
197226 {
198227 foreach ( var node in collections )
199228 {
200229 if ( ! node . IsRepository )
201230 {
202- outs . Add ( node ) ;
203- CollectGroups ( outs , node . SubNodes ) ;
231+ var displayName = prefix != null ? $ "{ prefix } /{ node . Name } " : node . Name ;
232+ outs . Add ( new ( node . Id , displayName , string . Empty ) ) ;
233+ CollectGroups ( outs , node . SubNodes , displayName ) ;
204234 }
205235 }
206236 }
@@ -210,9 +240,11 @@ private void CollectGroups(List<RepositoryNode> outs, List<RepositoryNode> colle
210240 private bool _useSSH = false ;
211241 private string _sshKey = string . Empty ;
212242 private string _parentFolder = string . Empty ;
243+ private string _defaultCloneDir = string . Empty ;
213244 private string _local = string . Empty ;
214245 private string _extraArgs = string . Empty ;
215- private RepositoryNode _selectedGroup = null ;
246+ private int _selectedGroupIndex = 0 ;
247+ private bool _canAutoSelectGroup = false ;
216248 private int _bookmark = 0 ;
217249 private CancellationTokenSource _cancellation = null ;
218250 }
0 commit comments