public IEnumerable <AzureReposBinding> GetBindings(string orgName = null)
        {
            var globalUsers = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
            var localUsers  = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            IGitConfiguration config = _git.GetConfiguration();

            string orgPrefix = $"{AzureDevOpsConstants.UrnOrgPrefix}/";

            config.Enumerate(
                Constants.GitConfiguration.Credential.SectionName,
                Constants.GitConfiguration.Credential.UserName,
                entry =>
            {
                if (GitConfigurationKeyComparer.TrySplit(entry.Key, out _, out string scope, out _) &&
                    Uri.TryCreate(scope, UriKind.Absolute, out Uri uri) &&
                    uri.Scheme == AzureDevOpsConstants.UrnScheme && uri.AbsolutePath.StartsWith(orgPrefix))
                {
                    string entryOrgName = uri.AbsolutePath.Substring(orgPrefix.Length);
                    if (orgName is null || StringComparer.OrdinalIgnoreCase.Equals(entryOrgName, orgName))
                    {
                        if (entry.Level == GitConfigurationLevel.Local)
                        {
                            localUsers[entryOrgName] = entry.Value;
                        }
                        else
                        {
                            globalUsers[entryOrgName] = entry.Value;
                        }
                    }
                }

                return(true);
            });
        public IEnumerable <AzureReposBinding> GetBindings(string orgName = null)
        {
            var globalUsers = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
            var localUsers  = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            IGitConfiguration config = _git.GetConfiguration();

            string orgPrefix = $"{AzureDevOpsConstants.UrnOrgPrefix}/";

            bool ExtractUserBinding(GitConfigurationEntry entry, IDictionary <string, string> dict)
            {
                if (GitConfigurationKeyComparer.TrySplit(entry.Key, out _, out string scope, out _) &&
                    Uri.TryCreate(scope, UriKind.Absolute, out Uri uri) &&
                    uri.Scheme == AzureDevOpsConstants.UrnScheme && uri.AbsolutePath.StartsWith(orgPrefix))
                {
                    string entryOrgName = uri.AbsolutePath.Substring(orgPrefix.Length);
                    if (string.IsNullOrWhiteSpace(orgName) || StringComparer.OrdinalIgnoreCase.Equals(entryOrgName, orgName))
                    {
                        dict[entryOrgName] = entry.Value;
                    }
                }

                return(true);
            }

            // Only enumerate local configuration if we are inside a repository
            if (_git.IsInsideRepository())
            {
                config.Enumerate(
                    GitConfigurationLevel.Local,
                    Constants.GitConfiguration.Credential.SectionName,
                    Constants.GitConfiguration.Credential.UserName,
                    entry => ExtractUserBinding(entry, localUsers));
            }

            config.Enumerate(
                GitConfigurationLevel.Global,
                Constants.GitConfiguration.Credential.SectionName,
                Constants.GitConfiguration.Credential.UserName,
                entry => ExtractUserBinding(entry, globalUsers));

            foreach (string org in globalUsers.Keys.Union(localUsers.Keys))
            {
                // NOT using the short-circuiting OR operator here on purpose - we need both branches to be evaluated
                if (globalUsers.TryGetValue(org, out string globalUser) | localUsers.TryGetValue(org, out string localUser))
                {
                    yield return(new AzureReposBinding(org, globalUser, localUser));
                }
            }
        }
        public void Clear()
        {
            _trace.WriteLine("Clearing all cached authorities...");

            IGitConfiguration config = _git.GetConfiguration();

            var orgKeys = new HashSet <string>(GitConfigurationKeyComparer.Instance);

            config.Enumerate(
                GitConfigurationLevel.Global,
                Constants.GitConfiguration.Credential.SectionName,
                AzureDevOpsConstants.GitConfiguration.Credential.AzureAuthority,
                entry =>
            {
                if (GitConfigurationKeyComparer.TrySplit(entry.Key, out _, out string scope, out _) &&
                    Uri.TryCreate(scope, UriKind.Absolute, out Uri orgUrn) &&
                    orgUrn.Scheme == AzureDevOpsConstants.UrnScheme)
                {
                    orgKeys.Add(entry.Key);
                }

                return(true);
            });