From a3391da14ef73b486d7abf6a48ac05081b18dff5 Mon Sep 17 00:00:00 2001 From: Theis Date: Tue, 5 Mar 2024 08:41:00 +0100 Subject: [PATCH] added a default exclusion test and cleaned the code up with some comments --- src_code/go_src/cocommit.go | 19 +++++++++++++++---- src_code/go_src/cocommit_test.go | 19 +++++++++++++++++-- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src_code/go_src/cocommit.go b/src_code/go_src/cocommit.go index 4ab24bc..e99514c 100644 --- a/src_code/go_src/cocommit.go +++ b/src_code/go_src/cocommit.go @@ -15,9 +15,14 @@ type user struct { email string } +// Map of all th users in the author file var users = make(map[string]user) +// String builder for building the commit message var sb strings.Builder +// Flag that can be toggled to include all users in a commit message (excluding defExclude) var all_flag = false +// DefaultExclude -> A list that contains users marked with ex meaning +// they should not be included in all and negations var defExclude = []string{} func main() { @@ -44,7 +49,7 @@ func main() { usr := user{username: info[2], email: info[3]} users[info[0]] = usr users[info[1]] = usr - + // Adds users with the ex tag to the defExclude list if len(info) > 4 { if info[4] == "ex" { defExclude = append(defExclude, info[2]) @@ -56,15 +61,19 @@ func main() { if err := scanner.Err(); err != nil { os.Exit(2) } - + // Removes the call command for the program args := os.Args[1:] + // Checks if the user called the program with any inputs or with non commit args NoInput(args, users) + // This list is used when doing negations and for removing duplicate users during string building excludeMode := []string{} // builds the commit message with the selected authors sb.WriteString(string(args[0]) + "\n") + + // Regex that catches one off authors reg, _ := regexp.Compile("([^:]+):([^:]+)") if args[1] == "all" || args[1] == "All" { @@ -72,7 +81,7 @@ func main() { goto skip_loop } - + // Loop that adds users for _, committer := range args[1:] { if _, ok := users[committer]; ok { sb_author(committer) @@ -85,7 +94,7 @@ func main() { sb.WriteString(str[1]) sb.WriteRune('>') - } else if committer[0] == '^' { + } else if committer[0] == '^' { // Negations excludeMode = append(excludeMode, users[committer[1:]].username) } else { @@ -93,9 +102,11 @@ func main() { } } + // Skip label for adding all skip_loop: if len(excludeMode) > 0 || all_flag { + // adds all users not in the excludeMode list add_x_users(excludeMode) } diff --git a/src_code/go_src/cocommit_test.go b/src_code/go_src/cocommit_test.go index cf08549..7113b44 100644 --- a/src_code/go_src/cocommit_test.go +++ b/src_code/go_src/cocommit_test.go @@ -65,7 +65,7 @@ func Test_add_all(t *testing.T) { if !strings.Contains(commit, "\nCo-authored-by: test1 ") || !strings.Contains(commit, "\nCo-authored-by: test2 ") || !strings.Contains(commit, "\nCo-authored-by: test3 ") { - t.Fatalf("String built incorrectly. Strings did not match: Created -> %s Expected -> Co-authored-by: test ",commit) + t.Fatalf("String built incorrectly. Strings did not match: Created -> %s Expected -> Co-authored-by: test1 \nCo-authored-by: test2 \n\nCo-authored-by: test3 ",commit) } } @@ -79,10 +79,25 @@ func Test_exclude_user(t *testing.T) { commit := sb_build() if strings.Contains(commit, "\nCo-authored-by: test1 ") { - t.Fatalf("String built incorrectly. Strings did not match: Created -> %s Expected -> Co-authored-by: test ",commit) + t.Fatalf("String built incorrectly. Strings did not match: Created -> %s Expected -> Co-authored-by: test2 \n\nCo-authored-by: test3 ",commit) } } +func Test_exclude_by_default(t *testing.T) { + // Reusing users from before + defExclude = append(defExclude, users["test1"].username) + + sb.Reset() + + add_x_users([]string{}) + + commit := sb_build() + + if strings.Contains(commit, "\nCo-authored-by: test1 ") { + t.Fatalf("String built incorrectly. Strings did not match: Created -> %s Expected -> Co-authored-by: test2 \n\nCo-authored-by: test3 ",commit) + } +} +