Ejemplo n.º 1
0
        public GitStatusEntry CreateGitStatusEntry(string path, GitFileStatus status, string originalPath = null, bool staged = false)
        {
            var npath        = new NPath(path).MakeAbsolute();
            var relativePath = npath.RelativeTo(environment.RepositoryPath);
            var projectPath  = npath.RelativeTo(environment.UnityProjectPath);

            return(new GitStatusEntry(relativePath, npath, projectPath, status, originalPath?.ToNPath(), staged));
        }
Ejemplo n.º 2
0
        public GitStatusEntry CreateGitStatusEntry(string path, GitFileStatus indexStatus, GitFileStatus workTreeStatus = GitFileStatus.None, string originalPath = null)
        {
            var absolutePath = new NPath(path).MakeAbsolute();
            var relativePath = absolutePath.RelativeTo(environment.RepositoryPath);
            var projectPath  = absolutePath.RelativeTo(environment.UnityProjectPath);

            return(new GitStatusEntry(relativePath, absolutePath, projectPath, indexStatus, workTreeStatus, originalPath?.ToNPath()));
        }
Ejemplo n.º 3
0
        public GitLock CreateGitLock(string path, string user, int id)
        {
            var npath    = new NPath(path).MakeAbsolute();
            var fullPath = npath.RelativeTo(environment.RepositoryPath);

            return(new GitLock(path, fullPath, user, id));
        }
Ejemplo n.º 4
0
        public static NPath GetRepositoryPath(this IEnvironment environment, NPath path)
        {
            Guard.ArgumentNotNull(path, nameof(path));

            NPath projectPath    = environment.UnityProjectPath;
            NPath repositoryPath = environment.RepositoryPath;

            if (projectPath == repositoryPath)
            {
                return(path);
            }

            if (repositoryPath.IsChildOf(projectPath))
            {
                throw new InvalidOperationException($"RepositoryPath:\"{repositoryPath}\" should not be child of ProjectPath:\"{projectPath}\"");
            }

            return(projectPath.RelativeTo(repositoryPath).Combine(path));
        }
Ejemplo n.º 5
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);
                        }
                    }
                }
            }
        }
Ejemplo n.º 6
0
        private int ProcessEvents(Event[] fileEvents)
        {
            Dictionary <EventType, List <EventData> > events = new Dictionary <EventType, List <EventData> >();

            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 (!events.ContainsKey(EventType.ConfigChanged) && fileA.Equals(paths.DotGitConfig))
                    {
                        events.Add(EventType.ConfigChanged, null);
                    }
                    else if (!events.ContainsKey(EventType.HeadChanged) && fileA.Equals(paths.DotGitHead))
                    {
                        events.Add(EventType.HeadChanged, null);
                    }
                    else if (!events.ContainsKey(EventType.IndexChanged) && fileA.Equals(paths.DotGitIndex))
                    {
                        events.Add(EventType.IndexChanged, null);
                    }
                    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 == sfw.net.EventType.DELETED)
                        {
                            if (fileA.ExtensionWithDot == ".lock")
                            {
                                continue;
                            }

                            var branch = string.Join(@"/", relativePathElements.Skip(1).ToArray());
                            AddOrUpdateEventData(events, EventType.RemoteBranchDeleted, new EventData {
                                Origin = origin, Branch = branch
                            });
                        }
                        else if (fileEvent.Type == sfw.net.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);
                                    AddOrUpdateEventData(events, EventType.RemoteBranchCreated, new EventData {
                                        Origin = origin, Branch = branch
                                    });
                                }
                            }
                        }
                    }
                    else if (fileA.IsChildOf(paths.BranchesPath))
                    {
                        if (fileEvent.Type == sfw.net.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());

                            AddOrUpdateEventData(events, EventType.LocalBranchChanged, new EventData {
                                Branch = branch
                            });
                        }
                        else if (fileEvent.Type == sfw.net.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());
                            AddOrUpdateEventData(events, EventType.LocalBranchDeleted, new EventData {
                                Branch = branch
                            });
                        }
                        else if (fileEvent.Type == sfw.net.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());
                                    AddOrUpdateEventData(events, EventType.LocalBranchCreated, new EventData {
                                        Branch = branch
                                    });
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (events.ContainsKey(EventType.RepositoryChanged) || ignoredPaths.Any(ignoredPath => fileA.IsChildOf(ignoredPath)))
                    {
                        continue;
                    }
                    events.Add(EventType.RepositoryChanged, null);
                }
            }

            return(FireEvents(events));
        }
Ejemplo n.º 7
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);
        }