Skip to content

Commit 4b8ee3c

Browse files
committed
feat: add manual repository refresh mode
1 parent 76c7c80 commit 4b8ee3c

6 files changed

Lines changed: 120 additions & 14 deletions

File tree

‎src/Models/Watcher.cs‎

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.ComponentModel;
34
using System.IO;
45
using System.Threading;
56
using System.Threading.Tasks;
@@ -79,20 +80,39 @@ public Watcher(IRepository repo, string fullpath, string gitDir)
7980
}
8081

8182
_timer = new Timer(Tick, null, 100, 100);
83+
ViewModels.Preferences.Instance.PropertyChanged += OnPreferencesChanged;
8284

8385
// Starts filesystem watchers in another thread to avoid UI blocking
84-
Task.Run(() =>
86+
Task.Run(UpdateWatching);
87+
}
88+
89+
private void UpdateWatching()
90+
{
91+
lock (_syncRoot)
8592
{
93+
if (_disposed)
94+
return;
95+
8696
try
8797
{
8898
foreach (var watcher in _watchers)
89-
watcher.EnableRaisingEvents = true;
99+
watcher.EnableRaisingEvents = !ViewModels.Preferences.Instance.ManualRefresh;
90100
}
91101
catch
92102
{
93-
// Ignore exceptions. This may occur while `Dispose` is called.
103+
// Ignore failures while starting filesystem watchers.
94104
}
95-
});
105+
}
106+
}
107+
108+
private void OnPreferencesChanged(object sender, PropertyChangedEventArgs e)
109+
{
110+
if (e.PropertyName != nameof(ViewModels.Preferences.ManualRefresh))
111+
return;
112+
113+
UpdateWatching();
114+
if (!ViewModels.Preferences.Instance.ManualRefresh)
115+
OnWatcherError(null, null); // Reload changes missed while filesystem watchers were disabled.
96116
}
97117

98118
public IDisposable Lock()
@@ -128,20 +148,25 @@ public void MarkSubmodulesUpdated()
128148

129149
public void Dispose()
130150
{
131-
foreach (var watcher in _watchers)
151+
ViewModels.Preferences.Instance.PropertyChanged -= OnPreferencesChanged;
152+
lock (_syncRoot)
132153
{
133-
watcher.EnableRaisingEvents = false;
134-
watcher.Dispose();
135-
}
154+
_disposed = true;
155+
foreach (var watcher in _watchers)
156+
{
157+
watcher.EnableRaisingEvents = false;
158+
watcher.Dispose();
159+
}
136160

137-
_watchers.Clear();
138-
_timer.Dispose();
139-
_timer = null;
161+
_watchers.Clear();
162+
_timer.Dispose();
163+
_timer = null;
164+
}
140165
}
141166

142167
private void Tick(object sender)
143168
{
144-
if (Interlocked.Read(ref _lockCount) > 0)
169+
if (ViewModels.Preferences.Instance.ManualRefresh || Interlocked.Read(ref _lockCount) > 0)
145170
return;
146171

147172
var now = DateTime.Now.ToFileTime();
@@ -254,6 +279,9 @@ private void OnRepositoryChanged(object o, FileSystemEventArgs e)
254279
/// </summary>
255280
private void OnWatcherError(object o, ErrorEventArgs e)
256281
{
282+
if (ViewModels.Preferences.Instance.ManualRefresh)
283+
return;
284+
257285
var desired = RecordChange();
258286
Interlocked.Exchange(ref _updateBranch, desired);
259287
Interlocked.Exchange(ref _updateTags, desired);
@@ -288,6 +316,9 @@ private void OnWorkingCopyChanged(object o, FileSystemEventArgs e)
288316

289317
private void HandleGitDirFileChanged(string name)
290318
{
319+
if (ViewModels.Preferences.Instance.ManualRefresh)
320+
return;
321+
291322
if (name.Contains("fsmonitor--daemon/", StringComparison.Ordinal) ||
292323
name.EndsWith(".lock", StringComparison.Ordinal) ||
293324
name.StartsWith("lfs/", StringComparison.Ordinal))
@@ -346,6 +377,9 @@ private void HandleGitDirFileChanged(string name)
346377

347378
private void HandleWorkingCopyFileChanged(string name, string fullpath)
348379
{
380+
if (ViewModels.Preferences.Instance.ManualRefresh)
381+
return;
382+
349383
if (name.StartsWith(".vs/", StringComparison.Ordinal))
350384
return;
351385

@@ -386,6 +420,8 @@ private long RecordChange()
386420
}
387421

388422
private readonly IRepository _repo;
423+
private readonly object _syncRoot = new object();
424+
private bool _disposed;
389425
private readonly string _root;
390426
private List<FileSystemWatcher> _watchers;
391427
private Timer _timer;

‎src/Resources/Locales/en_US.axaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@
707707
<x:String x:Key="Text.Preferences.General.DateFormat" xml:space="preserve">Date Format</x:String>
708708
<x:String x:Key="Text.Preferences.General.EnableCompactFolders" xml:space="preserve">Enable compact folders in changes tree</x:String>
709709
<x:String x:Key="Text.Preferences.General.Locale" xml:space="preserve">Language</x:String>
710+
<x:String x:Key="Text.Preferences.General.ManualRefresh" xml:space="preserve">Manual refresh</x:String>
710711
<x:String x:Key="Text.Preferences.General.MaxHistoryCommits" xml:space="preserve">History Commits</x:String>
711712
<x:String x:Key="Text.Preferences.General.RefreshThrottling" xml:space="preserve">Refresh throttling</x:String>
712713
<x:String x:Key="Text.Preferences.General.ShowChangesPageByDefault" xml:space="preserve">Show `LOCAL CHANGES` page by default</x:String>

‎src/ViewModels/Preferences.cs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ public int RefreshThrottling
141141
set => SetProperty(ref _refreshThrottling, Math.Clamp(value, 0, 5000));
142142
}
143143

144+
public bool ManualRefresh
145+
{
146+
get => _manualRefresh;
147+
set => SetProperty(ref _manualRefresh, value);
148+
}
149+
144150
public int SubjectGuideLength
145151
{
146152
get => _subjectGuideLength;
@@ -808,6 +814,7 @@ private bool RemoveInvalidRepositoriesRecursive(List<RepositoryNode> collection)
808814

809815
private int _maxHistoryCommits = 20000;
810816
private int _refreshThrottling = 1000;
817+
private bool _manualRefresh = false;
811818
private int _subjectGuideLength = 50;
812819
private bool _useFixedTabWidth = true;
813820
private bool _useAutoHideScrollBars = true;

‎src/Views/Preferences.axaml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,12 @@
145145
Text="{DynamicResource Text.Preferences.General.RefreshThrottling}"
146146
HorizontalAlignment="Right"
147147
Margin="0,0,16,0"/>
148-
<Grid Grid.Row="5" Grid.Column="1" ColumnDefinitions="*,64">
148+
<Grid Grid.Row="5" Grid.Column="1" ColumnDefinitions="*,64,Auto">
149149
<Slider Grid.Column="0"
150150
Minimum="0" Maximum="5000"
151151
TickPlacement="BottomRight" TickFrequency="100"
152152
IsSnapToTickEnabled="True"
153+
IsEnabled="{Binding !ManualRefresh}"
153154
VerticalAlignment="Center"
154155
Foreground="{DynamicResource Brush.Border1}"
155156
Value="{Binding RefreshThrottling, Mode=TwoWay}"/>
@@ -158,6 +159,10 @@
158159
HorizontalAlignment="Right" VerticalAlignment="Center"
159160
Foreground="{DynamicResource Brush.FG1}"
160161
Text="{Binding RefreshThrottling, StringFormat='{}{0} ms'}"/>
162+
<CheckBox Grid.Column="2"
163+
Margin="16,0,0,0"
164+
Content="{DynamicResource Text.Preferences.General.ManualRefresh}"
165+
IsChecked="{Binding ManualRefresh, Mode=TwoWay}"/>
161166
</Grid>
162167

163168
<CheckBox Grid.Row="6" Grid.Column="1"

‎src/Views/RepositoryToolbar.axaml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
2424
</StackPanel>
2525

2626
<StackPanel Grid.Column="1" Orientation="Horizontal">
27+
<Button Classes="icon_button" Width="32" Margin="0,0,16,0"
28+
Command="{Binding RefreshAll}"
29+
IsVisible="{Binding Source={x:Static vm:Preferences.Instance}, Path=ManualRefresh}">
30+
<ToolTip.Tip>
31+
<TextBlock>
32+
<Run Text="{DynamicResource Text.PageTabBar.Tab.Refresh}"/>
33+
<Run Text=" F5" FontSize="11" Foreground="{DynamicResource MenuFlyoutItemKeyboardAcceleratorTextForeground}"/>
34+
</TextBlock>
35+
</ToolTip.Tip>
36+
<Path Width="14" Height="14" Data="{StaticResource Icons.Loading}"/>
37+
</Button>
38+
2739
<Button Classes="icon_button" Width="32" Tapped="Fetch">
2840
<ToolTip.Tip>
2941
<StackPanel Orientation="Vertical">

‎tests/WatcherChecks/Program.cs‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,36 @@ void Check(bool condition, string message)
8181
Thread.Sleep(240);
8282
Tick();
8383
Check(repo.Calls.Count == 7, "Watcher overflow recovery must reload all categories.");
84+
repo.Calls.Clear();
85+
86+
var preferences = SourceGit.ViewModels.Preferences.Instance;
87+
var filesystemWatchers = (List<FileSystemWatcher>)typeof(Watcher).GetField("_watchers", flags).GetValue(watcher);
88+
WorkingCopy();
89+
preferences.ManualRefresh = true;
90+
Check(filesystemWatchers.All(x => !x.EnableRaisingEvents), "Manual mode must disable filesystem hooks immediately.");
91+
preferences.RefreshThrottling = 0;
92+
WorkingCopy();
93+
Git("HEAD");
94+
Invoke("OnWatcherError", null, new ErrorEventArgs(new InternalBufferOverflowException()));
95+
Tick();
96+
Check(repo.Calls.Count == 0, "Manual mode must suppress queued changes and overflow recovery, even with zero throttling.");
97+
98+
using (var manualWatcher = new Watcher(repo, root, Path.Combine(root, ".git")))
99+
{
100+
var manualFilesystemWatchers = (List<FileSystemWatcher>)typeof(Watcher).GetField("_watchers", flags).GetValue(manualWatcher);
101+
// Run the startup action explicitly as well, to check that it cannot enable hooks in manual mode.
102+
typeof(Watcher).GetMethod("UpdateWatching", flags).Invoke(manualWatcher, null);
103+
Check(manualFilesystemWatchers.All(x => !x.EnableRaisingEvents), "Repositories opened in manual mode must leave hooks disabled.");
104+
}
105+
106+
preferences.ManualRefresh = false;
107+
Check(filesystemWatchers.All(x => x.EnableRaisingEvents), "Returning to automatic mode must re-enable filesystem hooks.");
108+
Thread.Sleep(1);
109+
Tick();
110+
Check(repo.Calls.Count == 7, "Returning to automatic mode must reload changes missed while hooks were disabled.");
111+
repo.Calls.Clear();
112+
Tick();
113+
Check(repo.Calls.Count == 0, "Automatic recovery must refresh only once.");
84114
Console.WriteLine("Watcher checks passed.");
85115
}
86116
finally
@@ -104,9 +134,24 @@ sealed class TestRepository : IRepository
104134
namespace SourceGit.ViewModels
105135
{
106136
// Keep these watcher checks independent of the desktop application and user settings.
107-
public sealed class Preferences
137+
public sealed class Preferences : System.ComponentModel.INotifyPropertyChanged
108138
{
109139
public static Preferences Instance { get; } = new();
110140
public int RefreshThrottling { get; set; } = 1000;
141+
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
142+
public bool ManualRefresh
143+
{
144+
get => _manualRefresh;
145+
set
146+
{
147+
if (_manualRefresh == value)
148+
return;
149+
150+
_manualRefresh = value;
151+
PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(ManualRefresh)));
152+
}
153+
}
154+
155+
private bool _manualRefresh;
111156
}
112157
}

0 commit comments

Comments
 (0)