Skip to content

Commit e675c75

Browse files
committed
feat: start and end flags
1 parent e5800e8 commit e675c75

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

gitutils.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,33 @@ func openRepository(path string) *git.Repository {
106106

107107
return r
108108
}
109+
110+
// GetCommitFromString - get commit from hash string
111+
func GetCommitFromString(commitString string, repo *git.Repository) *object.Commit {
112+
baseCommitReference, err := repo.Head()
113+
if err != nil {
114+
log.Fatal("Unable to get Repo head:", err)
115+
}
116+
cIter, err := repo.Log(&git.LogOptions{From: baseCommitReference.Hash()})
117+
if err != nil {
118+
log.Fatal("Unable to get commits:", err)
119+
}
120+
foundMatch := false
121+
var commitRef *object.Commit
122+
123+
for {
124+
if foundMatch {
125+
break
126+
}
127+
128+
commit, _ := cIter.Next()
129+
130+
if commit.Hash.String() == commitString {
131+
foundMatch = true
132+
commitRef = commit
133+
}
134+
135+
}
136+
137+
return commitRef
138+
}

main.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
package main
44

55
import (
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

2121
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")
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

Comments
 (0)