Skip to content
Open
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
13 changes: 13 additions & 0 deletions src/Commands/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,19 @@ protected ProcessStartInfo CreateGitStartInfo(bool redirect)
return start;
}

protected CancellationTokenRegistration TerminateOnCancellation(Process proc)
{
if (!CancellationToken.CanBeCanceled)
return default;

// Disposing the returned registration waits for a running callback, so `proc` is always valid inside it.
return CancellationToken.Register(() =>
{
if (!proc.HasExited)
Native.OS.TerminateProcess(proc);
});
}

protected void RaiseException(string error)
{
Models.Notification.Send(Context, error, true);
Expand Down
9 changes: 8 additions & 1 deletion src/Commands/QueryCommits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public QueryCommits(string repo, string filter, Models.CommitSearchMethod method
proc.StartInfo = CreateGitStartInfo(true);
proc.Start();

using var cancellation = TerminateOnCancellation(proc);

var findHead = false;
while (await proc.StandardOutput.ReadLineAsync().ConfigureAwait(false) is { } line)
{
Expand All @@ -82,9 +84,13 @@ public QueryCommits(string repo, string filter, Models.CommitSearchMethod method

await proc.WaitForExitAsync().ConfigureAwait(false);

if (CancellationToken.IsCancellationRequested)
return commits;

if (_markMerged && !findHead && commits.Count > 0)
{
var set = await new QueryCurrentBranchCommitHashes(WorkingDirectory, commits[^1].CommitterTime)
.WithCancellation(CancellationToken)
.GetResultAsync()
.ConfigureAwait(false);

Expand All @@ -100,7 +106,8 @@ public QueryCommits(string repo, string filter, Models.CommitSearchMethod method
}
catch (Exception e)
{
RaiseException($"Failed to query commits. Reason: {e.Message}");
if (!CancellationToken.IsCancellationRequested)
RaiseException($"Failed to query commits. Reason: {e.Message}");
}

return commits;
Expand Down
2 changes: 2 additions & 0 deletions src/Commands/QueryCurrentBranchCommitHashes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public async Task<HashSet<string>> GetResultAsync()
proc.StartInfo = CreateGitStartInfo(true);
proc.Start();

using var cancellation = TerminateOnCancellation(proc);

while (await proc.StandardOutput.ReadLineAsync().ConfigureAwait(false) is { Length: > 8 } line)
outs.Add(line);

Expand Down
1 change: 1 addition & 0 deletions src/ViewModels/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,7 @@ public void RefreshCommits()
}

var commits = await new Commands.QueryCommits(FullPath, builder.ToString())
.WithCancellation(token)
.GetResultAsync()
.ConfigureAwait(false);

Expand Down