Skip to content

Commit 8190a8f

Browse files
committed
enhance: force kill the previous git status process (#2358)
Signed-off-by: leo <longshuang@msn.cn>
1 parent 90ff73b commit 8190a8f

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

src/Commands/QueryLocalChanges.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,24 @@ public QueryLocalChanges(string repo, bool includeUntracked = true, bool noOptio
3434
try
3535
{
3636
using var proc = new Process();
37+
var captured = new CapturedProcess() { Process = proc };
38+
var capturedLock = new object();
39+
3740
proc.StartInfo = CreateGitStartInfo(true);
3841
proc.Start();
3942

43+
if (CancellationToken.CanBeCanceled)
44+
{
45+
CancellationToken.Register(() =>
46+
{
47+
lock (capturedLock)
48+
{
49+
if (captured is { Process: { HasExited: false } })
50+
captured.Process.Kill(true);
51+
}
52+
});
53+
}
54+
4055
while (await proc.StandardOutput.ReadLineAsync().ConfigureAwait(false) is { } line)
4156
{
4257
var match = REG_FORMAT().Match(line);
@@ -165,6 +180,11 @@ public QueryLocalChanges(string repo, bool includeUntracked = true, bool noOptio
165180
if (change.Index != Models.ChangeState.None || change.WorkTree != Models.ChangeState.None)
166181
outs.Add(change);
167182
}
183+
184+
lock (capturedLock)
185+
{
186+
captured.Process = null;
187+
}
168188
}
169189
catch
170190
{
@@ -173,5 +193,10 @@ public QueryLocalChanges(string repo, bool includeUntracked = true, bool noOptio
173193

174194
return outs;
175195
}
196+
197+
private class CapturedProcess
198+
{
199+
public Process Process { get; set; } = null;
200+
}
176201
}
177202
}

src/ViewModels/Repository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,7 @@ public void RefreshWorkingCopyChanges()
13301330
Task.Run(async () =>
13311331
{
13321332
var changes = await new Commands.QueryLocalChanges(FullPath, _uiStates.IncludeUntrackedInLocalChanges, noOptionalLocks)
1333+
.WithCancellation(token)
13331334
.GetResultAsync()
13341335
.ConfigureAwait(false);
13351336

0 commit comments

Comments
 (0)