@@ -8,40 +8,46 @@ import (
8
8
"os"
9
9
"strings"
10
10
11
- "github.com/barelyhuman/commitlog/logcategory"
12
- "github.com/barelyhuman/commitlog/utils"
13
11
"github.com/go-git/go-git/v5"
14
12
"github.com/go-git/go-git/v5/plumbing/object"
15
13
)
16
14
17
- func normalizeCommit (commitMessage string ) string {
18
- var message string
19
- for i , msg := range strings .Split (commitMessage , "\n " ) {
20
- if i == 0 {
21
- message = msg
22
- break
23
- }
24
- }
25
- return strings .TrimSuffix (message , "\n " )
15
+ // ErrMessage - simple interface around error with a custom message
16
+ type ErrMessage struct {
17
+ message string
18
+ err error
26
19
}
27
20
28
21
func main () {
22
+
23
+ if len (os .Args ) < 2 {
24
+ fmt .Println ("Usage:\n > commitlog path/to/repo" )
25
+ os .Exit (0 )
26
+ }
27
+
29
28
path := os .Args [1 ]
30
- r , err := git .PlainOpen (path )
31
- if err != nil {
32
- log .Fatal ("Error opening Repository: " , err )
29
+
30
+ err := CommitLog (path )
31
+
32
+ if err .err != nil {
33
+ log .Fatal (err .message , err .err )
33
34
}
35
+ }
34
36
35
- ref , err := r .Head ()
37
+ // CommitLog - Generate commit log
38
+ func CommitLog (path string ) ErrMessage {
39
+ currentRepository := openRepository (path )
40
+
41
+ baseCommitReference , err := currentRepository .Head ()
36
42
37
43
if err != nil {
38
- log . Fatal ( "Unable to get repository HEAD:" , err )
44
+ return ErrMessage { "Unable to get repository HEAD:" , err }
39
45
}
40
46
41
- cIter , err := r .Log (& git.LogOptions {From : ref .Hash ()})
47
+ cIter , err := currentRepository .Log (& git.LogOptions {From : baseCommitReference .Hash ()})
42
48
43
49
if err != nil {
44
- log . Fatal ( "Unable to get repository commits:" , err )
50
+ return ErrMessage { "Unable to get repository commits:" , err }
45
51
}
46
52
47
53
var commits []* object.Commit
@@ -52,80 +58,32 @@ func main() {
52
58
})
53
59
54
60
if err != nil {
55
- log .Fatal ("Error getting commits : " , err )
56
- }
57
-
58
- logContainer := new (logcategory.LogsByCategory )
59
- latestTag , _ , err := utils .GetLatestTagFromRepository (r )
60
-
61
- if err != nil {
62
- log .Fatal ("Error Getting Tag Pairs" , err )
61
+ return ErrMessage {"Error getting commits : " , err }
63
62
}
64
63
65
- tillLatest := false
66
-
67
- if latestTag != nil {
68
- if latestTag .Hash ().String () == ref .Hash ().String () {
69
- tillLatest = false
70
- } else {
71
- tillLatest = true
72
- }
73
- }
64
+ logContainer := logsByCategory {}
74
65
75
66
for _ , c := range commits {
76
- switch {
77
- case strings .Contains (c .Message , "ci:" ):
78
- {
79
- logContainer .CI = append (logContainer .CI , c .Hash .String ()+ " - " + normalizeCommit (c .Message ))
80
- }
81
- case strings .Contains (c .Message , "fix:" ):
82
- {
83
- logContainer .FIX = append (logContainer .FIX , c .Hash .String ()+ " - " + normalizeCommit (c .Message ))
84
- }
85
- case strings .Contains (c .Message , "refactor:" ):
86
- {
87
- logContainer .REFACTOR = append (logContainer .REFACTOR , c .Hash .String ()+ " - " + normalizeCommit (c .Message ))
88
- }
89
- case strings .Contains (c .Message , "feat:" ):
90
- {
91
- logContainer .FEATURE = append (logContainer .FEATURE , c .Hash .String ()+ " - " + normalizeCommit (c .Message ))
92
- }
93
- case strings .Contains (c .Message , "feature:" ):
94
- {
95
- logContainer .FEATURE = append (logContainer .FEATURE , c .Hash .String ()+ " - " + normalizeCommit (c .Message ))
96
- }
97
- case strings .Contains (c .Message , "docs:" ):
98
- {
99
- logContainer .DOCS = append (logContainer .DOCS , c .Hash .String ()+ " - " + normalizeCommit (c .Message ))
100
- }
67
+ normalizedHash := c .Hash .String () + " - " + normalizeCommit (c .Message )
68
+ switch strings .SplitN (strings .TrimSpace (c .Message ), ":" , 2 )[0 ] {
69
+ case "ci" :
70
+ logContainer .CI = append (logContainer .CI , normalizedHash )
71
+ case "fix" :
72
+ logContainer .FIX = append (logContainer .FIX , normalizedHash )
73
+ case "refactor" :
74
+ logContainer .REFACTOR = append (logContainer .REFACTOR , normalizedHash )
75
+ case "feat" , "feature" :
76
+ logContainer .FEATURE = append (logContainer .FEATURE , normalizedHash )
77
+ case "docs" :
78
+ logContainer .DOCS = append (logContainer .DOCS , normalizedHash )
101
79
default :
102
- {
103
- logContainer .OTHER = append (logContainer .OTHER , c .Hash .String ()+ " - " + normalizeCommit (c .Message ))
104
- }
80
+ logContainer .OTHER = append (logContainer .OTHER , normalizedHash )
105
81
}
106
-
107
- if isCommitToNearestTag (r , c , tillLatest ) {
82
+ if isCommitToNearestTag (currentRepository , c ) {
108
83
break
109
84
}
110
85
}
86
+ fmt .Println (logContainer .ToMarkdown ())
111
87
112
- fmt .Println (logContainer .GenerateMarkdown ())
113
-
114
- }
115
-
116
- func isCommitToNearestTag (repo * git.Repository , commit * object.Commit , tillLatest bool ) bool {
117
- latestTag , previousTag , err := utils .GetLatestTagFromRepository (repo )
118
-
119
- if err != nil {
120
- log .Fatal ("Couldn't get latest tag..." , err )
121
- }
122
-
123
- if latestTag != nil {
124
- if tillLatest {
125
- return latestTag .Hash ().String () == commit .Hash .String ()
126
- }
127
- return previousTag .Hash ().String () == commit .Hash .String ()
128
-
129
- }
130
- return false
88
+ return ErrMessage {}
131
89
}
0 commit comments