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
+47 -26
View File
@@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"regexp"
"slices"
"strings"
)
@@ -14,24 +15,26 @@ type user struct {
email string
}
var users = make(map[string]user)
var sb strings.Builder
func main() {
users := make(map[string]user)
// Reads a shell env variable :: author_file
authors := os.Getenv("author_file")
file, err := os.Open(authors)
if err != nil {
print("File not found")
os.Exit(2)
}
defer file.Close()
scanner := bufio.NewScanner(file)
// eat a single input
scanner.Scan()
// reads the input of authors file and formats accordingly
for scanner.Scan() {
info := strings.Split(scanner.Text(), "|")
@@ -39,30 +42,26 @@ func main() {
users[info[0]] = usr
users[info[1]] = usr
}
if err := scanner.Err(); err != nil {
os.Exit(2)
}
args := os.Args[1:]
NoInput(args, users)
excludeMode := []string{}
// 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("([^:]+):([^:]+)")
for _, commiter := range args[1:] {
if _, ok := users[commiter]; ok {
sb.WriteString("\nCo-authored-by: ")
sb.WriteString(users[commiter].username)
sb.WriteString(" <")
sb.WriteString(users[commiter].email)
sb.WriteRune('>')
} else if match := reg.MatchString(commiter); match {
str := strings.Split(commiter, ":")
for _, committer := range args[1:] {
if _, ok := users[committer]; ok {
sb_author(committer)
} else if match := reg.MatchString(committer); match {
str := strings.Split(committer, ":")
sb.WriteString("\nCo-authored-by: ")
sb.WriteString(str[0])
@@ -70,18 +69,32 @@ func main() {
sb.WriteString(str[1])
sb.WriteRune('>')
} else if committer[0] == '^' {
excludeMode = append(excludeMode, users[committer[1:]].username)
} 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 := sb.String()
print(commit)
// commit shell command
cmd := exec.Command("git", "commit", "-m", commit)
// https://stackoverflow.com/questions/18159704/how-to-debug-exit-status-1-error-when-running-exec-command-in-golang
cmd_output, err := cmd.CombinedOutput()
if err != nil {
@@ -92,7 +105,15 @@ func main() {
}
//TODO: move half this into another function and call before building users to improve performance
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
func NoInput(args []string, users map[string]user) {
if len(args) < 2 {
// If you call binary with users prints users
@@ -103,9 +124,9 @@ func NoInput(args []string, users map[string]user) {
}
os.Exit(1)
}
// if calling binary with nothing or only string
// if calling binary with nothing or only string
print("Usage: cocommit <commit message> <co-author1> [co-author2] [co-author3] || \n cocommit <commit message> <co-author1:email> [co-author2:email] [co-author3:email] || Mixes of both")
os.Exit(1)
}
}