Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions coauthors.go → coauthors/coauthors.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package main
package coauthors

import (
"bufio"
"fmt"
"github.com/remotemobprogramming/mob/v5/say"
"os"
"path"
"regexp"
"sort"
"strings"

"github.com/remotemobprogramming/mob/v5/say"
)

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

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

coauthors = removeElementsContaining(coauthors, gitUserEmail())
coauthors = removeElementsContaining(coauthors, currentUserEmail)
say.Debug("Parsed coauthors without committer")
say.Debug(strings.Join(coauthors, ","))

Expand Down Expand Up @@ -93,7 +94,7 @@ func removeDuplicateValues(slice []string) []string {
return result
}

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

// read from repo/.git/SQUASH_MSG
coauthors := collectCoauthorsFromWipCommits(file)
coauthors := collectCoauthorsFromWipCommits(file, currentUserEmail)

if len(coauthors) > 0 {
coauthorSuffix := createCommitMessage(coauthors)
Expand Down
42 changes: 42 additions & 0 deletions coauthors/coauthors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package coauthors

import (
"reflect"
"testing"
)

func TestCreateCommitMessage(t *testing.T) {
expected := `

# automatically added all co-authors from WIP commits
# add missing co-authors manually
Co-authored-by: Alice <alice@example.com>
Co-authored-by: Bob <bob@example.com>
`
actual := createCommitMessage([]Author{"Alice <alice@example.com>", "Bob <bob@example.com>"})
if actual != expected {
t.Errorf("expected %q, got %q", expected, actual)
}
}

func TestSortByLength(t *testing.T) {
slice := []string{"aa", "b"}

sortByLength(slice)

expected := []string{"b", "aa"}
if !reflect.DeepEqual(expected, slice) {
t.Errorf("expected %v, got %v", expected, slice)
}
}

func TestRemoveDuplicateValues(t *testing.T) {
slice := []string{"aa", "b", "c", "b"}

actual := removeDuplicateValues(slice)

expected := []string{"aa", "b", "c"}
if !reflect.DeepEqual(expected, actual) {
t.Errorf("expected %v, got %v", expected, actual)
}
}
26 changes: 0 additions & 26 deletions coauthors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,3 @@ func TestStartDoneCoAuthors(t *testing.T) {
// include everyone else in commit order after removing duplicates
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")
}

func TestCreateCommitMessage(t *testing.T) {
equals(t, `

# automatically added all co-authors from WIP commits
# add missing co-authors manually
Co-authored-by: Alice <alice@example.com>
Co-authored-by: Bob <bob@example.com>
`, createCommitMessage([]Author{"Alice <alice@example.com>", "Bob <bob@example.com>"}))
}

func TestSortByLength(t *testing.T) {
slice := []string{"aa", "b"}

sortByLength(slice)

equals(t, []string{"b", "aa"}, slice)
}

func TestRemoveDuplicateValues(t *testing.T) {
slice := []string{"aa", "b", "c", "b"}

actual := removeDuplicateValues(slice)

equals(t, []string{"aa", "b", "c"}, actual)
}
3 changes: 2 additions & 1 deletion mob.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"
"time"

"github.com/remotemobprogramming/mob/v5/coauthors"
config "github.com/remotemobprogramming/mob/v5/configuration"
"github.com/remotemobprogramming/mob/v5/findnext"
"github.com/remotemobprogramming/mob/v5/goal"
Expand Down Expand Up @@ -993,7 +994,7 @@ func done(configuration config.Configuration) {
if hasCachedChanges {
say.InfoIndented(cachedChanges)
}
err := appendCoauthorsToSquashMsg(gitDir())
err := coauthors.AppendCoauthorsToSquashMsg(gitDir(), gitUserEmail())
if err != nil {
say.Warning(err.Error())
}
Expand Down
Loading
Loading