Beispiel #1
0
        protected string GetCacheServerUrlFromConfig(string repoUrl)
        {
            GitProcess git             = new GitProcess(this);
            string     cacheConfigName = GetCacheConfigSettingName(repoUrl);

            string cacheServerUrl = git.GetFromConfig(cacheConfigName);

            if (string.IsNullOrWhiteSpace(cacheServerUrl))
            {
                // Try getting from the deprecated setting for compatibility reasons
                cacheServerUrl = StripObjectsEndpointSuffix(git.GetFromConfig(DeprecatedObjectsEndpointGitConfigName));

                // Upgrade for future runs, but not at clone time.
                if (!string.IsNullOrWhiteSpace(cacheServerUrl) && Directory.Exists(this.WorkingDirectoryRoot))
                {
                    git.SetInLocalConfig(cacheConfigName, cacheServerUrl);
                    git.DeleteFromLocalConfig(DeprecatedObjectsEndpointGitConfigName);
                }
            }

            // Default to uncached url
            if (string.IsNullOrWhiteSpace(cacheServerUrl))
            {
                return(repoUrl);
            }

            return(cacheServerUrl);
        }
Beispiel #2
0
        private static bool TrySetConfig(Enlistment enlistment, Dictionary <string, string> configSettings, bool isRequired)
        {
            GitProcess git = new GitProcess(enlistment);

            Dictionary <string, GitConfigSetting> existingConfigSettings;

            // If the settings are required, then only check local config settings, because we don't want to depend on
            // global settings that can then change independent of this repo.
            if (!git.TryGetAllConfig(localOnly: isRequired, configSettings: out existingConfigSettings))
            {
                return(false);
            }

            foreach (KeyValuePair <string, string> setting in configSettings)
            {
                GitConfigSetting existingSetting;
                if (setting.Value != null)
                {
                    if (!existingConfigSettings.TryGetValue(setting.Key, out existingSetting) ||
                        (isRequired && !existingSetting.HasValue(setting.Value)))
                    {
                        GitProcess.Result setConfigResult = git.SetInLocalConfig(setting.Key, setting.Value);
                        if (setConfigResult.ExitCodeIsFailure)
                        {
                            return(false);
                        }
                    }
                }
                else
                {
                    if (existingConfigSettings.TryGetValue(setting.Key, out existingSetting))
                    {
                        git.DeleteFromLocalConfig(setting.Key);
                    }
                }
            }

            return(true);
        }
Beispiel #3
0
        private Result GitClone()
        {
            string gitBinPath = ScalarPlatform.Instance.GitInstallation.GetInstalledGitBinPath();

            if (string.IsNullOrWhiteSpace(gitBinPath))
            {
                return(new Result(ScalarConstants.GitIsNotInstalledError));
            }

            GitProcess git = new GitProcess(this.enlistment);

            // protocol.version=2 is broken right now.
            git.SetInLocalConfig("protocol.version", "1");

            git.SetInLocalConfig("remote.origin.url", this.RepositoryURL);
            git.SetInLocalConfig("remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*");
            git.SetInLocalConfig("remote.origin.promisor", "true");
            git.SetInLocalConfig("remote.origin.partialCloneFilter", "blob:none");

            string branch = this.Branch ?? "master";

            git.SetInLocalConfig($"branch.{branch}.remote", "origin");
            git.SetInLocalConfig($"branch.{branch}.merge", $"refs/heads/{branch}");

            if (!this.FullClone)
            {
                GitProcess.SparseCheckoutInit(this.enlistment);
            }

            this.context = new ScalarContext(this.tracer, this.fileSystem, this.enlistment);

            // Set required and optional config.
            // Explicitly pass useGvfsProtocol: true as the enlistment can not discover that setting from
            // Git config yet. Other verbs will discover this automatically from the config we set now.
            ConfigStep configStep = new ConfigStep(this.context, useGvfsProtocol: false);

            if (!configStep.TrySetConfig(out string configError))
            {
                return(new Result($"Failed to set initial config: {configError}"));
            }

            GitProcess.Result fetchResult = null;
            if (!this.ShowStatusWhileRunning(() =>
            {
                using (ITracer activity = this.tracer.StartActivity("git-fetch-partial", EventLevel.LogAlways))
                {
                    fetchResult = git.ForegroundFetch("origin");
                    return(fetchResult.ExitCodeIsSuccess);
                }
            },
                                             "Fetching objects from remote"))
            {
                if (!fetchResult.Errors.Contains("filtering not recognized by server"))
                {
                    return(new Result($"Failed to complete regular clone: {fetchResult?.Errors}"));
                }
            }

            if (fetchResult.ExitCodeIsFailure &&
                !this.ShowStatusWhileRunning(() =>
            {
                using (ITracer activity = this.tracer.StartActivity("git-fetch", EventLevel.LogAlways))
                {
                    git.DeleteFromLocalConfig("remote.origin.promisor");
                    git.DeleteFromLocalConfig("remote.origin.partialCloneFilter");
                    fetchResult = git.ForegroundFetch("origin");
                    return(fetchResult.ExitCodeIsSuccess);
                }
            },
                                             "Fetching objects from remote"))
            {
                return(new Result($"Failed to complete regular clone: {fetchResult?.Errors}"));
            }

            GitProcess.Result checkoutResult = null;

            if (!this.ShowStatusWhileRunning(() =>
            {
                using (ITracer activity = this.tracer.StartActivity("git-checkout", EventLevel.LogAlways))
                {
                    checkoutResult = git.ForceCheckout(branch);
                    return(checkoutResult.ExitCodeIsSuccess);
                }
            },
                                             $"Checking out '{branch}'"))
            {
                return(new Result($"Failed to complete regular clone: {checkoutResult?.Errors}"));
            }
            return(new Result(true));
        }