Skip to content

Commit b4a3ee4

Browse files
authored
Refactor CommitMsg
1 parent 16342ef commit b4a3ee4

File tree

3 files changed

+79
-85
lines changed

3 files changed

+79
-85
lines changed

commit.go

Lines changed: 20 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,74 +5,42 @@ import (
55
"fmt"
66
"strings"
77

8-
"github.com/go-git/go-git/v5"
9-
"github.com/go-git/go-git/v5/plumbing"
10-
118
"github.com/spenserblack/git-lazy-commit/pkg/fileutils"
129
)
1310

14-
// Commit commits all changes in the repository.
15-
//
16-
// It returns the commit hash and the commit message.
17-
func (r *LazyRepo) Commit() (hash plumbing.Hash, msg string, err error) {
18-
msg, err = r.CommitMsg()
19-
if err != nil {
20-
return
21-
}
22-
23-
hash, err = r.wt.Commit(msg, &git.CommitOptions{})
24-
return
25-
}
26-
2711
// CommitMsg builds a commit message using the tracked files in the repository.
28-
func (r *LazyRepo) CommitMsg() (string, error) {
29-
status, err := r.status()
12+
func (r Repo) CommitMsg() (string, error) {
13+
statuses, err := r.Status()
3014
if err != nil {
3115
return "", err
3216
}
33-
for filename, fileStatus := range status {
34-
if fileStatus.Staging == git.Unmodified || fileStatus.Staging == git.Untracked {
35-
delete(status, filename)
17+
18+
// NOTE: Filtering to only statuses that are staged and can be used for the commit message.
19+
commitableStatuses := make([]StatusRecord, 0, len(statuses))
20+
for _, status := range statuses {
21+
if _, ok := statusMap[status.Staged]; ok {
22+
commitableStatuses = append(commitableStatuses, status)
3623
}
3724
}
3825

39-
if len(status) == 0 {
26+
if len(commitableStatuses) == 0 {
4027
return "", errors.New("no tracked files")
4128
}
42-
if len(status) == 1 {
43-
for filename, fileStatus := range status {
44-
return singleFileMsg(filename, fileStatus), nil
45-
}
46-
}
47-
return multiFileMsg(status), nil
48-
}
4929

50-
func singleFileMsg(filename string, fileStatus *git.FileStatus) string {
51-
statusString := ""
52-
switch fileStatus.Staging {
53-
case git.Added:
54-
statusString = "Create"
55-
case git.Deleted:
56-
statusString = "Delete"
57-
case git.Modified:
58-
statusString = "Update"
59-
case git.Renamed:
60-
statusString = "Rename to"
61-
case git.Copied:
62-
statusString = "Copy to"
63-
default:
64-
statusString = "Do something to"
30+
if len(commitableStatuses) == 1 {
31+
status := commitableStatuses[0]
32+
return fmt.Sprintf("%s %s", statusMap[status.Staged], status.Path), nil
6533
}
6634

67-
return fmt.Sprintf("%s %s", statusString, filename)
35+
return multiFileMsg(commitableStatuses), nil
6836
}
6937

70-
func multiFileMsg(status git.Status) string {
38+
// MultiFileMsg builds a commit message from multiple files.
39+
func multiFileMsg(statuses []StatusRecord) string {
7140
var builder strings.Builder
72-
73-
filenames := make([]string, 0, len(status))
74-
for name := range status {
75-
filenames = append(filenames, name)
41+
filenames := make([]string, 0, len(statuses))
42+
for _, status := range statuses {
43+
filenames = append(filenames, status.Path)
7644
}
7745

7846
sharedDir := fileutils.SharedDirectory(filenames)
@@ -84,9 +52,8 @@ func multiFileMsg(status git.Status) string {
8452
}
8553
builder.WriteRune('\n')
8654

87-
for filename, fileStatus := range status {
88-
msgItem := singleFileMsg(filename, fileStatus)
89-
builder.WriteString(fmt.Sprintf("- %s\n", msgItem))
55+
for _, status := range statuses {
56+
builder.WriteString(fmt.Sprintf("- %s %s\n", statusMap[status.Staged], status.Path))
9057
}
9158

9259
return builder.String()

commit_test.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,53 @@ package lazycommit
33
import (
44
"strings"
55
"testing"
6-
7-
gitconfig "github.com/go-git/go-git/v5/config"
86
)
97

108
// Tests that a commit message can't be built when there are no staged changes.
11-
func TestBuildCommitMessageNoStaged(t *testing.T) {
9+
func TestBuildCommitMessage(t *testing.T) {
10+
t.Log("Creating a new repo.")
1211
dir := tempRepo(t)
13-
repo, err := OpenRepo(dir)
12+
repo := Repo(dir)
13+
14+
_, err := repo.CommitMsg()
15+
if err == nil && err.Error() != "no tracked files" {
16+
t.Errorf(`Expected "no tracked files", got %v`, err)
17+
}
18+
19+
f := commitFile(t, dir, "test.txt", "test")
20+
defer f.Close()
21+
22+
t.Log(`Modifying test.txt`)
23+
commitFile(t, dir, "test.txt", "")
24+
addFile(t, dir, "test.txt", "different text")
25+
26+
msg, err := repo.CommitMsg()
1427
if err != nil {
1528
t.Fatal(err)
1629
}
17-
_, err = repo.CommitMsg()
18-
if err == nil {
19-
t.Fatal("expected error")
30+
if msg != "Update test.txt" {
31+
t.Errorf(`Expected "Update test.txt", got %v`, msg)
2032
}
21-
}
2233

23-
// Tests that commit commits all files in the worktree.
24-
func TestCommit(t *testing.T) {
25-
dir := tempRepo(t)
26-
updateConfig(t, dir, func(config *gitconfig.Config) {
27-
config.User.Name = "Test User"
28-
config.User.Email = "test@example.com"
29-
})
30-
addFile(t, dir, "test.txt", "test")
34+
t.Log(`Adding a new file`)
3135
addFile(t, dir, "test2.txt", "test")
3236

33-
repo, err := OpenRepo(dir)
37+
msg, err = repo.CommitMsg()
3438
if err != nil {
3539
t.Fatal(err)
3640
}
37-
38-
_, msg, err := repo.Commit()
39-
if err != nil {
40-
t.Fatal(err)
41+
lines := strings.Split(msg, "\n")
42+
if lines[0] != "Update files" {
43+
t.Errorf(`Expected "Update files" in the header, got %v`, lines[0])
4144
}
42-
43-
wantHeader := "Update files"
44-
wantBodyLines := []string{"- Create test.txt", "- Create test2.txt"}
45-
46-
if !strings.HasPrefix(msg, wantHeader) {
47-
t.Errorf("expected commit message to start with %q, got %q", wantHeader, msg)
45+
if lines[1] != "" {
46+
t.Errorf(`Expected an empty line after the header, got %v`, lines[1])
4847
}
49-
50-
for _, line := range wantBodyLines {
51-
if !strings.Contains(msg, line) {
52-
t.Errorf("expected commit message to contain %q, got %q", line, msg)
48+
body := strings.Join(lines[2:], "\n")
49+
t.Logf("Body:\n %v", body)
50+
for _, want := range []string{"- Update test.txt", "- Create test2.txt"} {
51+
if !strings.Contains(body, want) {
52+
t.Errorf(`Expected %v in the body`, want)
5353
}
5454
}
5555
}

deprecated.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
21
package lazycommit
32

3+
import (
4+
"errors"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/go-git/go-git/v5"
9+
"github.com/go-git/go-git/v5/plumbing"
10+
"github.com/spenserblack/git-lazy-commit/pkg/fileutils"
11+
)
12+
413
// LazyRepo is a wrapper around go-git's Repository for simpler usage.
514
//
615
// DEPRECATED: Use Repo instead.
@@ -25,3 +34,21 @@ func (r *LazyRepo) NoStaged() (bool, error) {
2534
func (r *LazyRepo) StageAll() error {
2635
return r.r.StageAll()
2736
}
37+
38+
// Commit commits all changes in the repository.
39+
//
40+
// It returns the commit hash and the commit message.
41+
func (r *LazyRepo) Commit() (hash plumbing.Hash, msg string, err error) {
42+
msg, err = r.CommitMsg()
43+
if err != nil {
44+
return
45+
}
46+
47+
hash, err = r.wt.Commit(msg, &git.CommitOptions{})
48+
return
49+
}
50+
51+
// CommitMsg builds a commit message using the tracked files in the repository.
52+
func (r *LazyRepo) CommitMsg() (string, error) {
53+
return r.r.CommitMsg()
54+
}

0 commit comments

Comments
 (0)