Skip to content

Code Quality: Don't wait canceled git fetch #17330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Files.App/Utils/Git/GitHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public static bool ValidateBranchNameForRepository(string branchName, string rep
branch.FriendlyName.Equals(branchName, StringComparison.OrdinalIgnoreCase));
}

public static async void FetchOrigin(string? repositoryPath)
public static async void FetchOrigin(string? repositoryPath, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(repositoryPath))
return;
Expand All @@ -359,18 +359,24 @@ public static async void FetchOrigin(string? repositoryPath)

await DoGitOperationAsync<GitOperationResult>(() =>
{
cancellationToken.ThrowIfCancellationRequested();

var result = GitOperationResult.Success;
try
{
foreach (var remote in repository.Network.Remotes)
{
cancellationToken.ThrowIfCancellationRequested();

LibGit2Sharp.Commands.Fetch(
repository,
remote.Name,
remote.FetchRefSpecs.Select(rs => rs.Specification),
_fetchOptions,
"git fetch updated a ref");
}

cancellationToken.ThrowIfCancellationRequested();
}
catch (Exception ex)
{
Expand All @@ -384,6 +390,10 @@ await DoGitOperationAsync<GitOperationResult>(() =>

MainWindow.Instance.DispatcherQueue.TryEnqueue(() =>
{
if (cancellationToken.IsCancellationRequested)
// Do nothing because the operation was cancelled and another fetch may be in progress
return;

IsExecutingGitAction = false;
GitFetchCompleted?.Invoke(null, EventArgs.Empty);
});
Expand Down
9 changes: 5 additions & 4 deletions src/Files.App/Views/Shells/BaseShellPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,17 @@ protected async void FilesystemViewModel_DirectoryInfoUpdated(object sender, Eve
? headBranch.Name
: string.Empty;

var isGitFetchCanceled = false;
if (!_gitFetch.IsCompleted)
{
_gitFetchToken.Cancel();
await _gitFetch;
_gitFetchToken.TryReset();
_gitFetchToken = new CancellationTokenSource();
isGitFetchCanceled = true;
}
if (InstanceViewModel.IsGitRepository && !GitHelpers.IsExecutingGitAction)
if (InstanceViewModel.IsGitRepository && (!GitHelpers.IsExecutingGitAction || isGitFetchCanceled))
{
_gitFetch = Task.Run(
() => GitHelpers.FetchOrigin(InstanceViewModel.GitRepositoryPath),
() => GitHelpers.FetchOrigin(InstanceViewModel.GitRepositoryPath, _gitFetchToken.Token),
_gitFetchToken.Token);
}
}
Expand Down
Loading