33package main
44
55import (
6+ "flag"
67 "fmt"
78 "log"
8- "os"
99 "strings"
1010
1111 "github.com/go-git/go-git/v5"
@@ -19,32 +19,46 @@ type ErrMessage struct {
1919}
2020
2121func 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" )
2225
23- if len (os .Args ) < 2 {
24- fmt .Println ("Usage:\n > commitlog path/to/repo" )
25- os .Exit (0 )
26- }
26+ flag .Parse ()
2727
28- path := os . Args [ 1 ]
28+ path := repoPath
2929
30- err := CommitLog (path )
30+ err := CommitLog (* path , * startCommit , * endCommit )
3131
3232 if err .err != nil {
3333 log .Fatal (err .message , err .err )
3434 }
3535}
3636
3737// CommitLog - Generate commit log
38- func CommitLog (path string ) ErrMessage {
38+ func CommitLog (path string , startCommitString string , endCommitString string ) ErrMessage {
3939 currentRepository := openRepository (path )
4040
4141 baseCommitReference , err := currentRepository .Head ()
42+ var startHash , endHash * object.Commit
43+ var cIter object.CommitIter
4244
4345 if err != nil {
4446 return ErrMessage {"Unable to get repository HEAD:" , err }
4547 }
4648
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+ }
4862
4963 if err != nil {
5064 return ErrMessage {"Unable to get repository commits:" , err }
@@ -83,7 +97,10 @@ func CommitLog(path string) ErrMessage {
8397 default :
8498 logContainer .OTHER = append (logContainer .OTHER , normalizedHash )
8599 }
86- if isCommitToNearestTag (currentRepository , c ) {
100+
101+ if endHash == nil && isCommitToNearestTag (currentRepository , c ) {
102+ break
103+ } else if endHash != nil && c .Hash == endHash .Hash {
87104 break
88105 }
89106 }
0 commit comments