refactor: rewrite parts of the commit function to be a bit more sane, removed the jump as well

This commit is contained in:
Slug-Boi
2025-04-09 19:58:43 +02:00
parent 7b997c407e
commit 6e0d7c57f2
+19 -22
View File
@@ -10,33 +10,31 @@ import (
// This util file is used to create a commit message using a string builder // This util file is used to create a commit message using a string builder
// string builder for the commit message
var sb strings.Builder
// list of excluded authors based on the author file
var excludeMode = []string{}
// Regex pattern used to create temp users to add to the commit message // Regex pattern used to create temp users to add to the commit message
var reg, _ = regexp.Compile("([^:]+):([^:]+)") var reg, _ = regexp.Compile("([^:]+):([^:]+)")
func Commit(message string, authors []string) string { func Commit(message string, authors []string) string {
// string builder for the commit message
var sb strings.Builder
excludeMode := []string{}
// write the commit message to the string builder // write the commit message to the string builder
sb.WriteString(message + "\n") sb.WriteString(message + "\n")
fst := authors[0] fst := authors[0]
if fst == "all" || fst == "All" { if fst == "all" || fst == "All" {
add_x_users(excludeMode) add_x_users(excludeMode, &sb)
goto skip_loop return sb.String()
} else if Groups[fst] != nil { } else if Groups[fst] != nil {
excludeMode = group_selection(Groups[fst], excludeMode) excludeMode = group_selection(Groups[fst], excludeMode)
add_x_users(excludeMode) add_x_users(excludeMode, &sb)
goto skip_loop return sb.String()
} }
// Loop that adds users // Loop that adds users
for _, committer := range authors { for _, committer := range authors {
if _, ok := Users[committer]; ok { if _, ok := Users[committer]; ok {
sb_author(committer) sb_author(committer, &sb)
} else if match := reg.MatchString(committer); match { } else if match := reg.MatchString(committer); match {
str := strings.Split(committer, ":") str := strings.Split(committer, ":")
@@ -51,13 +49,12 @@ func Commit(message string, authors []string) string {
} else { } else {
println(committer, " was unknown. User either not defined or name typed wrong") println(committer, " was unknown. User either not defined or name typed wrong")
} }
} }
if len(excludeMode) > 0 {
add_x_users(excludeMode)
}
// Skip label for edge cases at top of function // Add excluded users after processing all authors
skip_loop: if len(excludeMode) > 0 {
add_x_users(excludeMode, &sb)
}
return sb.String() return sb.String()
} }
@@ -95,7 +92,7 @@ func GitPush() {
} }
// helper function to add an author to the commit message // helper function to add an author to the commit message
func sb_author(committer string) { func sb_author(committer string, sb *strings.Builder) {
sb.WriteString("\nCo-authored-by: ") sb.WriteString("\nCo-authored-by: ")
sb.WriteString(Users[committer].Username) sb.WriteString(Users[committer].Username)
sb.WriteString(" <") sb.WriteString(" <")
@@ -104,13 +101,13 @@ func sb_author(committer string) {
} }
// helper function to add x amount of users to the commit message // helper function to add x amount of users to the commit message
func add_x_users(excludeMode []string) { func add_x_users(excludeMode []string, sb *strings.Builder) {
if len(DefExclude) > 0 { if len(DefExclude) > 0 {
excludeMode = append(excludeMode, DefExclude...) excludeMode = append(excludeMode, DefExclude...)
} }
for key, user := range Users { for key, user := range Users {
if !slices.Contains(excludeMode, user.Username) { if !slices.Contains(excludeMode, user.Username) {
sb_author(key) sb_author(key, sb)
excludeMode = append(excludeMode, user.Username) excludeMode = append(excludeMode, user.Username)
} }
} }