Пример #1
0
        private int FireEvents(Dictionary <EventType, List <EventData> > events)
        {
            int eventsProcessed = 0;

            if (events.ContainsKey(EventType.ConfigChanged))
            {
                Logger.Trace("ConfigChanged");
                ConfigChanged?.Invoke();
                eventsProcessed++;
            }

            if (events.ContainsKey(EventType.HeadChanged))
            {
                Logger.Trace("HeadChanged");
                HeadChanged?.Invoke();
                eventsProcessed++;
            }

            if (events.ContainsKey(EventType.IndexChanged))
            {
                Logger.Trace("IndexChanged");
                IndexChanged?.Invoke();
                eventsProcessed++;
            }

            if (events.ContainsKey(EventType.RepositoryChanged))
            {
                Logger.Trace("RepositoryChanged");
                RepositoryChanged?.Invoke();
                eventsProcessed++;
            }

            List <EventData> localBranchesCreated;

            if (events.TryGetValue(EventType.LocalBranchCreated, out localBranchesCreated))
            {
                foreach (var evt in localBranchesCreated)
                {
                    Logger.Trace($"LocalBranchCreated: {evt.Branch}");
                    LocalBranchCreated?.Invoke(evt.Branch);
                    eventsProcessed++;
                }
            }

            List <EventData> localBranchesChanged;

            if (events.TryGetValue(EventType.LocalBranchChanged, out localBranchesChanged))
            {
                foreach (var evt in localBranchesChanged)
                {
                    Logger.Trace($"LocalBranchChanged: {evt.Branch}");
                    LocalBranchChanged?.Invoke(evt.Branch);
                    eventsProcessed++;
                }
            }

            List <EventData> localBranchesDeleted;

            if (events.TryGetValue(EventType.LocalBranchDeleted, out localBranchesDeleted))
            {
                foreach (var evt in localBranchesDeleted)
                {
                    Logger.Trace($"LocalBranchDeleted: {evt.Branch}");
                    LocalBranchDeleted?.Invoke(evt.Branch);
                    eventsProcessed++;
                }
            }

            List <EventData> remoteBranchesCreated;

            if (events.TryGetValue(EventType.RemoteBranchCreated, out remoteBranchesCreated))
            {
                foreach (var evt in remoteBranchesCreated)
                {
                    Logger.Trace($"RemoteBranchCreated: {evt.Origin}/{evt.Branch}");
                    RemoteBranchCreated?.Invoke(evt.Origin, evt.Branch);
                    eventsProcessed++;
                }
            }

            List <EventData> remoteBranchesDeleted;

            if (events.TryGetValue(EventType.RemoteBranchDeleted, out remoteBranchesDeleted))
            {
                foreach (var evt in remoteBranchesDeleted)
                {
                    Logger.Trace($"RemoteBranchDeleted: {evt.Origin}/{evt.Branch}");
                    RemoteBranchDeleted?.Invoke(evt.Origin, evt.Branch);
                    eventsProcessed++;
                }
            }
            return(eventsProcessed);
        }
Пример #2
0
        private void HandleEventInDotGit(Event fileEvent, NPath fileA, NPath fileB = null)
        {
            if (fileA.Equals(paths.DotGitConfig))
            {
                Logger.Trace("ConfigChanged");

                ConfigChanged?.Invoke();
            }
            else if (fileA.Equals(paths.DotGitHead))
            {
                string headContent = null;
                if (fileEvent.Type != EventType.DELETED)
                {
                    headContent = paths.DotGitHead.ReadAllLines().FirstOrDefault();
                }

                Logger.Trace("HeadChanged: {0}", headContent ?? "[null]");
                HeadChanged?.Invoke(headContent);
            }
            else if (fileA.Equals(paths.DotGitIndex))
            {
                Logger.Trace("IndexChanged");
                IndexChanged?.Invoke();
            }
            else if (fileA.IsChildOf(paths.RemotesPath))
            {
                var relativePath         = fileA.RelativeTo(paths.RemotesPath);
                var relativePathElements = relativePath.Elements.ToArray();

                if (!relativePathElements.Any())
                {
                    return;
                }

                var origin = relativePathElements[0];

                if (fileEvent.Type == EventType.DELETED)
                {
                    if (fileA.ExtensionWithDot == ".lock")
                    {
                        return;
                    }

                    var branch = string.Join(@"/", relativePathElements.Skip(1).ToArray());

                    Logger.Trace("RemoteBranchDeleted: {0}/{1}", origin, branch);
                    RemoteBranchDeleted?.Invoke(origin, branch);
                }
                else if (fileEvent.Type == EventType.RENAMED)
                {
                    if (fileA.ExtensionWithDot != ".lock")
                    {
                        return;
                    }

                    if (fileB != null && fileB.FileExists())
                    {
                        if (fileA.FileNameWithoutExtension == fileB.FileNameWithoutExtension)
                        {
                            var branchPathElement = relativePathElements.Skip(1)
                                                    .Take(relativePathElements.Length - 2)
                                                    .Union(new [] { fileA.FileNameWithoutExtension }).ToArray();

                            var branch = string.Join(@"/", branchPathElement);

                            Logger.Trace("RemoteBranchCreated: {0}/{1}", origin, branch);
                            RemoteBranchCreated?.Invoke(origin, branch);
                        }
                    }
                }
            }
            else if (fileA.IsChildOf(paths.BranchesPath))
            {
                if (fileEvent.Type == EventType.MODIFIED)
                {
                    if (fileA.DirectoryExists())
                    {
                        return;
                    }

                    if (fileA.ExtensionWithDot == ".lock")
                    {
                        return;
                    }

                    var relativePath         = fileA.RelativeTo(paths.BranchesPath);
                    var relativePathElements = relativePath.Elements.ToArray();

                    if (!relativePathElements.Any())
                    {
                        return;
                    }

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

                    Logger.Trace("LocalBranchChanged: {0}", branch);
                    LocalBranchChanged?.Invoke(branch);
                }
                else if (fileEvent.Type == EventType.DELETED)
                {
                    if (fileA.ExtensionWithDot == ".lock")
                    {
                        return;
                    }

                    var relativePath         = fileA.RelativeTo(paths.BranchesPath);
                    var relativePathElements = relativePath.Elements.ToArray();

                    if (!relativePathElements.Any())
                    {
                        return;
                    }

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

                    Logger.Trace("LocalBranchDeleted: {0}", branch);
                    LocalBranchDeleted?.Invoke(branch);
                }
                else if (fileEvent.Type == EventType.RENAMED)
                {
                    if (fileA.ExtensionWithDot != ".lock")
                    {
                        return;
                    }

                    if (fileB != null && fileB.FileExists())
                    {
                        if (fileA.FileNameWithoutExtension == fileB.FileNameWithoutExtension)
                        {
                            var relativePath         = fileB.RelativeTo(paths.BranchesPath);
                            var relativePathElements = relativePath.Elements.ToArray();

                            if (!relativePathElements.Any())
                            {
                                return;
                            }

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

                            Logger.Trace("LocalBranchCreated: {0}", branch);
                            LocalBranchCreated?.Invoke(branch);
                        }
                    }
                }
            }
        }
Пример #3
0
        private int ProcessEvents(Event[] fileEvents)
        {
            var eventsProcessed   = 0;
            var configChanged     = false;
            var headChanged       = false;
            var repositoryChanged = false;
            var indexChanged      = false;

            string headContent = null;

            foreach (var fileEvent in fileEvents)
            {
                if (!running)
                {
                    break;
                }

                if (cancellationToken.IsCancellationRequested)
                {
                    Stop();
                    break;
                }

                //Logger.Trace(fileEvent.Describe());

                var eventDirectory = new NPath(fileEvent.Directory);
                var fileA          = eventDirectory.Combine(fileEvent.FileA);

                NPath fileB = null;
                if (fileEvent.FileB != null)
                {
                    fileB = eventDirectory.Combine(fileEvent.FileB);
                }

                // handling events in .git/*
                if (fileA.IsChildOf(paths.DotGitPath))
                {
                    if (!configChanged && fileA.Equals(paths.DotGitConfig))
                    {
                        configChanged = true;
                    }
                    else if (!headChanged && fileA.Equals(paths.DotGitHead))
                    {
                        if (fileEvent.Type != EventType.DELETED)
                        {
                            headContent = paths.DotGitHead.ReadAllLines().FirstOrDefault();
                        }

                        headChanged = true;
                    }
                    else if (!indexChanged && fileA.Equals(paths.DotGitIndex))
                    {
                        indexChanged = true;
                    }
                    else if (fileA.IsChildOf(paths.RemotesPath))
                    {
                        var relativePath         = fileA.RelativeTo(paths.RemotesPath);
                        var relativePathElements = relativePath.Elements.ToArray();

                        if (!relativePathElements.Any())
                        {
                            continue;
                        }

                        var origin = relativePathElements[0];

                        if (fileEvent.Type == EventType.DELETED)
                        {
                            if (fileA.ExtensionWithDot == ".lock")
                            {
                                continue;
                            }

                            var branch = string.Join(@"/", relativePathElements.Skip(1).ToArray());

                            Logger.Trace("RemoteBranchDeleted: {0}/{1}", origin, branch);
                            RemoteBranchDeleted?.Invoke(origin, branch);
                            eventsProcessed++;
                        }
                        else if (fileEvent.Type == EventType.RENAMED)
                        {
                            if (fileA.ExtensionWithDot != ".lock")
                            {
                                continue;
                            }

                            if (fileB != null && fileB.FileExists())
                            {
                                if (fileA.FileNameWithoutExtension == fileB.FileNameWithoutExtension)
                                {
                                    var branchPathElement = relativePathElements
                                                            .Skip(1).Take(relativePathElements.Length - 2)
                                                            .Union(new[] { fileA.FileNameWithoutExtension }).ToArray();

                                    var branch = string.Join(@"/", branchPathElement);

                                    Logger.Trace("RemoteBranchCreated: {0}/{1}", origin, branch);
                                    RemoteBranchCreated?.Invoke(origin, branch);
                                    eventsProcessed++;
                                }
                            }
                        }
                    }
                    else if (fileA.IsChildOf(paths.BranchesPath))
                    {
                        if (fileEvent.Type == EventType.MODIFIED)
                        {
                            if (fileA.DirectoryExists())
                            {
                                continue;
                            }

                            if (fileA.ExtensionWithDot == ".lock")
                            {
                                continue;
                            }

                            var relativePath         = fileA.RelativeTo(paths.BranchesPath);
                            var relativePathElements = relativePath.Elements.ToArray();

                            if (!relativePathElements.Any())
                            {
                                continue;
                            }

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

                            Logger.Trace("LocalBranchChanged: {0}", branch);
                            LocalBranchChanged?.Invoke(branch);
                            eventsProcessed++;
                        }
                        else if (fileEvent.Type == EventType.DELETED)
                        {
                            if (fileA.ExtensionWithDot == ".lock")
                            {
                                continue;
                            }

                            var relativePath         = fileA.RelativeTo(paths.BranchesPath);
                            var relativePathElements = relativePath.Elements.ToArray();

                            if (!relativePathElements.Any())
                            {
                                continue;
                            }

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

                            Logger.Trace("LocalBranchDeleted: {0}", branch);
                            LocalBranchDeleted?.Invoke(branch);
                            eventsProcessed++;
                        }
                        else if (fileEvent.Type == EventType.RENAMED)
                        {
                            if (fileA.ExtensionWithDot != ".lock")
                            {
                                continue;
                            }

                            if (fileB != null && fileB.FileExists())
                            {
                                if (fileA.FileNameWithoutExtension == fileB.FileNameWithoutExtension)
                                {
                                    var relativePath         = fileB.RelativeTo(paths.BranchesPath);
                                    var relativePathElements = relativePath.Elements.ToArray();

                                    if (!relativePathElements.Any())
                                    {
                                        continue;
                                    }

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

                                    Logger.Trace("LocalBranchCreated: {0}", branch);
                                    LocalBranchCreated?.Invoke(branch);
                                    eventsProcessed++;
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (repositoryChanged || ignoredPaths.Any(ignoredPath => fileA.IsChildOf(ignoredPath)))
                    {
                        continue;
                    }

                    repositoryChanged = true;
                }
            }

            if (configChanged)
            {
                Logger.Trace("ConfigChanged");
                ConfigChanged?.Invoke();
                eventsProcessed++;
            }

            if (headChanged)
            {
                Logger.Trace("HeadChanged: {0}", headContent ?? "[null]");
                HeadChanged?.Invoke(headContent);
                eventsProcessed++;
            }

            if (indexChanged)
            {
                Logger.Trace("IndexChanged");
                IndexChanged?.Invoke();
                eventsProcessed++;
            }

            if (repositoryChanged)
            {
                Logger.Trace("RepositoryChanged");
                RepositoryChanged?.Invoke();
                eventsProcessed++;
            }

            return(eventsProcessed);
        }