package git import ( "regexp" "strings" ) // basicGitBranchNameRegex is a regular expression to validate a git branch name // it does not cover all cases as positive lookaheads are not supported in Go's regexp var basicGitBranchNameRegex = regexp.MustCompile(`^[a-zA-Z0-9\-\_\/\.]+$`) // IsValidGitBranchName checks if a branch name is valid. // It uses the following regexp `^[a-zA-Z0-9\-\_\/\.]+$` to validate the branch name with some additional checks that must satisfy the following rules: // 1. The branch name must have at least one character and must not be empty. // 2. The branch name cannot start with `/` or end with `/`, `.`, or whitespace. // 3. The branch name cannot contain consecutive slashes (`//`). // 4. The branch name cannot contain consecutive dots (`..`). // 5. The branch name cannot contain `@{`. // 6. The branch name cannot include the following characters: `~`, `^`, `:`, `?`, `*`, `[`, `\`, or `]`. func IsValidGitBranchName(branch string) bool { if !basicGitBranchNameRegex.MatchString(branch) { return false } // Additional checks for invalid patterns if strings.HasPrefix(branch, "/") || strings.HasSuffix(branch, "/") || strings.HasSuffix(branch, ".") || strings.Contains(branch, "..") || strings.Contains(branch, "//") || strings.HasSuffix(branch, ".lock") { return false } return true }