Skip to content

Commit 10bec54

Browse files
committed
fix: refresh after application operations in manual mode
1 parent 4b8ee3c commit 10bec54

4 files changed

Lines changed: 70 additions & 1 deletion

File tree

‎src/Models/Watcher.cs‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public LockContext(Watcher target)
1919

2020
public void Dispose()
2121
{
22-
Interlocked.Decrement(ref _target._lockCount);
22+
var target = Interlocked.Exchange(ref _target, null);
23+
if (target != null && Interlocked.Decrement(ref target._lockCount) == 0)
24+
target.RefreshAfterOperation();
2325
}
2426

2527
private Watcher _target;
@@ -120,6 +122,23 @@ public IDisposable Lock()
120122
return new LockContext(this);
121123
}
122124

125+
private void RefreshAfterOperation()
126+
{
127+
// Application operations use Lock() while changing the repository. In manual mode
128+
// there are no filesystem events to trigger their final refresh, so do it explicitly
129+
// when the outermost operation completes, including failed or cancelled operations.
130+
if (_disposed || !ViewModels.Preferences.Instance.ManualRefresh)
131+
return;
132+
133+
_repo.RefreshBranches();
134+
_repo.RefreshWorktrees();
135+
_repo.RefreshWorkingCopyChanges();
136+
_repo.RefreshSubmodules();
137+
_repo.RefreshStashes();
138+
_repo.RefreshTags();
139+
_repo.RefreshCommits();
140+
}
141+
123142
public void MarkBranchUpdated()
124143
{
125144
Interlocked.Exchange(ref _updateBranch, 0);
@@ -151,6 +170,9 @@ public void Dispose()
151170
ViewModels.Preferences.Instance.PropertyChanged -= OnPreferencesChanged;
152171
lock (_syncRoot)
153172
{
173+
if (_disposed)
174+
return;
175+
154176
_disposed = true;
155177
foreach (var watcher in _watchers)
156178
{

‎src/ViewModels/AssumeUnchangedManager.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public async Task RemoveAsync(string file)
2626
{
2727
if (!string.IsNullOrEmpty(file))
2828
{
29+
using var lockWatcher = _repo.LockWatcher();
2930
var log = _repo.CreateLog("Remove Assume Unchanged File");
3031

3132
await new Commands.AssumeUnchanged(_repo.FullPath, file, false)

‎src/ViewModels/Repository.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ public async Task InstallLFSAsync()
564564

565565
public async Task<bool> TrackLFSFileAsync(string pattern, bool isFilenameMode)
566566
{
567+
using var lockWatcher = LockWatcher();
567568
var log = CreateLog("Track LFS");
568569
var succ = await new Commands.LFS(FullPath)
569570
.Use(log)
@@ -1924,6 +1925,7 @@ private async Task AutoFetchOnUIThread()
19241925
if (remotes.Count == 0)
19251926
return;
19261927

1928+
using var lockWatcher = LockWatcher();
19271929
IsAutoFetching = true;
19281930
log = CreateLog("Auto-Fetch");
19291931

‎tests/WatcherChecks/Program.cs‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,54 @@ void Check(bool condition, string message)
9595
Tick();
9696
Check(repo.Calls.Count == 0, "Manual mode must suppress queued changes and overflow recovery, even with zero throttling.");
9797

98+
preferences.RefreshThrottling = 5000;
99+
using (watcher.Lock())
100+
{
101+
using (watcher.Lock())
102+
{
103+
WorkingCopy();
104+
Git("HEAD");
105+
Tick();
106+
}
107+
108+
Check(repo.Calls.Count == 0, "Nested application operations must not refresh before the outer operation finishes.");
109+
}
110+
Check(repo.Calls.Count == 7, "Application operations must refresh in manual mode without waiting for throttling.");
111+
Check(filesystemWatchers.All(x => !x.EnableRaisingEvents), "Application refreshes must leave external filesystem hooks disabled.");
112+
repo.Calls.Clear();
113+
Tick();
114+
Check(repo.Calls.Count == 0, "Completing an application operation must not enable filesystem refreshes.");
115+
116+
try
117+
{
118+
using var operation = watcher.Lock();
119+
throw new OperationCanceledException();
120+
}
121+
catch (OperationCanceledException)
122+
{
123+
Check(repo.Calls.Count == 7, "Cancelled or failed operations must refresh any partial repository changes.");
124+
}
125+
repo.Calls.Clear();
126+
127+
var completedOperation = watcher.Lock();
128+
completedOperation.Dispose();
129+
completedOperation.Dispose();
130+
Check(repo.Calls.Count == 7, "Disposing an operation twice must refresh only once and preserve the lock count.");
131+
repo.Calls.Clear();
132+
98133
using (var manualWatcher = new Watcher(repo, root, Path.Combine(root, ".git")))
99134
{
100135
var manualFilesystemWatchers = (List<FileSystemWatcher>)typeof(Watcher).GetField("_watchers", flags).GetValue(manualWatcher);
101136
// Run the startup action explicitly as well, to check that it cannot enable hooks in manual mode.
102137
typeof(Watcher).GetMethod("UpdateWatching", flags).Invoke(manualWatcher, null);
103138
Check(manualFilesystemWatchers.All(x => !x.EnableRaisingEvents), "Repositories opened in manual mode must leave hooks disabled.");
139+
var operation = manualWatcher.Lock();
140+
manualWatcher.Dispose();
141+
operation.Dispose();
142+
Check(repo.Calls.Count == 0, "An operation finishing after the repository closes must not refresh it.");
104143
}
105144

145+
preferences.RefreshThrottling = 0;
106146
preferences.ManualRefresh = false;
107147
Check(filesystemWatchers.All(x => x.EnableRaisingEvents), "Returning to automatic mode must re-enable filesystem hooks.");
108148
Thread.Sleep(1);
@@ -111,6 +151,10 @@ void Check(bool condition, string message)
111151
repo.Calls.Clear();
112152
Tick();
113153
Check(repo.Calls.Count == 0, "Automatic recovery must refresh only once.");
154+
using (watcher.Lock())
155+
{
156+
}
157+
Check(repo.Calls.Count == 0, "Automatic mode must retain its existing event-driven refresh behavior.");
114158
Console.WriteLine("Watcher checks passed.");
115159
}
116160
finally

0 commit comments

Comments
 (0)