Example #1
0
        /// <summary>
        /// Ensures that the branch name meets the GIT branch naming conventions.
        /// For more details refer to <see href="https://www.git-scm.com/docs/git-check-ref-format/1.8.2"/>.
        /// </summary>
        /// <param name="branchName">Name of the branch.</param>
        /// <param name="options">The options.</param>
        /// <returns>Normalised branch name.</returns>
        public string Normalise(string branchName, GitBranchNameOptions options)
        {
            if (string.IsNullOrWhiteSpace(branchName))
            {
                return(string.Empty);
            }

            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            // run rules in reverse order
            branchName = Rule09(branchName, options);
            branchName = Rule08(branchName, options);
            branchName = Rule07(branchName, options);
            branchName = Rule05(branchName, options);
            branchName = Rule04(branchName, options);
            branchName = Rule03(branchName, options);
            // rule #2 is not applicable
            // rule #6 runs as second last to ensure no consecutive '/' are left after previous normalisations
            branchName = Rule06(branchName, options);
            branchName = Rule01(branchName, options);

            return(branchName);
        }
        /// <summary>
        /// Ensures that the branch name meets the GIT branch naming conventions.
        /// For more details refer to <see href="https://www.git-scm.com/docs/git-check-ref-format/1.8.2"/>.
        /// </summary>
        /// <param name="branchName">Name of the branch.</param>
        /// <param name="options">The options.</param>
        /// <returns>Normalised branch name.</returns>
        public string Normalise(string branchName, GitBranchNameOptions options)
        {
            if (string.IsNullOrWhiteSpace(branchName))
            {
                return string.Empty;
            }

            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            // run rules in reverse order
            branchName = Rule09(branchName, options);
            branchName = Rule08(branchName, options);
            branchName = Rule07(branchName, options);
            branchName = Rule05(branchName, options);
            branchName = Rule04(branchName, options);
            branchName = Rule03(branchName, options);
            // rule #2 is not applicable
            // rule #6 runs as second last to ensure no consecutive '/' are left after previous normalisations
            branchName = Rule06(branchName, options);
            branchName = Rule01(branchName, options);

            return branchName;
        }
Example #3
0
 /// <summary>
 /// Branch name begin or end with a slash '/' or contain multiple consecutive slashes.
 /// </summary>
 /// <param name="branchName">Name of the branch.</param>
 /// <param name="options">The options.</param>
 /// <returns>Normalised branch name.</returns>
 internal string Rule06(string branchName, GitBranchNameOptions options)
 {
     branchName = Regex.Replace(branchName, @"(\/{2,})", "/");
     if (branchName.StartsWith("/"))
     {
         branchName = branchName.Substring(1);
     }
     if (branchName.EndsWith("/"))
     {
         branchName = branchName.Substring(0, branchName.Length - 1);
     }
     return(branchName);
 }
Example #4
0
        /// <summary>
        /// Branch name can include slash '/' for hierarchical (directory) grouping,
        /// but no slash-separated component can begin with a dot '.' or end with the sequence '.lock'.
        /// </summary>
        /// <param name="branchName"></param>
        /// <param name="options"></param>
        /// <returns>Normalised branch name.</returns>
        internal string Rule01(string branchName, GitBranchNameOptions options)
        {
            var tokens = branchName.Split('/').ToList();

            for (int i = 0; i < tokens.Count; i++)
            {
                if (tokens[i].StartsWith("."))
                {
                    tokens[i] = Regex.Replace(tokens[i], "^(\\.)*", options.ReplacementToken);
                }
                if (tokens[i].EndsWith(".lock", StringComparison.OrdinalIgnoreCase))
                {
                    tokens[i] = Regex.Replace(tokens[i], "(\\.lock)$", options.ReplacementToken + "lock");
                }
            }
            return(tokens.Join("/"));
        }
Example #5
0
        /// <summary>
        /// Branch name cannot have ASCII control characters (i.e. bytes whose values are lower than \040, or \177 'DEL'),
        /// space, tilde '~', caret '^', or colon ':' anywhere.
        /// </summary>
        /// <param name="branchName">Name of the branch.</param>
        /// <param name="options">The options.</param>
        /// <returns>Normalised branch name.</returns>
        internal string Rule04(string branchName, GitBranchNameOptions options)
        {
            var result = new StringBuilder(branchName.Length);

            foreach (char t in branchName)
            {
                if (IsValidChar(t))
                {
                    result.Append(t);
                }
                else
                {
                    result.Append(options.ReplacementToken);
                }
            }
            return(result.ToString());
        }
Example #6
0
 /// <summary>
 /// Branch name cannot contain a sequence '@{'.
 /// </summary>
 /// <param name="branchName">Name of the branch.</param>
 /// <param name="options">The options.</param>
 /// <returns>Normalised branch name.</returns>
 internal string Rule08(string branchName, GitBranchNameOptions options)
 {
     return(Regex.Replace(branchName, "(@\\{)", options.ReplacementToken));
 }
Example #7
0
 /// <summary>
 /// Branch name cannot have question-mark '?', asterisk '*', or open bracket '[' anywhere.
 /// </summary>
 /// <param name="branchName">Name of the branch.</param>
 /// <param name="options">The options.</param>
 /// <returns>Normalised branch name.</returns>
 internal string Rule05(string branchName, GitBranchNameOptions options)
 {
     return(Regex.Replace(branchName, "(\\?|\\*|\\[)", options.ReplacementToken));
 }
Example #8
0
 /// <summary>
 /// Branch name cannot have two consecutive dots '..' anywhere.
 /// </summary>
 /// <param name="branchName">Name of the branch.</param>
 /// <param name="options">The options.</param>
 /// <returns>Normalised branch name.</returns>
 internal string Rule03(string branchName, GitBranchNameOptions options)
 {
     return(Regex.Replace(branchName, "\\.{2,}", options.ReplacementToken));
 }
 public void Normalise(string input, string replacementToken, string expected)
 {
     _gitBranchNameOptions = new GitBranchNameOptions(replacementToken);
     _gitBranchNameNormaliser.Normalise(input, _gitBranchNameOptions).Should().Be(expected);
 }
 /// <summary>
 /// Branch name cannot contain a sequence '@{'.
 /// </summary>
 /// <param name="branchName">Name of the branch.</param>
 /// <param name="options">The options.</param>
 /// <returns>Normalised branch name.</returns>
 internal string Rule08(string branchName, GitBranchNameOptions options)
 {
     return Regex.Replace(branchName, "(@\\{)", options.ReplacementToken);
 }
Example #11
0
 /// <summary>
 /// Branch name cannot be the single character '@'.
 /// </summary>
 /// <param name="branchName">Name of the branch.</param>
 /// <param name="options">The options.</param>
 /// <returns>Normalised branch name.</returns>
 internal string Rule09(string branchName, GitBranchNameOptions options)
 {
     return(branchName != "@" ? branchName : options.ReplacementToken);
 }
 /// <summary>
 /// Branch name cannot have question-mark '?', asterisk '*', or open bracket '[' anywhere.
 /// </summary>
 /// <param name="branchName">Name of the branch.</param>
 /// <param name="options">The options.</param>
 /// <returns>Normalised branch name.</returns>
 internal string Rule05(string branchName, GitBranchNameOptions options)
 {
     return Regex.Replace(branchName, "(\\?|\\*|\\[)", options.ReplacementToken);
 }
 /// <summary>
 /// Branch name begin or end with a slash '/' or contain multiple consecutive slashes.
 /// </summary>
 /// <param name="branchName">Name of the branch.</param>
 /// <param name="options">The options.</param>
 /// <returns>Normalised branch name.</returns>
 internal string Rule06(string branchName, GitBranchNameOptions options)
 {
     branchName = Regex.Replace(branchName, @"(\/{2,})", "/");
     if (branchName.StartsWith("/"))
     {
         branchName = branchName.Substring(1);
     }
     if (branchName.EndsWith("/"))
     {
         branchName = branchName.Substring(0, branchName.Length - 1);
     }
     return branchName;
 }
 /// <summary>
 /// Branch name cannot have ASCII control characters (i.e. bytes whose values are lower than \040, or \177 'DEL'), 
 /// space, tilde '~', caret '^', or colon ':' anywhere.
 /// </summary>
 /// <param name="branchName">Name of the branch.</param>
 /// <param name="options">The options.</param>
 /// <returns>Normalised branch name.</returns>
 internal string Rule04(string branchName, GitBranchNameOptions options)
 {
     var result = new StringBuilder(branchName.Length);
     foreach (char t in branchName)
     {
         if (IsValidChar(t))
         {
             result.Append(t);
         }
         else
         {
             result.Append(options.ReplacementToken);
         }
     }
     return result.ToString();
 }
 /// <summary>
 /// Branch name cannot have two consecutive dots '..' anywhere.
 /// </summary>
 /// <param name="branchName">Name of the branch.</param>
 /// <param name="options">The options.</param>
 /// <returns>Normalised branch name.</returns>
 internal string Rule03(string branchName, GitBranchNameOptions options)
 {
     return Regex.Replace(branchName, "\\.{2,}", options.ReplacementToken);
 }
 /// <summary>
 /// Branch name can include slash '/' for hierarchical (directory) grouping, 
 /// but no slash-separated component can begin with a dot '.' or end with the sequence '.lock'.
 /// </summary>
 /// <param name="branchName"></param>
 /// <param name="options"></param>
 /// <returns>Normalised branch name.</returns>
 internal string Rule01(string branchName, GitBranchNameOptions options)
 {
     var tokens = branchName.Split('/').ToList();
     for (int i = 0; i < tokens.Count; i++)
     {
         if (tokens[i].StartsWith("."))
         {
             tokens[i] = Regex.Replace(tokens[i], "^(\\.)*", options.ReplacementToken);
         }
         if (tokens[i].EndsWith(".lock", StringComparison.OrdinalIgnoreCase))
         {
             tokens[i] = Regex.Replace(tokens[i], "(\\.lock)$", options.ReplacementToken + "lock");
         }
     }
     return tokens.Join("/");
 }
Example #17
0
 /// <summary>
 /// Branch name cannot contain a '\'.
 /// </summary>
 /// <param name="branchName">Name of the branch.</param>
 /// <param name="options">The options.</param>
 /// <returns>Normalised branch name.</returns>
 internal string Rule09(string branchName, GitBranchNameOptions options)
 {
     return(Regex.Replace(branchName, @"(\\{1,})", options.ReplacementToken));
 }
        public void ReplacementToken_can_be_null_or_empty(string token, string expected) {
            var options = new GitBranchNameOptions(token);

            options.ReplacementToken.Should().Be(expected);
        }
 /// <summary>
 /// Branch name cannot contain a '\'.
 /// </summary>
 /// <param name="branchName">Name of the branch.</param>
 /// <param name="options">The options.</param>
 /// <returns>Normalised branch name.</returns>
 internal string Rule09(string branchName, GitBranchNameOptions options)
 {
     return Regex.Replace(branchName, @"(\\{1,})", options.ReplacementToken);
 }
 public void Setup()
 {
     _gitBranchNameNormaliser = new GitBranchNameNormaliser();
     _gitBranchNameOptions = new GitBranchNameOptions("_");
 }