added the exclude feature so that you much more easily can write commits

This commit is contained in:
Theis
2024-03-04 21:49:11 +01:00
parent 2ab5c47eca
commit 20974803c1
+33 -12
View File
@@ -6,6 +6,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"regexp" "regexp"
"slices"
"strings" "strings"
) )
@@ -14,8 +15,10 @@ type user struct {
email string email string
} }
var users = make(map[string]user)
var sb strings.Builder
func main() { func main() {
users := make(map[string]user)
// Reads a shell env variable :: author_file // Reads a shell env variable :: author_file
authors := os.Getenv("author_file") authors := os.Getenv("author_file")
@@ -48,21 +51,17 @@ func main() {
NoInput(args, users) NoInput(args, users)
excludeMode := []string{}
// builds the commit message with the selected authors // builds the commit message with the selected authors
var sb strings.Builder
sb.WriteString(string(args[0]) + "\n") sb.WriteString(string(args[0]) + "\n")
reg, _ := regexp.Compile("([^:]+):([^:]+)") reg, _ := regexp.Compile("([^:]+):([^:]+)")
for _, commiter := range args[1:] { for _, committer := range args[1:] {
if _, ok := users[commiter]; ok { if _, ok := users[committer]; ok {
sb.WriteString("\nCo-authored-by: ") sb_author(committer)
sb.WriteString(users[commiter].username) } else if match := reg.MatchString(committer); match {
sb.WriteString(" <") str := strings.Split(committer, ":")
sb.WriteString(users[commiter].email)
sb.WriteRune('>')
} else if match := reg.MatchString(commiter); match {
str := strings.Split(commiter, ":")
sb.WriteString("\nCo-authored-by: ") sb.WriteString("\nCo-authored-by: ")
sb.WriteString(str[0]) sb.WriteString(str[0])
@@ -70,13 +69,27 @@ func main() {
sb.WriteString(str[1]) sb.WriteString(str[1])
sb.WriteRune('>') sb.WriteRune('>')
} else if committer[0] == '^' {
excludeMode = append(excludeMode, users[committer[1:]].username)
} else { } else {
println(commiter, " 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 {
for key, user := range users {
if !slices.Contains(excludeMode, user.username) {
sb_author(key)
excludeMode = append(excludeMode, user.username)
}
} }
} }
// commit msg built // commit msg built
commit := sb.String() commit := sb.String()
print(commit)
// commit shell command // commit shell command
cmd := exec.Command("git", "commit", "-m", commit) cmd := exec.Command("git", "commit", "-m", commit)
@@ -92,6 +105,14 @@ func main() {
} }
func sb_author(committer string) {
sb.WriteString("\nCo-authored-by: ")
sb.WriteString(users[committer].username)
sb.WriteString(" <")
sb.WriteString(users[committer].email)
sb.WriteRune('>')
}
// TODO: move half this into another function and call before building users to improve performance // TODO: move half this into another function and call before building users to improve performance
func NoInput(args []string, users map[string]user) { func NoInput(args []string, users map[string]user) {
if len(args) < 2 { if len(args) < 2 {