@@ -5,74 +5,42 @@ import (
5
5
"fmt"
6
6
"strings"
7
7
8
- "github.com/go-git/go-git/v5"
9
- "github.com/go-git/go-git/v5/plumbing"
10
-
11
8
"github.com/spenserblack/git-lazy-commit/pkg/fileutils"
12
9
)
13
10
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
-
27
11
// 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 ()
30
14
if err != nil {
31
15
return "" , err
32
16
}
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 )
36
23
}
37
24
}
38
25
39
- if len (status ) == 0 {
26
+ if len (commitableStatuses ) == 0 {
40
27
return "" , errors .New ("no tracked files" )
41
28
}
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
- }
49
29
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
65
33
}
66
34
67
- return fmt . Sprintf ( "%s %s" , statusString , filename )
35
+ return multiFileMsg ( commitableStatuses ), nil
68
36
}
69
37
70
- func multiFileMsg (status git.Status ) string {
38
+ // MultiFileMsg builds a commit message from multiple files.
39
+ func multiFileMsg (statuses []StatusRecord ) string {
71
40
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 )
76
44
}
77
45
78
46
sharedDir := fileutils .SharedDirectory (filenames )
@@ -84,9 +52,8 @@ func multiFileMsg(status git.Status) string {
84
52
}
85
53
builder .WriteRune ('\n' )
86
54
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 ))
90
57
}
91
58
92
59
return builder .String ()
0 commit comments