3
3
package main
4
4
5
5
import (
6
+ "flag"
6
7
"fmt"
7
8
"log"
8
- "os"
9
9
"strings"
10
10
11
11
"github.com/go-git/go-git/v5"
@@ -19,32 +19,46 @@ type ErrMessage struct {
19
19
}
20
20
21
21
func main () {
22
+ var repoPath = flag .String ("p" , "." , "path to the repository, points to the current working directory by default" )
23
+ var startCommit = flag .String ("s" , "" , "commit hash string start collecting commit messages from" )
24
+ var endCommit = flag .String ("e" , "" , "commit hash string to stop collecting commit message at" )
22
25
23
- if len (os .Args ) < 2 {
24
- fmt .Println ("Usage:\n > commitlog path/to/repo" )
25
- os .Exit (0 )
26
- }
26
+ flag .Parse ()
27
27
28
- path := os . Args [ 1 ]
28
+ path := repoPath
29
29
30
- err := CommitLog (path )
30
+ err := CommitLog (* path , * startCommit , * endCommit )
31
31
32
32
if err .err != nil {
33
33
log .Fatal (err .message , err .err )
34
34
}
35
35
}
36
36
37
37
// CommitLog - Generate commit log
38
- func CommitLog (path string ) ErrMessage {
38
+ func CommitLog (path string , startCommitString string , endCommitString string ) ErrMessage {
39
39
currentRepository := openRepository (path )
40
40
41
41
baseCommitReference , err := currentRepository .Head ()
42
+ var startHash , endHash * object.Commit
43
+ var cIter object.CommitIter
42
44
43
45
if err != nil {
44
46
return ErrMessage {"Unable to get repository HEAD:" , err }
45
47
}
46
48
47
- cIter , err := currentRepository .Log (& git.LogOptions {From : baseCommitReference .Hash ()})
49
+ if startCommitString != "" {
50
+ startHash = GetCommitFromString (startCommitString , currentRepository )
51
+ }
52
+
53
+ if endCommitString != "" {
54
+ endHash = GetCommitFromString (endCommitString , currentRepository )
55
+ }
56
+
57
+ if startHash != nil {
58
+ cIter , err = currentRepository .Log (& git.LogOptions {From : startHash .Hash })
59
+ } else {
60
+ cIter , err = currentRepository .Log (& git.LogOptions {From : baseCommitReference .Hash ()})
61
+ }
48
62
49
63
if err != nil {
50
64
return ErrMessage {"Unable to get repository commits:" , err }
@@ -83,7 +97,10 @@ func CommitLog(path string) ErrMessage {
83
97
default :
84
98
logContainer .OTHER = append (logContainer .OTHER , normalizedHash )
85
99
}
86
- if isCommitToNearestTag (currentRepository , c ) {
100
+
101
+ if endHash == nil && isCommitToNearestTag (currentRepository , c ) {
102
+ break
103
+ } else if endHash != nil && c .Hash == endHash .Hash {
87
104
break
88
105
}
89
106
}
0 commit comments