@@ -333,7 +333,7 @@ public static bool ValidateBranchNameForRepository(string branchName, string rep
333333 branch . FriendlyName . Equals ( branchName , StringComparison . OrdinalIgnoreCase ) ) ;
334334 }
335335
336- public static async void FetchOrigin ( string ? repositoryPath )
336+ public static async void FetchOrigin ( string ? repositoryPath , CancellationToken cancellationToken = default )
337337 {
338338 if ( string . IsNullOrWhiteSpace ( repositoryPath ) )
339339 return ;
@@ -359,18 +359,24 @@ public static async void FetchOrigin(string? repositoryPath)
359359
360360 await DoGitOperationAsync < GitOperationResult > ( ( ) =>
361361 {
362+ cancellationToken . ThrowIfCancellationRequested ( ) ;
363+
362364 var result = GitOperationResult . Success ;
363365 try
364366 {
365367 foreach ( var remote in repository . Network . Remotes )
366368 {
369+ cancellationToken . ThrowIfCancellationRequested ( ) ;
370+
367371 LibGit2Sharp . Commands . Fetch (
368372 repository ,
369373 remote . Name ,
370374 remote . FetchRefSpecs . Select ( rs => rs . Specification ) ,
371375 _fetchOptions ,
372376 "git fetch updated a ref" ) ;
373377 }
378+
379+ cancellationToken . ThrowIfCancellationRequested ( ) ;
374380 }
375381 catch ( Exception ex )
376382 {
@@ -384,6 +390,10 @@ await DoGitOperationAsync<GitOperationResult>(() =>
384390
385391 MainWindow . Instance . DispatcherQueue . TryEnqueue ( ( ) =>
386392 {
393+ if ( cancellationToken . IsCancellationRequested )
394+ // Do nothing because the operation was cancelled and another fetch may be in progress
395+ return ;
396+
387397 IsExecutingGitAction = false ;
388398 GitFetchCompleted ? . Invoke ( null , EventArgs . Empty ) ;
389399 } ) ;
0 commit comments