Skip to content

Commit 167bbd3

Browse files
committed
Extract functions AddCoAuthorToMessage and AddCoAuthorToDescription
In this commit we only need the former; the latter will be reused later in this branch.
1 parent c5e88f7 commit 167bbd3

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

pkg/commands/git_commands/commit.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ func (self *CommitCommands) SetAuthor(value string) error {
3939
}
4040

4141
// Add a commit's coauthor using Github/Gitlab Co-authored-by metadata. Value is expected to be of the form 'Name <Email>'
42-
func (self *CommitCommands) AddCoAuthor(sha string, value string) error {
42+
func (self *CommitCommands) AddCoAuthor(sha string, author string) error {
4343
message, err := self.GetCommitMessage(sha)
4444
if err != nil {
4545
return err
4646
}
4747

48-
message = message + fmt.Sprintf("\nCo-authored-by: %s", value)
48+
message = AddCoAuthorToMessage(message, author)
4949

5050
cmdArgs := NewGitCmd("commit").
5151
Arg("--allow-empty", "--amend", "--only", "-m", message).
@@ -54,6 +54,25 @@ func (self *CommitCommands) AddCoAuthor(sha string, value string) error {
5454
return self.cmd.New(cmdArgs).Run()
5555
}
5656

57+
func AddCoAuthorToMessage(message string, author string) string {
58+
subject, body, _ := strings.Cut(message, "\n")
59+
60+
return strings.TrimSpace(subject) + "\n\n" + AddCoAuthorToDescription(strings.TrimSpace(body), author)
61+
}
62+
63+
func AddCoAuthorToDescription(description string, author string) string {
64+
if description != "" {
65+
lines := strings.Split(description, "\n")
66+
if strings.HasPrefix(lines[len(lines)-1], "Co-authored-by:") {
67+
description += "\n"
68+
} else {
69+
description += "\n\n"
70+
}
71+
}
72+
73+
return description + fmt.Sprintf("Co-authored-by: %s", author)
74+
}
75+
5776
// ResetToCommit reset to commit
5877
func (self *CommitCommands) ResetToCommit(sha string, strength string, envVars []string) error {
5978
cmdArgs := NewGitCmd("reset").Arg("--"+strength, sha).ToArgv()

pkg/commands/git_commands/commit_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,70 @@ func TestGetCommitMessageFromHistory(t *testing.T) {
333333
})
334334
}
335335
}
336+
337+
func TestAddCoAuthorToMessage(t *testing.T) {
338+
scenarios := []struct {
339+
name string
340+
message string
341+
expectedResult string
342+
}{
343+
{
344+
// This never happens, I think it isn't possible to create a commit
345+
// with an empty message. Just including it for completeness.
346+
name: "Empty message",
347+
message: "",
348+
expectedResult: "\n\nCo-authored-by: John Doe <[email protected]>",
349+
},
350+
{
351+
name: "Just a subject, no body",
352+
message: "Subject",
353+
expectedResult: "Subject\n\nCo-authored-by: John Doe <[email protected]>",
354+
},
355+
{
356+
name: "Subject and body",
357+
message: "Subject\n\nBody",
358+
expectedResult: "Subject\n\nBody\n\nCo-authored-by: John Doe <[email protected]>",
359+
},
360+
{
361+
name: "Body already ending with a Co-authored-by line",
362+
message: "Subject\n\nBody\n\nCo-authored-by: Jane Smith <[email protected]>",
363+
expectedResult: "Subject\n\nBody\n\nCo-authored-by: Jane Smith <[email protected]>\nCo-authored-by: John Doe <[email protected]>",
364+
},
365+
}
366+
for _, s := range scenarios {
367+
t.Run(s.name, func(t *testing.T) {
368+
result := AddCoAuthorToMessage(s.message, "John Doe <[email protected]>")
369+
assert.Equal(t, s.expectedResult, result)
370+
})
371+
}
372+
}
373+
374+
func TestAddCoAuthorToDescription(t *testing.T) {
375+
scenarios := []struct {
376+
name string
377+
description string
378+
expectedResult string
379+
}{
380+
{
381+
name: "Empty description",
382+
description: "",
383+
expectedResult: "Co-authored-by: John Doe <[email protected]>",
384+
},
385+
{
386+
name: "Non-empty description",
387+
description: "Body",
388+
expectedResult: "Body\n\nCo-authored-by: John Doe <[email protected]>",
389+
},
390+
{
391+
name: "Description already ending with a Co-authored-by line",
392+
description: "Body\n\nCo-authored-by: Jane Smith <[email protected]>",
393+
expectedResult: "Body\n\nCo-authored-by: Jane Smith <[email protected]>\nCo-authored-by: John Doe <[email protected]>",
394+
},
395+
}
396+
for _, s := range scenarios {
397+
t.Run(s.name, func(t *testing.T) {
398+
result := AddCoAuthorToDescription(s.description, "John Doe <[email protected]>")
399+
assert.Equal(t, s.expectedResult, result)
400+
})
401+
}
402+
}

pkg/integration/tests/commit/add_co_author.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var AddCoAuthor = NewIntegrationTest(NewIntegrationTestArgs{
3434

3535
t.Views().Main().ContainsLines(
3636
Equals(" initial commit"),
37+
Equals(" "),
3738
Equals(" Co-authored-by: John Smith <[email protected]>"),
3839
)
3940
},

0 commit comments

Comments
 (0)