Example #1
0
        internal static string ApplyInsteadOfUrlMapping(GitConfig config, string url)
        {
            // See https://git-scm.com/docs/git-config#Documentation/git-config.txt-urlltbasegtinsteadOf
            // Notes:
            //  - URL prefix matching is case sensitive.
            //  - if the replacement is empty the URL is prefixed with the replacement string

            int    longestPrefixLength = -1;
            string?replacement         = null;

            foreach (var variable in config.Variables)
            {
                if (variable.Key.SectionNameEquals(UrlSectionName) &&
                    variable.Key.VariableNameEquals("insteadOf"))
                {
                    foreach (var prefix in variable.Value)
                    {
                        if (prefix.Length > longestPrefixLength && url.StartsWith(prefix, StringComparison.Ordinal))
                        {
                            longestPrefixLength = prefix.Length;
                            replacement         = variable.Key.SubsectionName;
                        }
                    }
                }
            }

            return((longestPrefixLength >= 0) ? replacement + url.Substring(longestPrefixLength) : url);
        }
Example #2
0
        private static bool TryGetRemote(GitConfig config, [NotNullWhen(true)] out string?remoteName, [NotNullWhen(true)] out string?remoteUrl)
        {
            remoteName = RemoteOriginName;
            remoteUrl  = config.GetVariableValue(RemoteSectionName, remoteName, UrlVariableName);
            if (remoteUrl != null)
            {
                return(true);
            }

            var remoteVariable = config.Variables.
                                 Where(kvp => kvp.Key.SectionNameEquals(RemoteSectionName) && kvp.Key.VariableNameEquals(UrlVariableName)).
                                 OrderBy(kvp => kvp.Key.SubsectionName, GitVariableName.SubsectionNameComparer).
                                 FirstOrDefault();

            remoteName = remoteVariable.Key.SubsectionName;
            if (remoteName == null)
            {
                return(false);
            }

            remoteUrl = remoteVariable.Value.Last();
            return(true);
        }
Example #3
0
 internal static string NormalizeUrl(GitConfig config, string url, string root)
 => NormalizeUrl(ApplyInsteadOfUrlMapping(config, url), root);