@@ -143,15 +143,32 @@ public int CheckAndProcessEvents()
143143
144144 signalProcessingEventsDone . Reset ( ) ;
145145 processingEvents = true ;
146- lastCountOfProcessedEvents = 0 ;
147- var fileEvents = nativeInterface . GetEvents ( ) ;
146+ var processedEventCount = 0 ;
148147
148+ var fileEvents = nativeInterface . GetEvents ( ) ;
149149 if ( fileEvents . Length > 0 )
150150 {
151- Logger . Trace ( "Processing {0} Events" , fileEvents . Length ) ;
151+ Logger . Trace ( "Handling {0} Events" , fileEvents . Length ) ;
152+ processedEventCount = ProcessEvents ( fileEvents ) ;
153+ Logger . Trace ( "Processed {0} Events" , processedEventCount ) ;
152154 }
153155
156+ lastCountOfProcessedEvents = processedEventCount ;
157+ processingEvents = false ;
158+ signalProcessingEventsDone . Set ( ) ;
159+
160+ return processedEventCount ;
161+ }
162+
163+ private int ProcessEvents ( Event [ ] fileEvents )
164+ {
165+ var eventsProcessed = 0 ;
166+ var configChanged = false ;
167+ var headChanged = false ;
154168 var repositoryChanged = false ;
169+ var indexChanged = false ;
170+
171+ string headContent = null ;
155172
156173 foreach ( var fileEvent in fileEvents )
157174 {
@@ -180,176 +197,190 @@ public int CheckAndProcessEvents()
180197 // handling events in .git/*
181198 if ( fileA . IsChildOf ( paths . DotGitPath ) )
182199 {
183- HandleEventInDotGit ( fileEvent , fileA , fileB ) ;
184- }
185- else
186- {
187- if ( repositoryChanged || ignoredPaths . Any ( ignoredPath => fileA . IsChildOf ( ignoredPath ) ) )
200+ if ( ! configChanged && fileA . Equals ( paths . DotGitConfig ) )
188201 {
189- continue ;
202+ configChanged = true ;
190203 }
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- }
207-
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 ] ;
243-
244- if ( fileEvent . Type == EventType . DELETED )
245- {
246- if ( fileA . ExtensionWithDot == ".lock" )
204+ else if ( ! headChanged && fileA . Equals ( paths . DotGitHead ) )
247205 {
248- return ;
249- }
250-
251- var branch = string . Join ( @"/" , relativePathElements . Skip ( 1 ) . ToArray ( ) ) ;
206+ if ( fileEvent . Type != EventType . DELETED )
207+ {
208+ headContent = paths . DotGitHead . ReadAllLines ( ) . FirstOrDefault ( ) ;
209+ }
252210
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" )
211+ headChanged = true ;
212+ }
213+ else if ( ! indexChanged && fileA . Equals ( paths . DotGitIndex ) )
259214 {
260- return ;
215+ indexChanged = true ;
261216 }
262-
263- if ( fileB != null && fileB . FileExists ( ) )
217+ else if ( fileA . IsChildOf ( paths . RemotesPath ) )
264218 {
265- if ( fileA . FileNameWithoutExtension == fileB . FileNameWithoutExtension )
266- {
267- var branchPathElement = relativePathElements . Skip ( 1 )
268- . Take ( relativePathElements . Length - 2 )
269- . Union ( new [ ] { fileA . FileNameWithoutExtension } ) . ToArray ( ) ;
270-
271- var branch = string . Join ( @"/" , branchPathElement ) ;
219+ var relativePath = fileA . RelativeTo ( paths . RemotesPath ) ;
220+ var relativePathElements = relativePath . Elements . ToArray ( ) ;
272221
273- Logger . Trace ( "RemoteBranchCreated: {0}/{1}" , origin , branch ) ;
274- RemoteBranchCreated ? . Invoke ( origin , branch ) ;
222+ if ( ! relativePathElements . Any ( ) )
223+ {
224+ continue ;
275225 }
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- }
287226
288- if ( fileA . ExtensionWithDot == ".lock" )
289- {
290- return ;
291- }
227+ var origin = relativePathElements [ 0 ] ;
292228
293- var relativePath = fileA . RelativeTo ( paths . BranchesPath ) ;
294- var relativePathElements = relativePath . Elements . ToArray ( ) ;
229+ if ( fileEvent . Type == EventType . DELETED )
230+ {
231+ if ( fileA . ExtensionWithDot == ".lock" )
232+ {
233+ continue ;
234+ }
295235
296- if ( ! relativePathElements . Any ( ) )
297- {
298- return ;
299- }
236+ var branch = string . Join ( @"/" , relativePathElements . Skip ( 1 ) . ToArray ( ) ) ;
300237
301- var branch = string . Join ( @"/" , relativePathElements . ToArray ( ) ) ;
238+ Logger . Trace ( "RemoteBranchDeleted: {0}/{1}" , origin , branch ) ;
239+ RemoteBranchDeleted ? . Invoke ( origin , branch ) ;
240+ eventsProcessed ++ ;
241+ }
242+ else if ( fileEvent . Type == EventType . RENAMED )
243+ {
244+ if ( fileA . ExtensionWithDot != ".lock" )
245+ {
246+ continue ;
247+ }
302248
303- Logger . Trace ( "LocalBranchChanged: {0}" , branch ) ;
304- LocalBranchChanged ? . Invoke ( branch ) ;
305- }
306- else if ( fileEvent . Type == EventType . DELETED )
307- {
308- if ( fileA . ExtensionWithDot == ".lock" )
309- {
310- return ;
249+ if ( fileB != null && fileB . FileExists ( ) )
250+ {
251+ if ( fileA . FileNameWithoutExtension == fileB . FileNameWithoutExtension )
252+ {
253+ var branchPathElement = relativePathElements
254+ . Skip ( 1 ) . Take ( relativePathElements . Length - 2 )
255+ . Union ( new [ ] { fileA . FileNameWithoutExtension } ) . ToArray ( ) ;
256+
257+ var branch = string . Join ( @"/" , branchPathElement ) ;
258+
259+ Logger . Trace ( "RemoteBranchCreated: {0}/{1}" , origin , branch ) ;
260+ RemoteBranchCreated ? . Invoke ( origin , branch ) ;
261+ eventsProcessed ++ ;
262+ }
263+ }
264+ }
311265 }
266+ else if ( fileA . IsChildOf ( paths . BranchesPath ) )
267+ {
268+ if ( fileEvent . Type == EventType . MODIFIED )
269+ {
270+ if ( fileA . DirectoryExists ( ) )
271+ {
272+ continue ;
273+ }
312274
313- var relativePath = fileA . RelativeTo ( paths . BranchesPath ) ;
314- var relativePathElements = relativePath . Elements . ToArray ( ) ;
275+ if ( fileA . ExtensionWithDot == ".lock" )
276+ {
277+ continue ;
278+ }
315279
316- if ( ! relativePathElements . Any ( ) )
317- {
318- return ;
319- }
280+ var relativePath = fileA . RelativeTo ( paths . BranchesPath ) ;
281+ var relativePathElements = relativePath . Elements . ToArray ( ) ;
320282
321- var branch = string . Join ( @"/" , relativePathElements . ToArray ( ) ) ;
283+ if ( ! relativePathElements . Any ( ) )
284+ {
285+ continue ;
286+ }
322287
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- }
288+ var branch = string . Join ( @"/" , relativePathElements . ToArray ( ) ) ;
332289
333- if ( fileB != null && fileB . FileExists ( ) )
334- {
335- if ( fileA . FileNameWithoutExtension == fileB . FileNameWithoutExtension )
290+ Logger . Trace ( "LocalBranchChanged: {0}" , branch ) ;
291+ LocalBranchChanged ? . Invoke ( branch ) ;
292+ eventsProcessed ++ ;
293+ }
294+ else if ( fileEvent . Type == EventType . DELETED )
336295 {
337- var relativePath = fileB . RelativeTo ( paths . BranchesPath ) ;
296+ if ( fileA . ExtensionWithDot == ".lock" )
297+ {
298+ continue ;
299+ }
300+
301+ var relativePath = fileA . RelativeTo ( paths . BranchesPath ) ;
338302 var relativePathElements = relativePath . Elements . ToArray ( ) ;
339303
340304 if ( ! relativePathElements . Any ( ) )
341305 {
342- return ;
306+ continue ;
343307 }
344308
345309 var branch = string . Join ( @"/" , relativePathElements . ToArray ( ) ) ;
346310
347- Logger . Trace ( "LocalBranchCreated: {0}" , branch ) ;
348- LocalBranchCreated ? . Invoke ( branch ) ;
311+ Logger . Trace ( "LocalBranchDeleted: {0}" , branch ) ;
312+ LocalBranchDeleted ? . Invoke ( branch ) ;
313+ eventsProcessed ++ ;
314+ }
315+ else if ( fileEvent . Type == EventType . RENAMED )
316+ {
317+ if ( fileA . ExtensionWithDot != ".lock" )
318+ {
319+ continue ;
320+ }
321+
322+ if ( fileB != null && fileB . FileExists ( ) )
323+ {
324+ if ( fileA . FileNameWithoutExtension == fileB . FileNameWithoutExtension )
325+ {
326+ var relativePath = fileB . RelativeTo ( paths . BranchesPath ) ;
327+ var relativePathElements = relativePath . Elements . ToArray ( ) ;
328+
329+ if ( ! relativePathElements . Any ( ) )
330+ {
331+ continue ;
332+ }
333+
334+ var branch = string . Join ( @"/" , relativePathElements . ToArray ( ) ) ;
335+
336+ Logger . Trace ( "LocalBranchCreated: {0}" , branch ) ;
337+ LocalBranchCreated ? . Invoke ( branch ) ;
338+ eventsProcessed ++ ;
339+ }
340+ }
349341 }
350342 }
351343 }
344+ else
345+ {
346+ if ( repositoryChanged || ignoredPaths . Any ( ignoredPath => fileA . IsChildOf ( ignoredPath ) ) )
347+ {
348+ continue ;
349+ }
350+
351+ repositoryChanged = true ;
352+ }
352353 }
354+
355+ if ( configChanged )
356+ {
357+ Logger . Trace ( "ConfigChanged" ) ;
358+ ConfigChanged ? . Invoke ( ) ;
359+ eventsProcessed ++ ;
360+ }
361+
362+ if ( headChanged )
363+ {
364+ Logger . Trace ( "HeadChanged: {0}" , headContent ?? "[null]" ) ;
365+ HeadChanged ? . Invoke ( headContent ) ;
366+ eventsProcessed ++ ;
367+ }
368+
369+ if ( indexChanged )
370+ {
371+ Logger . Trace ( "IndexChanged" ) ;
372+ IndexChanged ? . Invoke ( ) ;
373+ eventsProcessed ++ ;
374+ }
375+
376+ if ( repositoryChanged )
377+ {
378+ Logger . Trace ( "RepositoryChanged" ) ;
379+ RepositoryChanged ? . Invoke ( ) ;
380+ eventsProcessed ++ ;
381+ }
382+
383+ return eventsProcessed ;
353384 }
354385
355386 private bool disposed ;
0 commit comments