コード例 #1
0
        /// <summary>
        /// Try and get the value of a configuration entry as a string.
        /// </summary>
        /// <param name="config">Configuration object.</param>
        /// <param name="section">Configuration section name.</param>
        /// <param name="scope">Configuration section scope.</param>
        /// <param name="property">Configuration property name.</param>
        /// <param name="value">Configuration entry value.</param>
        /// <returns>True if the value was found, false otherwise.</returns>
        public static bool TryGetValue(this IGitConfiguration config, string section, string scope, string property, out string value)
        {
            if (scope is null)
            {
                return(TryGetValue(config, section, property, out value));
            }

            return(config.TryGetValue($"{section}.{scope}.{property}", out value));
        }
コード例 #2
0
        /// <summary>
        /// Get the value of a configuration entry as a string.
        /// </summary>
        /// <exception cref="System.Collections.Generic.KeyNotFoundException">A configuration entry with the specified key was not found.</exception>
        /// <param name="config">Configuration object.</param>
        /// <param name="name">Configuration entry name.</param>
        /// <returns>Configuration entry value.</returns>
        public static string GetValue(this IGitConfiguration config, string name)
        {
            if (!config.TryGetValue(name, out string value))
            {
                throw new KeyNotFoundException($"Git configuration entry with the name '{name}' was not found.");
            }

            return(value);
        }
コード例 #3
0
        public void GitConfiguration_TryGetValue_Name_DoesNotExists_ReturnsFalse()
        {
            string repoPath = CreateRepository();

            string            gitPath = GetGitPath();
            var               trace   = new NullTrace();
            var               git     = new GitProcess(trace, gitPath, repoPath);
            IGitConfiguration config  = git.GetConfiguration();

            string randomName = $"{Guid.NewGuid():N}.{Guid.NewGuid():N}";
            bool   result     = config.TryGetValue(randomName, out string value);

            Assert.False(result);
            Assert.Null(value);
        }
コード例 #4
0
        public void GitConfiguration_TryGetValue_SectionProperty_DoesNotExists_ReturnsFalse()
        {
            string repoPath = CreateRepository();

            string            gitPath = GetGitPath();
            var               trace   = new NullTrace();
            var               git     = new GitProcess(trace, gitPath, repoPath);
            IGitConfiguration config  = git.GetConfiguration();

            string randomSection  = Guid.NewGuid().ToString("N");
            string randomProperty = Guid.NewGuid().ToString("N");
            bool   result         = config.TryGetValue(randomSection, randomProperty, out string value);

            Assert.False(result);
            Assert.Null(value);
        }
コード例 #5
0
        public void GitConfiguration_TryGetValue_SectionScopeProperty_NullScope_ReturnsTrueOutUnscopedString()
        {
            string repoPath = CreateRepository(out string workDirPath);

            Git(repoPath, workDirPath, "config --local user.name john.doe").AssertSuccess();

            string            gitPath = GetGitPath();
            var               trace   = new NullTrace();
            var               git     = new GitProcess(trace, gitPath, repoPath);
            IGitConfiguration config  = git.GetConfiguration();

            bool result = config.TryGetValue("user", null, "name", out string value);

            Assert.True(result);
            Assert.NotNull(value);
            Assert.Equal("john.doe", value);
        }
コード例 #6
0
        public Task ConfigureAsync(
            IEnvironment environment, EnvironmentVariableTarget environmentTarget,
            IGit git, GitConfigurationLevel configurationLevel)
        {
            string useHttpPathKey = $"{KnownGitCfg.Credential.SectionName}.https://dev.azure.com.{KnownGitCfg.Credential.UseHttpPath}";

            IGitConfiguration targetConfig = git.GetConfiguration(configurationLevel);

            if (targetConfig.TryGetValue(useHttpPathKey, out string currentValue) && currentValue.IsTruthy())
            {
                Context.Trace.WriteLine("Git configuration 'credential.useHttpPath' is already set to 'true' for https://dev.azure.com.");
            }
            else
            {
                Context.Trace.WriteLine("Setting Git configuration 'credential.useHttpPath' to 'true' for https://dev.azure.com...");
                targetConfig.SetValue(useHttpPathKey, "true");
            }

            return(Task.CompletedTask);
        }
        public Task ConfigureAsync(ConfigurationTarget target)
        {
            string useHttpPathKey = $"{KnownGitCfg.Credential.SectionName}.https://dev.azure.com.{KnownGitCfg.Credential.UseHttpPath}";

            GitConfigurationLevel configurationLevel = target == ConfigurationTarget.System
                ? GitConfigurationLevel.System
                : GitConfigurationLevel.Global;

            IGitConfiguration targetConfig = _context.Git.GetConfiguration(configurationLevel);

            if (targetConfig.TryGetValue(useHttpPathKey, out string currentValue) && currentValue.IsTruthy())
            {
                _context.Trace.WriteLine("Git configuration 'credential.useHttpPath' is already set to 'true' for https://dev.azure.com.");
            }
            else
            {
                _context.Trace.WriteLine("Setting Git configuration 'credential.useHttpPath' to 'true' for https://dev.azure.com...");
                targetConfig.SetValue(useHttpPathKey, "true");
            }

            return(Task.CompletedTask);
        }
コード例 #8
0
        /// <summary>
        /// Try and get the all values of a specified setting as specified in the environment and Git configuration,
        /// in the correct order or precedence.
        /// </summary>
        /// <param name="envarName">Optional environment variable name.</param>
        /// <param name="section">Optional Git configuration section name.</param>
        /// <param name="property">Git configuration property name. Required if <paramref name="section"/> is set, optional otherwise.</param>
        /// <returns>All values for the specified setting, in order of precedence, or an empty collection if no such values are set.</returns>
        public IEnumerable <string> GetSettingValues(string envarName, string section, string property)
        {
            string value;

            if (envarName != null)
            {
                if (_environment.Variables.TryGetValue(envarName, out value))
                {
                    yield return(value);
                }
            }

            if (section != null && property != null)
            {
                IGitConfiguration config = GetGitConfiguration();

                if (RemoteUri != null)
                {
                    /*
                     * Look for URL scoped "section" configuration entries, starting from the most specific
                     * down to the least specific (stopping before the TLD).
                     *
                     * In a divergence from standard Git configuration rules, we also consider matching URL scopes
                     * without a scheme ("protocol://").
                     *
                     * For each level of scope, we look for an entry with the scheme included (the default), and then
                     * also one without it specified. This allows you to have one configuration scope for both "http" and
                     * "https" without needing to repeat yourself, for example.
                     *
                     * For example, starting with "https://foo.example.com/bar/buzz" we have:
                     *
                     *   1a. [section "https://foo.example.com/bar/buzz"]
                     *          property = value
                     *
                     *   1b. [section "foo.example.com/bar/buzz"]
                     *          property = value
                     *
                     *   2a. [section "https://foo.example.com/bar"]
                     *          property = value
                     *
                     *   2b. [section "foo.example.com/bar"]
                     *          property = value
                     *
                     *   3a. [section "https://foo.example.com"]
                     *          property = value
                     *
                     *   3b. [section "foo.example.com"]
                     *          property = value
                     *
                     *   4a. [section "https://example.com"]
                     *          property = value
                     *
                     *   4b. [section "example.com"]
                     *          property = value
                     *
                     */

                    // Enumerate all configuration entries with the correct section and property name
                    // and make a local copy of them here to avoid needing to call `TryGetValue` on the
                    // IGitConfiguration object multiple times in a loop below.
                    var configEntries = new Dictionary <string, string>();
                    config.Enumerate((entryName, entryValue) =>
                    {
                        string entrySection  = entryName.TruncateFromIndexOf('.');
                        string entryProperty = entryName.TrimUntilLastIndexOf('.');

                        if (StringComparer.OrdinalIgnoreCase.Equals(entrySection, section) &&
                            StringComparer.OrdinalIgnoreCase.Equals(entryProperty, property))
                        {
                            configEntries[entryName] = entryValue;
                        }

                        // Continue the enumeration
                        return(true);
                    });

                    foreach (string scope in RemoteUri.GetGitConfigurationScopes())
                    {
                        string queryName = $"{section}.{scope}.{property}";
                        // Look for a scoped entry that includes the scheme "protocol://example.com" first as this is more specific
                        if (configEntries.TryGetValue(queryName, out value))
                        {
                            yield return(value);
                        }

                        // Now look for a scoped entry that omits the scheme "example.com" second as this is less specific
                        string scopeWithoutScheme  = scope.TrimUntilIndexOf(Uri.SchemeDelimiter);
                        string queryWithSchemeName = $"{section}.{scopeWithoutScheme}.{property}";
                        if (configEntries.TryGetValue(queryWithSchemeName, out value))
                        {
                            yield return(value);
                        }
                    }
                }

                /*
                 * Try to look for an un-scoped "section" property setting:
                 *
                 *    [section]
                 *        property = value
                 *
                 */
                if (config.TryGetValue($"{section}.{property}", out value))
                {
                    yield return(value);
                }
            }
        }
コード例 #9
0
 /// <summary>
 /// Try and get the value of a configuration entry as a string.
 /// </summary>
 /// <param name="config">Configuration object.</param>
 /// <param name="section">Configuration section name.</param>
 /// <param name="property">Configuration property name.</param>
 /// <param name="value">Configuration entry value.</param>
 /// <returns>True if the value was found, false otherwise.</returns>
 public static bool TryGetValue(this IGitConfiguration config, string section, string property, out string value)
 {
     return(config.TryGetValue($"{section}.{property}", out value));
 }