22using System . Collections . Generic ;
33using System . ComponentModel . DataAnnotations ;
44using System . IO ;
5+ using System . Threading ;
56using System . Threading . Tasks ;
67
78namespace SourceGit . ViewModels
@@ -78,6 +79,7 @@ public bool InitAndUpdateSubmodules
7879 public Clone ( string pageId )
7980 {
8081 _pageId = pageId ;
82+ CanTerminate = true ;
8183
8284 Groups = new List < RepositoryNode > ( ) ;
8385 Groups . Add ( new RepositoryNode { Name = "No Group (Uncategorized)" , Id = string . Empty } ) ;
@@ -111,7 +113,11 @@ public override async Task<bool> Sure()
111113 var log = new CommandLog ( "Clone" ) ;
112114 Use ( log ) ;
113115
116+ _cancellation = new CancellationTokenSource ( ) ;
117+ var token = _cancellation . Token ;
118+
114119 var succ = await new Commands . Clone ( _pageId , _parentFolder , _remote , _local , _useSSH ? _sshKey : "" , _extraArgs )
120+ . WithCancellation ( token )
115121 . Use ( log )
116122 . ExecAsync ( ) ;
117123 if ( ! succ )
@@ -146,11 +152,12 @@ public override async Task<bool> Sure()
146152 . SetAsync ( "remote.origin.sshkey" , _sshKey ) ;
147153 }
148154
149- if ( InitAndUpdateSubmodules )
155+ if ( InitAndUpdateSubmodules && ! token . IsCancellationRequested )
150156 {
151157 var submodules = await new Commands . QueryUpdatableSubmodules ( path , true ) . GetResultAsync ( ) ;
152158 if ( submodules . Count > 0 )
153159 await new Commands . Submodule ( path )
160+ . WithCancellation ( token )
154161 . Use ( log )
155162 . UpdateAsync ( submodules , true , true , false ) ;
156163 }
@@ -175,9 +182,16 @@ public override async Task<bool> Sure()
175182
176183 Welcome . Instance . Refresh ( ) ;
177184 launcher . OpenRepositoryInTab ( node , page ) ;
185+
186+ _cancellation = null ;
178187 return true ;
179188 }
180189
190+ public override void Terminate ( )
191+ {
192+ _cancellation ? . Cancel ( ) ;
193+ }
194+
181195 private void CollectGroups ( List < RepositoryNode > outs , List < RepositoryNode > collections )
182196 {
183197 foreach ( var node in collections )
@@ -199,5 +213,6 @@ private void CollectGroups(List<RepositoryNode> outs, List<RepositoryNode> colle
199213 private string _extraArgs = string . Empty ;
200214 private RepositoryNode _selectedGroup = null ;
201215 private int _bookmark = 0 ;
216+ private CancellationTokenSource _cancellation = null ;
202217 }
203218}
0 commit comments