Skip to content

Commit 9b806b3

Browse files
authored
Code Quality: Don't wait canceled git fetch (#17330)
1 parent 28ce3cf commit 9b806b3

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/Files.App/Utils/Git/GitHelpers.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
});

src/Files.App/Views/Shells/BaseShellPage.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,16 +238,17 @@ protected async void FilesystemViewModel_DirectoryInfoUpdated(object sender, Eve
238238
? headBranch.Name
239239
: string.Empty;
240240

241+
var isGitFetchCanceled = false;
241242
if (!_gitFetch.IsCompleted)
242243
{
243244
_gitFetchToken.Cancel();
244-
await _gitFetch;
245-
_gitFetchToken.TryReset();
245+
_gitFetchToken = new CancellationTokenSource();
246+
isGitFetchCanceled = true;
246247
}
247-
if (InstanceViewModel.IsGitRepository && !GitHelpers.IsExecutingGitAction)
248+
if (InstanceViewModel.IsGitRepository && (!GitHelpers.IsExecutingGitAction || isGitFetchCanceled))
248249
{
249250
_gitFetch = Task.Run(
250-
() => GitHelpers.FetchOrigin(InstanceViewModel.GitRepositoryPath),
251+
() => GitHelpers.FetchOrigin(InstanceViewModel.GitRepositoryPath, _gitFetchToken.Token),
251252
_gitFetchToken.Token);
252253
}
253254
}

0 commit comments

Comments
 (0)