public void InitializeRepository(NPath expectedRepositoryPath = null)
        {
            if (RepositoryPath != null && RepositoryPath.DirectoryExists(".git"))
            {
                FileSystem.SetCurrentDirectory(RepositoryPath);
                return;
            }

            Guard.NotNull(this, UnityProjectPath, nameof(UnityProjectPath));

            if (expectedRepositoryPath == null)
            {
                expectedRepositoryPath = UnityProjectPath;
            }

            Guard.NotNull(this, FileSystem, nameof(FileSystem));
            if (expectedRepositoryPath != null && expectedRepositoryPath.DirectoryExists(".git"))
            {
                RepositoryPath = expectedRepositoryPath;
                FileSystem.SetCurrentDirectory(RepositoryPath);
                return;
            }

            RepositoryPath = UnityProjectPath.RecursiveParents.FirstOrDefault(d => d.DirectoryExists(".git"));
            if (RepositoryPath == null)
            {
                FileSystem.SetCurrentDirectory(UnityProjectPath);
            }
            else
            {
                FileSystem.SetCurrentDirectory(RepositoryPath);
            }
        }
示例#2
0
        public void InitializeRepository(NPath expectedRepositoryPath = null)
        {
            Guard.NotNull(this, FileSystem, nameof(FileSystem));

            Logger.Trace("InitializeRepository expectedRepositoryPath:{0}", expectedRepositoryPath);

            if (RepositoryPath == null)
            {
                Guard.NotNull(this, UnityProjectPath, nameof(UnityProjectPath));

                Logger.Trace("RepositoryPath is null");

                if (expectedRepositoryPath == null)
                {
                    expectedRepositoryPath = UnityProjectPath;
                }

                if (!expectedRepositoryPath.DirectoryExists(".git"))
                {
                    Logger.Trace(".git folder exists");

                    var reporoot = UnityProjectPath.RecursiveParents.FirstOrDefault(d => d.DirectoryExists(".git"));
                    if (reporoot != null)
                    {
                        expectedRepositoryPath = reporoot;
                    }
                }
            }
            else
            {
                Logger.Trace("Set to RepositoryPath");
                expectedRepositoryPath = RepositoryPath;
            }

            FileSystem.SetCurrentDirectory(expectedRepositoryPath);
            if (expectedRepositoryPath.DirectoryExists(".git"))
            {
                Logger.Trace("Determined expectedRepositoryPath:{0}", expectedRepositoryPath);
                RepositoryPath = expectedRepositoryPath;
                Repository     = new Repository(RepositoryPath.FileName, RepositoryPath);
            }
        }
        private static async Task <NPath> LookForGitInstallationPath(IGitClient gitClient, ISettings systemSettings)
        {
            NPath cachedGitInstallPath = null;
            var   path = systemSettings.Get(Constants.GitInstallPathKey);

            if (!String.IsNullOrEmpty(path))
            {
                cachedGitInstallPath = path.ToNPath();
            }

            // Root paths
            if (cachedGitInstallPath != null && cachedGitInstallPath.DirectoryExists())
            {
                return(cachedGitInstallPath);
            }
            return(await gitClient.FindGitInstallation());
        }
示例#4
0
        public void Configure(ProcessStartInfo psi, NPath workingDirectory, bool dontSetupGit = false)
        {
            psi.WorkingDirectory             = workingDirectory;
            psi.EnvironmentVariables["HOME"] = NPath.HomeDirectory;
            psi.EnvironmentVariables["TMP"]  = psi.EnvironmentVariables["TEMP"] = NPath.SystemTemp;

            var path = Environment.Path;

            psi.EnvironmentVariables["GHU_WORKINGDIR"] = workingDirectory;

            if (dontSetupGit)
            {
                psi.EnvironmentVariables["GHU_FULLPATH"] = path;
                psi.EnvironmentVariables["PATH"]         = path;
                return;
            }

            Guard.ArgumentNotNull(psi, "psi");

            var    pathEntries = new List <string>();
            string separator   = Environment.IsWindows ? ";" : ":";

            NPath         libexecPath    = NPath.Default;
            List <string> gitPathEntries = new List <string>();

            if (Environment.GitInstallPath.IsInitialized)
            {
                var gitPathRoot      = Environment.GitExecutablePath.Resolve().Parent.Parent;
                var gitExecutableDir = Environment.GitExecutablePath.Parent; // original path to git (might be different from install path if it's a symlink)

                var baseExecPath = gitPathRoot;
                var binPath      = baseExecPath;
                if (Environment.IsWindows)
                {
                    if (baseExecPath.DirectoryExists("mingw32"))
                    {
                        baseExecPath = baseExecPath.Combine("mingw32");
                    }
                    else
                    {
                        baseExecPath = baseExecPath.Combine("mingw64");
                    }
                    binPath = baseExecPath.Combine("bin");
                }

                libexecPath = baseExecPath.Combine("libexec", "git-core");
                if (!libexecPath.DirectoryExists())
                {
                    libexecPath = NPath.Default;
                }

                if (Environment.IsWindows)
                {
                    gitPathEntries.AddRange(new[] { gitPathRoot.Combine("cmd").ToString(), gitPathRoot.Combine("usr", "bin") });
                }
                else
                {
                    gitPathEntries.Add(gitExecutableDir.ToString());
                }

                if (libexecPath.IsInitialized)
                {
                    gitPathEntries.Add(libexecPath);
                }
                gitPathEntries.Add(binPath);

                // we can only set this env var if there is a libexec/git-core. git will bypass internally bundled tools if this env var
                // is set, which will break Apple's system git on certain tools (like osx-credentialmanager)
                if (libexecPath.IsInitialized)
                {
                    psi.EnvironmentVariables["GIT_EXEC_PATH"] = libexecPath.ToString();
                }
            }

            if (Environment.GitLfsInstallPath.IsInitialized && libexecPath != Environment.GitLfsInstallPath)
            {
                pathEntries.Add(Environment.GitLfsInstallPath);
            }
            if (gitPathEntries.Count > 0)
            {
                pathEntries.AddRange(gitPathEntries);
            }

            pathEntries.Add("END");

            path = String.Join(separator, pathEntries.ToArray()) + separator + path;

            psi.EnvironmentVariables["GHU_FULLPATH"] = path;
            psi.EnvironmentVariables["PATH"]         = path;

            //TODO: Remove with Git LFS Locking becomes standard
            psi.EnvironmentVariables["GITLFSLOCKSENABLED"] = "1";

            if (Environment.IsWindows)
            {
                psi.EnvironmentVariables["PLINK_PROTOCOL"] = "ssh";
                psi.EnvironmentVariables["TERM"]           = "msys";
            }

            var httpProxy = Environment.GetEnvironmentVariable("HTTP_PROXY");

            if (!String.IsNullOrEmpty(httpProxy))
            {
                psi.EnvironmentVariables["HTTP_PROXY"] = httpProxy;
            }

            var httpsProxy = Environment.GetEnvironmentVariable("HTTPS_PROXY");

            if (!String.IsNullOrEmpty(httpsProxy))
            {
                psi.EnvironmentVariables["HTTPS_PROXY"] = httpsProxy;
            }
        }
示例#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);
                        }
                    }
                }
            }
        }