From 35ff3ce2de55ca5ccbdcaa777bc38bbd2117512d Mon Sep 17 00:00:00 2001 From: Pete Cornish Date: Sun, 30 Apr 2023 23:28:02 +0100 Subject: [PATCH] fix: trim commit messages to first line only. --- vcs/operations.go | 13 ++++++++++++- vcs/operations_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 vcs/operations_test.go diff --git a/vcs/operations.go b/vcs/operations.go index b21447c..94541ca 100644 --- a/vcs/operations.go +++ b/vcs/operations.go @@ -130,7 +130,8 @@ func fetchCommitsAfter(repoPath string, tag string) ([]string, error) { if c.Hash == afterTagCommit.Hash { return storer.ErrStop } - commitMessages = append(commitMessages, strings.TrimSpace(c.Message)) + message := getShortMessage(c.Message) + commitMessages = append(commitMessages, message) return nil }) if err != nil { @@ -138,3 +139,13 @@ func fetchCommitsAfter(repoPath string, tag string) ([]string, error) { } return commitMessages, nil } + +func getShortMessage(message string) string { + var short string + if strings.Contains(message, "\n") { + short = strings.Split(message, "\n")[0] + } else { + short = message + } + return strings.TrimSpace(short) +} diff --git a/vcs/operations_test.go b/vcs/operations_test.go new file mode 100644 index 0000000..38f4469 --- /dev/null +++ b/vcs/operations_test.go @@ -0,0 +1,36 @@ +package vcs + +import "testing" + +func Test_getShortMessage(t *testing.T) { + type args struct { + message string + } + tests := []struct { + name string + args args + want string + }{ + { + name: "short message", + args: args{ + message: "long message\nwith multiple lines", + }, + want: "long message", + }, + { + name: "remove trailing newlines", + args: args{ + message: "commit message\n\n", + }, + want: "commit message", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := getShortMessage(tt.args.message); got != tt.want { + t.Errorf("getShortMessage() = %v, want %v", got, tt.want) + } + }) + } +}