예제 #1
0
        public AzureReposBinding GetBinding(string orgName)
        {
            EnsureArgument.NotNullOrWhiteSpace(orgName, nameof(orgName));

            string orgKey = GetOrgUserKey(orgName);

            IGitConfiguration config = _git.GetConfiguration();

            _trace.WriteLine($"Looking up organization binding for '{orgName}'...");

            // NOT using the short-circuiting OR operator here on purpose - we need both branches to be evaluated
            if (config.TryGet(GitConfigurationLevel.Local, orgKey, out string localUser) |
                config.TryGet(GitConfigurationLevel.Global, orgKey, out string globalUser))
            {
                return(new AzureReposBinding(orgName, globalUser, localUser));
            }

            // No bound user
            return(null);
        }
        public void GitConfiguration_TryGet_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.TryGet(randomName, out string value);

            Assert.False(result);
            Assert.Null(value);
        }
        public string GetAuthority(string orgName)
        {
            EnsureArgument.NotNullOrWhiteSpace(orgName, nameof(orgName));

            _trace.WriteLine($"Looking up cached authority for organization '{orgName}'...");

            IGitConfiguration config = _git.GetConfiguration();

            if (config.TryGet(GitConfigurationLevel.Global, GetAuthorityKey(orgName), out string authority))
            {
                return(authority);
            }

            return(null);
        }
        public void GitConfiguration_TryGet_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.TryGet(randomSection, randomProperty, out string value);

            Assert.False(result);
            Assert.Null(value);
        }
        public void GitConfiguration_TryGet_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.TryGet("user", null, "name", out string value);

            Assert.True(result);
            Assert.NotNull(value);
            Assert.Equal("john.doe", value);
        }
예제 #6
0
        public void GitConfiguration_TryGet_IsPath_False_ReturnsRawConfig()
        {
            string repoPath = CreateRepository(out string workDirPath);

            ExecGit(repoPath, workDirPath, "config --local example.path ~/test").AssertSuccess();

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

            bool result = config.TryGet("example.path", false, out string value);

            Assert.True(result);
            Assert.NotNull(value);
            Assert.Equal($"~/test", value);
        }
예제 #7
0
        public void GitConfiguration_TryGet_Name_Exists_ReturnsTrueOutString()
        {
            string repoPath = CreateRepository(out string workDirPath);

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

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

            bool result = config.TryGet("user.name", false, out string value);

            Assert.True(result);
            Assert.NotNull(value);
            Assert.Equal("john.doe", value);
        }
예제 #8
0
        public void GitConfiguration_TryGet_BoolWithoutType_ReturnsRawConfig()
        {
            string repoPath = CreateRepository(out string workDirPath);

            ExecGit(repoPath, workDirPath, "config --local example.bool fAlSe").AssertSuccess();

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

            bool result = config.TryGet(GitConfigurationLevel.Local, GitConfigurationType.Raw,
                                        "example.bool", out string value);

            Assert.True(result);
            Assert.NotNull(value);
            Assert.Equal("fAlSe", value);
        }
예제 #9
0
        public void GitConfiguration_TryGet_IsPath_True_ReturnsCanonicalPath()
        {
            string repoPath = CreateRepository(out string workDirPath);

            ExecGit(repoPath, workDirPath, "config --local example.path ~/test").AssertSuccess();

            string            homeDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            string            gitPath       = GetGitPath();
            var               trace         = new NullTrace();
            var               env           = new TestEnvironment();
            var               git           = new GitProcess(trace, env, gitPath, repoPath);
            IGitConfiguration config        = git.GetConfiguration();

            bool result = config.TryGet("example.path", true, out string value);

            Assert.True(result);
            Assert.NotNull(value);
            Assert.Equal(Path.GetFullPath($"{homeDirectory}/test").Replace("\\", "/"), value);
        }
        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();

            if (targetConfig.TryGet(useHttpPathKey, false, 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.Set(configurationLevel, useHttpPathKey, "true");
            }

            return(Task.CompletedTask);
        }
        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 = _git.GetConfiguration();

                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
                     *
                     * It is also important to note that although the section and property names are NOT case
                     * sensitive, the "scope" part IS case sensitive! We must be careful when searching to ensure
                     * we follow Git's rules.
                     *
                     */

                    // 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>(GitConfigurationKeyComparer.Instance);
                    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.TryGet($"{section}.{property}", out value))
                {
                    yield return(value);
                }
            }
        }