Skip to content

Commit 679e434

Browse files
authored
Extract coauthors package from main
1 parent f7bae34 commit 679e434

5 files changed

Lines changed: 366 additions & 44 deletions

File tree

coauthors.go renamed to coauthors/coauthors.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
package main
1+
package coauthors
22

33
import (
44
"bufio"
55
"fmt"
6-
"github.com/remotemobprogramming/mob/v5/say"
76
"os"
87
"path"
98
"regexp"
109
"sort"
1110
"strings"
11+
12+
"github.com/remotemobprogramming/mob/v5/say"
1213
)
1314

1415
// Author is a coauthor "Full Name <email>"
1516
type Author = string
1617

17-
func collectCoauthorsFromWipCommits(file *os.File) []Author {
18+
func collectCoauthorsFromWipCommits(file *os.File, currentUserEmail string) []Author {
1819
// Here we parse the SQUASH_MSG file for the list of authors on
1920
// the WIP branch. If this technique later turns out to be
2021
// problematic, an alternative would be to instead fetch the
@@ -28,7 +29,7 @@ func collectCoauthorsFromWipCommits(file *os.File) []Author {
2829
say.Debug("Parsed coauthors")
2930
say.Debug(strings.Join(coauthors, ","))
3031

31-
coauthors = removeElementsContaining(coauthors, gitUserEmail())
32+
coauthors = removeElementsContaining(coauthors, currentUserEmail)
3233
say.Debug("Parsed coauthors without committer")
3334
say.Debug(strings.Join(coauthors, ","))
3435

@@ -93,7 +94,7 @@ func removeDuplicateValues(slice []string) []string {
9394
return result
9495
}
9596

96-
func appendCoauthorsToSquashMsg(gitDir string) error {
97+
func AppendCoauthorsToSquashMsg(gitDir string, currentUserEmail string) error {
9798
squashMsgPath := path.Join(gitDir, "SQUASH_MSG")
9899
say.Debug("opening " + squashMsgPath)
99100
file, err := os.OpenFile(squashMsgPath, os.O_APPEND|os.O_RDWR, 0644)
@@ -109,7 +110,7 @@ func appendCoauthorsToSquashMsg(gitDir string) error {
109110
defer file.Close()
110111

111112
// read from repo/.git/SQUASH_MSG
112-
coauthors := collectCoauthorsFromWipCommits(file)
113+
coauthors := collectCoauthorsFromWipCommits(file, currentUserEmail)
113114

114115
if len(coauthors) > 0 {
115116
coauthorSuffix := createCommitMessage(coauthors)

coauthors/coauthors_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package coauthors
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestCreateCommitMessage(t *testing.T) {
9+
expected := `
10+
11+
# automatically added all co-authors from WIP commits
12+
# add missing co-authors manually
13+
Co-authored-by: Alice <alice@example.com>
14+
Co-authored-by: Bob <bob@example.com>
15+
`
16+
actual := createCommitMessage([]Author{"Alice <alice@example.com>", "Bob <bob@example.com>"})
17+
if actual != expected {
18+
t.Errorf("expected %q, got %q", expected, actual)
19+
}
20+
}
21+
22+
func TestSortByLength(t *testing.T) {
23+
slice := []string{"aa", "b"}
24+
25+
sortByLength(slice)
26+
27+
expected := []string{"b", "aa"}
28+
if !reflect.DeepEqual(expected, slice) {
29+
t.Errorf("expected %v, got %v", expected, slice)
30+
}
31+
}
32+
33+
func TestRemoveDuplicateValues(t *testing.T) {
34+
slice := []string{"aa", "b", "c", "b"}
35+
36+
actual := removeDuplicateValues(slice)
37+
38+
expected := []string{"aa", "b", "c"}
39+
if !reflect.DeepEqual(expected, actual) {
40+
t.Errorf("expected %v, got %v", expected, actual)
41+
}
42+
}

coauthors_test.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,3 @@ func TestStartDoneCoAuthors(t *testing.T) {
4444
// include everyone else in commit order after removing duplicates
4545
assertOutputContains(t, &output, "\nCo-authored-by: bob <bob@example.com>\nCo-authored-by: alice <alice@example.com>\nCo-authored-by: localother <localother@example.com>\n")
4646
}
47-
48-
func TestCreateCommitMessage(t *testing.T) {
49-
equals(t, `
50-
51-
# automatically added all co-authors from WIP commits
52-
# add missing co-authors manually
53-
Co-authored-by: Alice <alice@example.com>
54-
Co-authored-by: Bob <bob@example.com>
55-
`, createCommitMessage([]Author{"Alice <alice@example.com>", "Bob <bob@example.com>"}))
56-
}
57-
58-
func TestSortByLength(t *testing.T) {
59-
slice := []string{"aa", "b"}
60-
61-
sortByLength(slice)
62-
63-
equals(t, []string{"b", "aa"}, slice)
64-
}
65-
66-
func TestRemoveDuplicateValues(t *testing.T) {
67-
slice := []string{"aa", "b", "c", "b"}
68-
69-
actual := removeDuplicateValues(slice)
70-
71-
equals(t, []string{"aa", "b", "c"}, actual)
72-
}

mob.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strings"
1515
"time"
1616

17+
"github.com/remotemobprogramming/mob/v5/coauthors"
1718
config "github.com/remotemobprogramming/mob/v5/configuration"
1819
"github.com/remotemobprogramming/mob/v5/findnext"
1920
"github.com/remotemobprogramming/mob/v5/goal"
@@ -993,7 +994,7 @@ func done(configuration config.Configuration) {
993994
if hasCachedChanges {
994995
say.InfoIndented(cachedChanges)
995996
}
996-
err := appendCoauthorsToSquashMsg(gitDir())
997+
err := coauthors.AppendCoauthorsToSquashMsg(gitDir(), gitUserEmail())
997998
if err != nil {
998999
say.Warning(err.Error())
9991000
}

0 commit comments

Comments
 (0)