Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit c4938ed

Browse files
Batching events from the RepositoryWatcher
1 parent c4d410f commit c4938ed

File tree

1 file changed

+157
-135
lines changed

1 file changed

+157
-135
lines changed

src/GitHub.Api/Events/RepositoryWatcher.cs

Lines changed: 157 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,22 @@ public int CheckAndProcessEvents()
149149
if (fileEvents.Length > 0)
150150
{
151151
Logger.Trace("Processing {0} Events", fileEvents.Length);
152+
ProcessEvents(fileEvents);
152153
}
153154

155+
processingEvents = false;
156+
signalProcessingEventsDone.Set();
157+
return lastCountOfProcessedEvents;
158+
}
159+
160+
private void ProcessEvents(Event[] fileEvents)
161+
{
162+
var configChanged = false;
163+
var headChanged = false;
154164
var repositoryChanged = false;
165+
var indexChanged = false;
166+
167+
string headContent = null;
155168

156169
foreach (var fileEvent in fileEvents)
157170
{
@@ -180,175 +193,184 @@ public int CheckAndProcessEvents()
180193
// handling events in .git/*
181194
if (fileA.IsChildOf(paths.DotGitPath))
182195
{
183-
HandleEventInDotGit(fileEvent, fileA, fileB);
184-
}
185-
else
186-
{
187-
if (repositoryChanged || ignoredPaths.Any(ignoredPath => fileA.IsChildOf(ignoredPath)))
196+
if (fileA.Equals(paths.DotGitConfig))
188197
{
189-
continue;
190-
}
191-
192-
repositoryChanged = true;
193-
}
194-
lastCountOfProcessedEvents++;
195-
}
196-
197-
if (repositoryChanged)
198-
{
199-
Logger.Trace("RepositoryChanged");
200-
RepositoryChanged?.Invoke();
201-
}
202-
203-
processingEvents = false;
204-
signalProcessingEventsDone.Set();
205-
return lastCountOfProcessedEvents;
206-
}
198+
Logger.Trace("ConfigChanged");
207199

208-
private void HandleEventInDotGit(Event fileEvent, NPath fileA, NPath fileB = null)
209-
{
210-
if (fileA.Equals(paths.DotGitConfig))
211-
{
212-
Logger.Trace("ConfigChanged");
213-
214-
ConfigChanged?.Invoke();
215-
}
216-
else if (fileA.Equals(paths.DotGitHead))
217-
{
218-
string headContent = null;
219-
if (fileEvent.Type != EventType.DELETED)
220-
{
221-
headContent = paths.DotGitHead.ReadAllLines().FirstOrDefault();
222-
}
223-
224-
Logger.Trace("HeadChanged: {0}", headContent ?? "[null]");
225-
HeadChanged?.Invoke(headContent);
226-
}
227-
else if (fileA.Equals(paths.DotGitIndex))
228-
{
229-
Logger.Trace("IndexChanged");
230-
IndexChanged?.Invoke();
231-
}
232-
else if (fileA.IsChildOf(paths.RemotesPath))
233-
{
234-
var relativePath = fileA.RelativeTo(paths.RemotesPath);
235-
var relativePathElements = relativePath.Elements.ToArray();
236-
237-
if (!relativePathElements.Any())
238-
{
239-
return;
240-
}
241-
242-
var origin = relativePathElements[0];
200+
configChanged = true;
201+
}
202+
else if (fileA.Equals(paths.DotGitHead))
203+
{
204+
if (fileEvent.Type != EventType.DELETED)
205+
{
206+
headContent = paths.DotGitHead.ReadAllLines().FirstOrDefault();
207+
}
243208

244-
if (fileEvent.Type == EventType.DELETED)
245-
{
246-
if (fileA.ExtensionWithDot == ".lock")
209+
Logger.Trace("HeadChanged: {0}", headContent ?? "[null]");
210+
headChanged = true;
211+
}
212+
else if (fileA.Equals(paths.DotGitIndex))
247213
{
248-
return;
214+
Logger.Trace("IndexChanged");
215+
indexChanged = true;
249216
}
217+
else if (fileA.IsChildOf(paths.RemotesPath))
218+
{
219+
var relativePath = fileA.RelativeTo(paths.RemotesPath);
220+
var relativePathElements = relativePath.Elements.ToArray();
250221

251-
var branch = string.Join(@"/", relativePathElements.Skip(1).ToArray());
222+
if (!relativePathElements.Any())
223+
{
224+
continue;
225+
}
252226

253-
Logger.Trace("RemoteBranchDeleted: {0}/{1}", origin, branch);
254-
RemoteBranchDeleted?.Invoke(origin, branch);
255-
}
256-
else if (fileEvent.Type == EventType.RENAMED)
257-
{
258-
if (fileA.ExtensionWithDot != ".lock")
259-
{
260-
return;
261-
}
227+
var origin = relativePathElements[0];
262228

263-
if (fileB != null && fileB.FileExists())
264-
{
265-
if (fileA.FileNameWithoutExtension == fileB.FileNameWithoutExtension)
229+
if (fileEvent.Type == EventType.DELETED)
266230
{
267-
var branchPathElement = relativePathElements.Skip(1)
268-
.Take(relativePathElements.Length-2)
269-
.Union(new [] { fileA.FileNameWithoutExtension }).ToArray();
231+
if (fileA.ExtensionWithDot == ".lock")
232+
{
233+
continue;
234+
}
270235

271-
var branch = string.Join(@"/", branchPathElement);
236+
var branch = string.Join(@"/", relativePathElements.Skip(1).ToArray());
272237

273-
Logger.Trace("RemoteBranchCreated: {0}/{1}", origin, branch);
274-
RemoteBranchCreated?.Invoke(origin, branch);
238+
Logger.Trace("RemoteBranchDeleted: {0}/{1}", origin, branch);
239+
RemoteBranchDeleted?.Invoke(origin, branch);
275240
}
276-
}
277-
}
278-
}
279-
else if (fileA.IsChildOf(paths.BranchesPath))
280-
{
281-
if (fileEvent.Type == EventType.MODIFIED)
282-
{
283-
if (fileA.DirectoryExists())
284-
{
285-
return;
286-
}
241+
else if (fileEvent.Type == EventType.RENAMED)
242+
{
243+
if (fileA.ExtensionWithDot != ".lock")
244+
{
245+
continue;
246+
}
287247

288-
if (fileA.ExtensionWithDot == ".lock")
289-
{
290-
return;
291-
}
248+
if (fileB != null && fileB.FileExists())
249+
{
250+
if (fileA.FileNameWithoutExtension == fileB.FileNameWithoutExtension)
251+
{
252+
var branchPathElement = relativePathElements
253+
.Skip(1).Take(relativePathElements.Length - 2)
254+
.Union(new[] { fileA.FileNameWithoutExtension }).ToArray();
292255

293-
var relativePath = fileA.RelativeTo(paths.BranchesPath);
294-
var relativePathElements = relativePath.Elements.ToArray();
256+
var branch = string.Join(@"/", branchPathElement);
295257

296-
if (!relativePathElements.Any())
297-
{
298-
return;
258+
Logger.Trace("RemoteBranchCreated: {0}/{1}", origin, branch);
259+
RemoteBranchCreated?.Invoke(origin, branch);
260+
}
261+
}
262+
}
299263
}
300-
301-
var branch = string.Join(@"/", relativePathElements.ToArray());
302-
303-
Logger.Trace("LocalBranchChanged: {0}", branch);
304-
LocalBranchChanged?.Invoke(branch);
305-
}
306-
else if (fileEvent.Type == EventType.DELETED)
307-
{
308-
if (fileA.ExtensionWithDot == ".lock")
264+
else if (fileA.IsChildOf(paths.BranchesPath))
309265
{
310-
return;
311-
}
266+
if (fileEvent.Type == EventType.MODIFIED)
267+
{
268+
if (fileA.DirectoryExists())
269+
{
270+
continue;
271+
}
312272

313-
var relativePath = fileA.RelativeTo(paths.BranchesPath);
314-
var relativePathElements = relativePath.Elements.ToArray();
273+
if (fileA.ExtensionWithDot == ".lock")
274+
{
275+
continue;
276+
}
315277

316-
if (!relativePathElements.Any())
317-
{
318-
return;
319-
}
278+
var relativePath = fileA.RelativeTo(paths.BranchesPath);
279+
var relativePathElements = relativePath.Elements.ToArray();
320280

321-
var branch = string.Join(@"/", relativePathElements.ToArray());
281+
if (!relativePathElements.Any())
282+
{
283+
continue;
284+
}
322285

323-
Logger.Trace("LocalBranchDeleted: {0}", branch);
324-
LocalBranchDeleted?.Invoke(branch);
325-
}
326-
else if (fileEvent.Type == EventType.RENAMED)
327-
{
328-
if (fileA.ExtensionWithDot != ".lock")
329-
{
330-
return;
331-
}
286+
var branch = string.Join(@"/", relativePathElements.ToArray());
332287

333-
if (fileB != null && fileB.FileExists())
334-
{
335-
if (fileA.FileNameWithoutExtension == fileB.FileNameWithoutExtension)
288+
Logger.Trace("LocalBranchChanged: {0}", branch);
289+
LocalBranchChanged?.Invoke(branch);
290+
}
291+
else if (fileEvent.Type == EventType.DELETED)
336292
{
337-
var relativePath = fileB.RelativeTo(paths.BranchesPath);
293+
if (fileA.ExtensionWithDot == ".lock")
294+
{
295+
continue;
296+
}
297+
298+
var relativePath = fileA.RelativeTo(paths.BranchesPath);
338299
var relativePathElements = relativePath.Elements.ToArray();
339300

340301
if (!relativePathElements.Any())
341302
{
342-
return;
303+
continue;
343304
}
344305

345306
var branch = string.Join(@"/", relativePathElements.ToArray());
346307

347-
Logger.Trace("LocalBranchCreated: {0}", branch);
348-
LocalBranchCreated?.Invoke(branch);
308+
Logger.Trace("LocalBranchDeleted: {0}", branch);
309+
LocalBranchDeleted?.Invoke(branch);
310+
}
311+
else if (fileEvent.Type == EventType.RENAMED)
312+
{
313+
if (fileA.ExtensionWithDot != ".lock")
314+
{
315+
continue;
316+
}
317+
318+
if (fileB != null && fileB.FileExists())
319+
{
320+
if (fileA.FileNameWithoutExtension == fileB.FileNameWithoutExtension)
321+
{
322+
var relativePath = fileB.RelativeTo(paths.BranchesPath);
323+
var relativePathElements = relativePath.Elements.ToArray();
324+
325+
if (!relativePathElements.Any())
326+
{
327+
continue;
328+
}
329+
330+
var branch = string.Join(@"/", relativePathElements.ToArray());
331+
332+
Logger.Trace("LocalBranchCreated: {0}", branch);
333+
LocalBranchCreated?.Invoke(branch);
334+
}
335+
}
349336
}
350337
}
351338
}
339+
else
340+
{
341+
if (repositoryChanged || ignoredPaths.Any(ignoredPath => fileA.IsChildOf(ignoredPath)))
342+
{
343+
continue;
344+
}
345+
346+
repositoryChanged = true;
347+
}
348+
349+
lastCountOfProcessedEvents++;
350+
}
351+
352+
if (configChanged)
353+
{
354+
Logger.Trace("ConfigChanged");
355+
ConfigChanged?.Invoke();
356+
}
357+
358+
if (headChanged)
359+
{
360+
Logger.Trace("ConfigChanged");
361+
HeadChanged?.Invoke(headContent);
362+
}
363+
364+
if (indexChanged)
365+
{
366+
Logger.Trace("IndexChanged");
367+
IndexChanged?.Invoke();
368+
}
369+
370+
if (repositoryChanged)
371+
{
372+
Logger.Trace("RepositoryChanged");
373+
RepositoryChanged?.Invoke();
352374
}
353375
}
354376

0 commit comments

Comments
 (0)