diff options
author | Stefan Haller <stefan@haller-berlin.de> | 2024-03-03 17:23:39 +0100 |
---|---|---|
committer | Stefan Haller <stefan@haller-berlin.de> | 2024-03-11 09:18:40 +0100 |
commit | b8f4cd0ef67c9d7842cc20fd346c8decdb65be2f (patch) | |
tree | eb40da63cd8894890c07fa15e1028a21954c984d /pkg | |
parent | 287195b5b243be74828cd4409e0ec049ce56bc30 (diff) |
Extract functions AddCoAuthorToMessage and AddCoAuthorToDescription
In this commit we only need the former; the latter will be reused later in this
branch.
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/commands/git_commands/commit.go | 23 | ||||
-rw-r--r-- | pkg/commands/git_commands/commit_test.go | 67 | ||||
-rw-r--r-- | pkg/integration/tests/commit/add_co_author.go | 1 |
3 files changed, 89 insertions, 2 deletions
diff --git a/pkg/commands/git_commands/commit.go b/pkg/commands/git_commands/commit.go index 960fab811..400cd7608 100644 --- a/pkg/commands/git_commands/commit.go +++ b/pkg/commands/git_commands/commit.go @@ -39,13 +39,13 @@ func (self *CommitCommands) SetAuthor(value string) error { } // Add a commit's coauthor using Github/Gitlab Co-authored-by metadata. Value is expected to be of the form 'Name <Email>' -func (self *CommitCommands) AddCoAuthor(sha string, value string) error { +func (self *CommitCommands) AddCoAuthor(sha string, author string) error { message, err := self.GetCommitMessage(sha) if err != nil { return err } - message = message + fmt.Sprintf("\nCo-authored-by: %s", value) + message = AddCoAuthorToMessage(message, author) cmdArgs := NewGitCmd("commit"). Arg("--allow-empty", "--amend", "--only", "-m", message). @@ -54,6 +54,25 @@ func (self *CommitCommands) AddCoAuthor(sha string, value string) error { return self.cmd.New(cmdArgs).Run() } +func AddCoAuthorToMessage(message string, author string) string { + subject, body, _ := strings.Cut(message, "\n") + + return strings.TrimSpace(subject) + "\n\n" + AddCoAuthorToDescription(strings.TrimSpace(body), author) +} + +func AddCoAuthorToDescription(description string, author string) string { + if description != "" { + lines := strings.Split(description, "\n") + if strings.HasPrefix(lines[len(lines)-1], "Co-authored-by:") { + description += "\n" + } else { + description += "\n\n" + } + } + + return description + fmt.Sprintf("Co-authored-by: %s", author) +} + // ResetToCommit reset to commit func (self *CommitCommands) ResetToCommit(sha string, strength string, envVars []string) error { cmdArgs := NewGitCmd("reset").Arg("--"+strength, sha).ToArgv() diff --git a/pkg/commands/git_commands/commit_test.go b/pkg/commands/git_commands/commit_test.go index 34b064d67..d6e3397e3 100644 --- a/pkg/commands/git_commands/commit_test.go +++ b/pkg/commands/git_commands/commit_test.go @@ -333,3 +333,70 @@ func TestGetCommitMessageFromHistory(t *testing.T) { }) } } + +func TestAddCoAuthorToMessage(t *testing.T) { + scenarios := []struct { + name string + message string + expectedResult string + }{ + { + // This never happens, I think it isn't possible to create a commit + // with an empty message. Just including it for completeness. + name: "Empty message", + message: "", + expectedResult: "\n\nCo-authored-by: John Doe <john@doe.com>", + }, + { + name: "Just a subject, no body", + message: "Subject", + expectedResult: "Subject\n\nCo-authored-by: John Doe <john@doe.com>", + }, + { + name: "Subject and body", + message: "Subject\n\nBody", + expectedResult: "Subject\n\nBody\n\nCo-authored-by: John Doe <john@doe.com>", + }, + { + name: "Body already ending with a Co-authored-by line", + message: "Subject\n\nBody\n\nCo-authored-by: Jane Smith <jane@smith.com>", + expectedResult: "Subject\n\nBody\n\nCo-authored-by: Jane Smith <jane@smith.com>\nCo-authored-by: John Doe <john@doe.com>", + }, + } + for _, s := range scenarios { + t.Run(s.name, func(t *testing.T) { + result := AddCoAuthorToMessage(s.message, "John Doe <john@doe.com>") + assert.Equal(t, s.expectedResult, result) + }) + } +} + +func TestAddCoAuthorToDescription(t *testing.T) { + scenarios := []struct { + name string + description string + expectedResult string + }{ + { + name: "Empty description", + description: "", + expectedResult: "Co-authored-by: John Doe <john@doe.com>", + }, + { + name: "Non-empty description", + description: "Body", + expectedResult: "Body\n\nCo-authored-by: John Doe <john@doe.com>", + }, + { + name: "Description already ending with a Co-authored-by line", + description: "Body\n\nCo-authored-by: Jane Smith <jane@smith.com>", + expectedResult: "Body\n\nCo-authored-by: Jane Smith <jane@smith.com>\nCo-authored-by: John Doe <john@doe.com>", + }, + } + for _, s := range scenarios { + t.Run(s.name, func(t *testing.T) { + result := AddCoAuthorToDescription(s.description, "John Doe <john@doe.com>") + assert.Equal(t, s.expectedResult, result) + }) + } +} diff --git a/pkg/integration/tests/commit/add_co_author.go b/pkg/integration/tests/commit/add_co_author.go index 46ac0d055..f4c8d2c52 100644 --- a/pkg/integration/tests/commit/add_co_author.go +++ b/pkg/integration/tests/commit/add_co_author.go @@ -34,6 +34,7 @@ var AddCoAuthor = NewIntegrationTest(NewIntegrationTestArgs{ t.Views().Main().ContainsLines( Equals(" initial commit"), + Equals(" "), Equals(" Co-authored-by: John Smith <jsmith@gmail.com>"), ) }, |