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 ' -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 ", + }, + { + name: "Just a subject, no body", + message: "Subject", + expectedResult: "Subject\n\nCo-authored-by: John Doe ", + }, + { + name: "Subject and body", + message: "Subject\n\nBody", + expectedResult: "Subject\n\nBody\n\nCo-authored-by: John Doe ", + }, + { + name: "Body already ending with a Co-authored-by line", + message: "Subject\n\nBody\n\nCo-authored-by: Jane Smith ", + expectedResult: "Subject\n\nBody\n\nCo-authored-by: Jane Smith \nCo-authored-by: John Doe ", + }, + } + for _, s := range scenarios { + t.Run(s.name, func(t *testing.T) { + result := AddCoAuthorToMessage(s.message, "John Doe ") + 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 ", + }, + { + name: "Non-empty description", + description: "Body", + expectedResult: "Body\n\nCo-authored-by: John Doe ", + }, + { + name: "Description already ending with a Co-authored-by line", + description: "Body\n\nCo-authored-by: Jane Smith ", + expectedResult: "Body\n\nCo-authored-by: Jane Smith \nCo-authored-by: John Doe ", + }, + } + for _, s := range scenarios { + t.Run(s.name, func(t *testing.T) { + result := AddCoAuthorToDescription(s.description, "John Doe ") + 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 "), ) },