@@ -11,7 +11,7 @@ interface IRepositoryWatcher : IDisposable
11
11
{
12
12
void Start ( ) ;
13
13
void Stop ( ) ;
14
- event Action < string > HeadChanged ;
14
+ event Action HeadChanged ;
15
15
event Action IndexChanged ;
16
16
event Action ConfigChanged ;
17
17
event Action < string > LocalBranchChanged ;
@@ -38,7 +38,7 @@ class RepositoryWatcher : IRepositoryWatcher
38
38
private bool processingEvents ;
39
39
private readonly ManualResetEventSlim signalProcessingEventsDone = new ManualResetEventSlim ( false ) ;
40
40
41
- public event Action < string > HeadChanged ;
41
+ public event Action HeadChanged ;
42
42
public event Action IndexChanged ;
43
43
public event Action ConfigChanged ;
44
44
public event Action < string > LocalBranchChanged ;
@@ -162,14 +162,7 @@ public int CheckAndProcessEvents()
162
162
163
163
private int ProcessEvents ( Event [ ] fileEvents )
164
164
{
165
- var eventsProcessed = 0 ;
166
- var configChanged = false ;
167
- var headChanged = false ;
168
- var repositoryChanged = false ;
169
- var indexChanged = false ;
170
-
171
- string headContent = null ;
172
-
165
+ Dictionary < EventType , List < EventData > > events = new Dictionary < EventType , List < EventData > > ( ) ;
173
166
foreach ( var fileEvent in fileEvents )
174
167
{
175
168
if ( ! running )
@@ -197,22 +190,17 @@ private int ProcessEvents(Event[] fileEvents)
197
190
// handling events in .git/*
198
191
if ( fileA . IsChildOf ( paths . DotGitPath ) )
199
192
{
200
- if ( ! configChanged && fileA . Equals ( paths . DotGitConfig ) )
193
+ if ( ! events . ContainsKey ( EventType . ConfigChanged ) && fileA . Equals ( paths . DotGitConfig ) )
201
194
{
202
- configChanged = true ;
195
+ events . Add ( EventType . ConfigChanged , null ) ;
203
196
}
204
- else if ( ! headChanged && fileA . Equals ( paths . DotGitHead ) )
197
+ else if ( ! events . ContainsKey ( EventType . HeadChanged ) && fileA . Equals ( paths . DotGitHead ) )
205
198
{
206
- if ( fileEvent . Type != EventType . DELETED )
207
- {
208
- headContent = paths . DotGitHead . ReadAllLines ( ) . FirstOrDefault ( ) ;
209
- }
210
-
211
- headChanged = true ;
199
+ events . Add ( EventType . HeadChanged , null ) ;
212
200
}
213
- else if ( ! indexChanged && fileA . Equals ( paths . DotGitIndex ) )
201
+ else if ( ! events . ContainsKey ( EventType . IndexChanged ) && fileA . Equals ( paths . DotGitIndex ) )
214
202
{
215
- indexChanged = true ;
203
+ events . Add ( EventType . IndexChanged , null ) ;
216
204
}
217
205
else if ( fileA . IsChildOf ( paths . RemotesPath ) )
218
206
{
@@ -226,20 +214,17 @@ private int ProcessEvents(Event[] fileEvents)
226
214
227
215
var origin = relativePathElements [ 0 ] ;
228
216
229
- if ( fileEvent . Type == EventType . DELETED )
217
+ if ( fileEvent . Type == sfw . net . EventType . DELETED )
230
218
{
231
219
if ( fileA . ExtensionWithDot == ".lock" )
232
220
{
233
221
continue ;
234
222
}
235
223
236
224
var branch = string . Join ( @"/" , relativePathElements . Skip ( 1 ) . ToArray ( ) ) ;
237
-
238
- Logger . Trace ( "RemoteBranchDeleted: {0}/{1}" , origin , branch ) ;
239
- RemoteBranchDeleted ? . Invoke ( origin , branch ) ;
240
- eventsProcessed ++ ;
225
+ AddOrUpdateEventData ( events , EventType . RemoteBranchDeleted , new EventData { Origin = origin , Branch = branch } ) ;
241
226
}
242
- else if ( fileEvent . Type == EventType . RENAMED )
227
+ else if ( fileEvent . Type == sfw . net . EventType . RENAMED )
243
228
{
244
229
if ( fileA . ExtensionWithDot != ".lock" )
245
230
{
@@ -255,17 +240,14 @@ private int ProcessEvents(Event[] fileEvents)
255
240
. Union ( new [ ] { fileA . FileNameWithoutExtension } ) . ToArray ( ) ;
256
241
257
242
var branch = string . Join ( @"/" , branchPathElement ) ;
258
-
259
- Logger . Trace ( "RemoteBranchCreated: {0}/{1}" , origin , branch ) ;
260
- RemoteBranchCreated ? . Invoke ( origin , branch ) ;
261
- eventsProcessed ++ ;
243
+ AddOrUpdateEventData ( events , EventType . RemoteBranchCreated , new EventData { Origin = origin , Branch = branch } ) ;
262
244
}
263
245
}
264
246
}
265
247
}
266
248
else if ( fileA . IsChildOf ( paths . BranchesPath ) )
267
249
{
268
- if ( fileEvent . Type == EventType . MODIFIED )
250
+ if ( fileEvent . Type == sfw . net . EventType . MODIFIED )
269
251
{
270
252
if ( fileA . DirectoryExists ( ) )
271
253
{
@@ -287,11 +269,10 @@ private int ProcessEvents(Event[] fileEvents)
287
269
288
270
var branch = string . Join ( @"/" , relativePathElements . ToArray ( ) ) ;
289
271
290
- Logger . Trace ( "LocalBranchChanged: {0}" , branch ) ;
291
- LocalBranchChanged ? . Invoke ( branch ) ;
292
- eventsProcessed ++ ;
272
+ AddOrUpdateEventData ( events , EventType . LocalBranchChanged , new EventData { Branch = branch } ) ;
273
+
293
274
}
294
- else if ( fileEvent . Type == EventType . DELETED )
275
+ else if ( fileEvent . Type == sfw . net . EventType . DELETED )
295
276
{
296
277
if ( fileA . ExtensionWithDot == ".lock" )
297
278
{
@@ -307,12 +288,9 @@ private int ProcessEvents(Event[] fileEvents)
307
288
}
308
289
309
290
var branch = string . Join ( @"/" , relativePathElements . ToArray ( ) ) ;
310
-
311
- Logger . Trace ( "LocalBranchDeleted: {0}" , branch ) ;
312
- LocalBranchDeleted ? . Invoke ( branch ) ;
313
- eventsProcessed ++ ;
291
+ AddOrUpdateEventData ( events , EventType . LocalBranchDeleted , new EventData { Branch = branch } ) ;
314
292
}
315
- else if ( fileEvent . Type == EventType . RENAMED )
293
+ else if ( fileEvent . Type == sfw . net . EventType . RENAMED )
316
294
{
317
295
if ( fileA . ExtensionWithDot != ".lock" )
318
296
{
@@ -332,54 +310,117 @@ private int ProcessEvents(Event[] fileEvents)
332
310
}
333
311
334
312
var branch = string . Join ( @"/" , relativePathElements . ToArray ( ) ) ;
335
-
336
- Logger . Trace ( "LocalBranchCreated: {0}" , branch ) ;
337
- LocalBranchCreated ? . Invoke ( branch ) ;
338
- eventsProcessed ++ ;
313
+ AddOrUpdateEventData ( events , EventType . LocalBranchCreated , new EventData { Branch = branch } ) ;
339
314
}
340
315
}
341
316
}
342
317
}
343
318
}
344
319
else
345
320
{
346
- if ( repositoryChanged || ignoredPaths . Any ( ignoredPath => fileA . IsChildOf ( ignoredPath ) ) )
321
+ if ( events . ContainsKey ( EventType . RepositoryChanged ) || ignoredPaths . Any ( ignoredPath => fileA . IsChildOf ( ignoredPath ) ) )
347
322
{
348
323
continue ;
349
324
}
350
-
351
- repositoryChanged = true ;
325
+ events . Add ( EventType . RepositoryChanged , null ) ;
352
326
}
353
327
}
354
328
355
- if ( configChanged )
329
+ return FireEvents ( events ) ;
330
+ }
331
+
332
+ private void AddOrUpdateEventData ( Dictionary < EventType , List < EventData > > events , EventType type , EventData data )
333
+ {
334
+ if ( ! events . ContainsKey ( type ) )
335
+ events . Add ( type , new List < EventData > ( ) ) ;
336
+ events [ type ] . Add ( data ) ;
337
+ }
338
+
339
+ private int FireEvents ( Dictionary < EventType , List < EventData > > events )
340
+ {
341
+ int eventsProcessed = 0 ;
342
+ if ( events . ContainsKey ( EventType . ConfigChanged ) )
356
343
{
357
344
Logger . Trace ( "ConfigChanged" ) ;
358
345
ConfigChanged ? . Invoke ( ) ;
359
346
eventsProcessed ++ ;
360
347
}
361
348
362
- if ( headChanged )
349
+ if ( events . ContainsKey ( EventType . HeadChanged ) )
363
350
{
364
- Logger . Trace ( "HeadChanged: {0}" , headContent ?? "[null] ") ;
365
- HeadChanged ? . Invoke ( headContent ) ;
351
+ Logger . Trace ( "HeadChanged" ) ;
352
+ HeadChanged ? . Invoke ( ) ;
366
353
eventsProcessed ++ ;
367
354
}
368
355
369
- if ( indexChanged )
356
+ if ( events . ContainsKey ( EventType . IndexChanged ) )
370
357
{
371
358
Logger . Trace ( "IndexChanged" ) ;
372
359
IndexChanged ? . Invoke ( ) ;
373
360
eventsProcessed ++ ;
374
361
}
375
362
376
- if ( repositoryChanged )
363
+ if ( events . ContainsKey ( EventType . RepositoryChanged ) )
377
364
{
378
365
Logger . Trace ( "RepositoryChanged" ) ;
379
366
RepositoryChanged ? . Invoke ( ) ;
380
367
eventsProcessed ++ ;
381
368
}
382
369
370
+ List < EventData > localBranchesCreated ;
371
+ if ( events . TryGetValue ( EventType . LocalBranchCreated , out localBranchesCreated ) )
372
+ {
373
+ foreach ( var evt in localBranchesCreated )
374
+ {
375
+ Logger . Trace ( $ "LocalBranchCreated: { evt . Branch } ") ;
376
+ LocalBranchCreated ? . Invoke ( evt . Branch ) ;
377
+ eventsProcessed ++ ;
378
+ }
379
+ }
380
+
381
+ List < EventData > localBranchesChanged ;
382
+ if ( events . TryGetValue ( EventType . LocalBranchChanged , out localBranchesChanged ) )
383
+ {
384
+ foreach ( var evt in localBranchesChanged )
385
+ {
386
+ Logger . Trace ( $ "LocalBranchChanged: { evt . Branch } ") ;
387
+ LocalBranchChanged ? . Invoke ( evt . Branch ) ;
388
+ eventsProcessed ++ ;
389
+ }
390
+ }
391
+
392
+ List < EventData > localBranchesDeleted ;
393
+ if ( events . TryGetValue ( EventType . LocalBranchDeleted , out localBranchesDeleted ) )
394
+ {
395
+ foreach ( var evt in localBranchesDeleted )
396
+ {
397
+ Logger . Trace ( $ "LocalBranchDeleted: { evt . Branch } ") ;
398
+ LocalBranchDeleted ? . Invoke ( evt . Branch ) ;
399
+ eventsProcessed ++ ;
400
+ }
401
+ }
402
+
403
+ List < EventData > remoteBranchesCreated ;
404
+ if ( events . TryGetValue ( EventType . RemoteBranchCreated , out remoteBranchesCreated ) )
405
+ {
406
+ foreach ( var evt in remoteBranchesCreated )
407
+ {
408
+ Logger . Trace ( $ "RemoteBranchCreated: { evt . Origin } /{ evt . Branch } ") ;
409
+ RemoteBranchCreated ? . Invoke ( evt . Origin , evt . Branch ) ;
410
+ eventsProcessed ++ ;
411
+ }
412
+ }
413
+
414
+ List < EventData > remoteBranchesDeleted ;
415
+ if ( events . TryGetValue ( EventType . RemoteBranchDeleted , out remoteBranchesDeleted ) )
416
+ {
417
+ foreach ( var evt in remoteBranchesDeleted )
418
+ {
419
+ Logger . Trace ( $ "RemoteBranchDeleted: { evt . Origin } /{ evt . Branch } ") ;
420
+ RemoteBranchDeleted ? . Invoke ( evt . Origin , evt . Branch ) ;
421
+ eventsProcessed ++ ;
422
+ }
423
+ }
383
424
return eventsProcessed ;
384
425
}
385
426
@@ -407,5 +448,25 @@ public void Dispose()
407
448
}
408
449
409
450
protected static ILogging Logger { get ; } = Logging . GetLogger < RepositoryWatcher > ( ) ;
451
+
452
+ private enum EventType
453
+ {
454
+ None ,
455
+ ConfigChanged ,
456
+ HeadChanged ,
457
+ RepositoryChanged ,
458
+ IndexChanged ,
459
+ RemoteBranchDeleted ,
460
+ RemoteBranchCreated ,
461
+ LocalBranchDeleted ,
462
+ LocalBranchCreated ,
463
+ LocalBranchChanged
464
+ }
465
+
466
+ private class EventData
467
+ {
468
+ public string Origin ;
469
+ public string Branch ;
470
+ }
410
471
}
411
472
}
0 commit comments