-
Notifications
You must be signed in to change notification settings - Fork 386
Expand file tree
/
Copy pathWatcher.cs
More file actions
364 lines (318 loc) · 12.8 KB
/
Watcher.cs
File metadata and controls
364 lines (318 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace SourceGit.Models
{
public class Watcher : IDisposable
{
public class LockContext : IDisposable
{
public LockContext(Watcher target)
{
_target = target;
Interlocked.Increment(ref _target._lockCount);
}
public void Dispose()
{
Interlocked.Decrement(ref _target._lockCount);
}
private Watcher _target;
}
public Watcher(IRepository repo, string fullpath, string gitDir)
{
_repo = repo;
_root = new DirectoryInfo(fullpath).FullName;
_watchers = new List<FileSystemWatcher>();
var testGitDir = new DirectoryInfo(Path.Combine(fullpath, ".git")).FullName;
var desiredDir = new DirectoryInfo(gitDir).FullName;
if (testGitDir.Equals(desiredDir, StringComparison.Ordinal))
{
var combined = new FileSystemWatcher();
combined.Path = fullpath;
combined.Filter = "*";
combined.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.DirectoryName | NotifyFilters.FileName;
combined.IncludeSubdirectories = true;
combined.Created += OnRepositoryChanged;
combined.Renamed += OnRepositoryChanged;
combined.Changed += OnRepositoryChanged;
combined.Deleted += OnRepositoryChanged;
combined.EnableRaisingEvents = false;
_watchers.Add(combined);
}
else
{
var wc = new FileSystemWatcher();
wc.Path = fullpath;
wc.Filter = "*";
wc.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.DirectoryName | NotifyFilters.FileName;
wc.IncludeSubdirectories = true;
wc.Created += OnWorkingCopyChanged;
wc.Renamed += OnWorkingCopyChanged;
wc.Changed += OnWorkingCopyChanged;
wc.Deleted += OnWorkingCopyChanged;
wc.EnableRaisingEvents = false;
var git = new FileSystemWatcher();
git.Path = gitDir;
git.Filter = "*";
git.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.DirectoryName | NotifyFilters.FileName;
git.IncludeSubdirectories = true;
git.Created += OnGitDirChanged;
git.Renamed += OnGitDirChanged;
git.Changed += OnGitDirChanged;
git.Deleted += OnGitDirChanged;
git.EnableRaisingEvents = false;
_watchers.Add(wc);
_watchers.Add(git);
}
_timer = new Timer(Tick, null, 100, 100);
// Starts filesystem watchers in another thread to avoid UI blocking
Task.Run(() =>
{
try
{
foreach (var watcher in _watchers)
watcher.EnableRaisingEvents = true;
}
catch
{
// Ignore exceptions. This may occur while `Dispose` is called.
}
});
}
public IDisposable Lock()
{
return new LockContext(this);
}
public void MarkBranchUpdated()
{
Interlocked.Exchange(ref _updateBranch, 0);
Interlocked.Exchange(ref _updateWC, 0);
}
public void MarkTagUpdated()
{
Interlocked.Exchange(ref _updateTags, 0);
}
public void MarkWorkingCopyUpdated()
{
Interlocked.Exchange(ref _updateWC, 0);
}
public void MarkStashUpdated()
{
Interlocked.Exchange(ref _updateStashes, 0);
}
public void MarkSubmodulesUpdated()
{
Interlocked.Exchange(ref _updateSubmodules, 0);
}
public void Dispose()
{
foreach (var watcher in _watchers)
{
watcher.EnableRaisingEvents = false;
watcher.Dispose();
}
_watchers.Clear();
_timer.Dispose();
_timer = null;
}
private void Tick(object sender)
{
if (Interlocked.Read(ref _lockCount) > 0)
return;
var now = DateTime.Now.ToFileTime();
var refreshCommits = false;
var refreshSubmodules = false;
var refreshWC = false;
var oldUpdateBranch = Interlocked.Exchange(ref _updateBranch, -1);
if (oldUpdateBranch > 0)
{
if (now > oldUpdateBranch)
{
refreshCommits = true;
refreshSubmodules = _repo.MayHaveSubmodules();
refreshWC = true;
_repo.RefreshBranches();
_repo.RefreshWorktrees();
}
else
{
Interlocked.CompareExchange(ref _updateBranch, oldUpdateBranch, -1);
}
}
if (refreshWC)
{
Interlocked.Exchange(ref _updateWC, -1);
_repo.RefreshWorkingCopyChanges();
}
else
{
var oldUpdateWC = Interlocked.Exchange(ref _updateWC, -1);
if (oldUpdateWC > 0)
{
if (now > oldUpdateWC)
_repo.RefreshWorkingCopyChanges();
else
Interlocked.CompareExchange(ref _updateWC, oldUpdateWC, -1);
}
}
if (refreshSubmodules)
{
Interlocked.Exchange(ref _updateSubmodules, -1);
_repo.RefreshSubmodules();
}
else
{
var oldUpdateSubmodule = Interlocked.Exchange(ref _updateSubmodules, -1);
if (oldUpdateSubmodule > 0)
{
if (now > oldUpdateSubmodule)
_repo.RefreshSubmodules();
else
Interlocked.CompareExchange(ref _updateSubmodules, oldUpdateSubmodule, -1);
}
}
var oldUpdateStashes = Interlocked.Exchange(ref _updateStashes, -1);
if (oldUpdateStashes > 0)
{
if (now > oldUpdateStashes)
_repo.RefreshStashes();
else
Interlocked.CompareExchange(ref _updateStashes, oldUpdateStashes, -1);
}
var oldUpdateTags = Interlocked.Exchange(ref _updateTags, -1);
if (oldUpdateTags > 0)
{
if (now > oldUpdateTags)
{
refreshCommits = true;
_repo.RefreshTags();
}
else
{
Interlocked.CompareExchange(ref _updateTags, oldUpdateTags, -1);
}
}
if (refreshCommits)
_repo.RefreshCommits();
}
private void OnRepositoryChanged(object o, FileSystemEventArgs e)
{
if (string.IsNullOrEmpty(e.Name) || e.Name.Equals(".git", StringComparison.Ordinal))
return;
var name = e.Name.Replace('\\', '/').TrimEnd('/');
if (name.EndsWith("/.git", StringComparison.Ordinal))
return;
if (name.StartsWith(".git/", StringComparison.Ordinal))
HandleGitDirFileChanged(name.Substring(5));
else
HandleWorkingCopyFileChanged(name, e.FullPath);
}
private void OnGitDirChanged(object o, FileSystemEventArgs e)
{
if (string.IsNullOrEmpty(e.Name))
return;
var name = e.Name.Replace('\\', '/').TrimEnd('/');
HandleGitDirFileChanged(name);
}
private void OnWorkingCopyChanged(object o, FileSystemEventArgs e)
{
if (string.IsNullOrEmpty(e.Name))
return;
var name = e.Name.Replace('\\', '/').TrimEnd('/');
if (name.Equals(".git", StringComparison.Ordinal) ||
name.StartsWith(".git/", StringComparison.Ordinal) ||
name.EndsWith("/.git", StringComparison.Ordinal))
return;
HandleWorkingCopyFileChanged(name, e.FullPath);
}
private void HandleGitDirFileChanged(string name)
{
if (name.Contains("fsmonitor--daemon/", StringComparison.Ordinal) ||
name.EndsWith(".lock", StringComparison.Ordinal) ||
name.StartsWith("lfs/", StringComparison.Ordinal))
return;
if (name.StartsWith("modules", StringComparison.Ordinal))
{
if (name.EndsWith("/HEAD", StringComparison.Ordinal) ||
name.EndsWith("/ORIG_HEAD", StringComparison.Ordinal))
{
var desired = DateTime.Now.AddSeconds(1).ToFileTime();
Interlocked.Exchange(ref _updateSubmodules, desired);
Interlocked.Exchange(ref _updateWC, desired);
}
}
else if (name.Equals("MERGE_HEAD", StringComparison.Ordinal) ||
name.Equals("AUTO_MERGE", StringComparison.Ordinal))
{
if (_repo.MayHaveSubmodules())
Interlocked.Exchange(ref _updateSubmodules, DateTime.Now.AddSeconds(1).ToFileTime());
}
else if (name.StartsWith("refs/tags", StringComparison.Ordinal))
{
Interlocked.Exchange(ref _updateTags, DateTime.Now.AddSeconds(.5).ToFileTime());
}
else if (name.StartsWith("refs/stash", StringComparison.Ordinal))
{
Interlocked.Exchange(ref _updateStashes, DateTime.Now.AddSeconds(.5).ToFileTime());
}
else if (name.Equals("HEAD", StringComparison.Ordinal) ||
name.Equals("BISECT_START", StringComparison.Ordinal) ||
name.StartsWith("refs/heads/", StringComparison.Ordinal) ||
name.StartsWith("refs/remotes/", StringComparison.Ordinal) ||
(name.StartsWith("worktrees/", StringComparison.Ordinal) && name.EndsWith("/HEAD", StringComparison.Ordinal)))
{
Interlocked.Exchange(ref _updateBranch, DateTime.Now.AddSeconds(.5).ToFileTime());
}
else if (name.StartsWith("reftable/", StringComparison.Ordinal))
{
var desired = DateTime.Now.AddSeconds(.5).ToFileTime();
Interlocked.Exchange(ref _updateBranch, desired);
Interlocked.Exchange(ref _updateTags, desired);
Interlocked.Exchange(ref _updateStashes, desired);
}
else if (name.StartsWith("objects/", StringComparison.Ordinal) || name.Equals("index", StringComparison.Ordinal))
{
Interlocked.Exchange(ref _updateWC, DateTime.Now.AddSeconds(1).ToFileTime());
}
}
private void HandleWorkingCopyFileChanged(string name, string fullpath)
{
if (name.StartsWith(".vs/", StringComparison.Ordinal))
return;
if (name.Equals(".gitmodules", StringComparison.Ordinal))
{
var desired = DateTime.Now.AddSeconds(1).ToFileTime();
Interlocked.Exchange(ref _updateSubmodules, desired);
Interlocked.Exchange(ref _updateWC, desired);
return;
}
var dir = Directory.Exists(fullpath) ? fullpath : Path.GetDirectoryName(fullpath);
if (IsInSubmodule(dir))
{
Interlocked.Exchange(ref _updateSubmodules, DateTime.Now.AddSeconds(1).ToFileTime());
return;
}
Interlocked.Exchange(ref _updateWC, DateTime.Now.AddSeconds(1).ToFileTime());
}
private bool IsInSubmodule(string folder)
{
if (string.IsNullOrEmpty(folder) || folder.Equals(_root, StringComparison.Ordinal))
return false;
if (File.Exists($"{folder}/.git"))
return true;
return IsInSubmodule(Path.GetDirectoryName(folder));
}
private readonly IRepository _repo;
private readonly string _root;
private List<FileSystemWatcher> _watchers;
private Timer _timer;
private long _lockCount;
private long _updateWC;
private long _updateBranch;
private long _updateSubmodules;
private long _updateStashes;
private long _updateTags;
}
}