Skip to content

Commit b513e53

Browse files
committed
feat: generator subcommand implementation
implement the base set of functionality for the generator command TODO: should improve the naming of the internal structs
1 parent b78e067 commit b513e53

File tree

5 files changed

+441
-7
lines changed

5 files changed

+441
-7
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
CHANGELOG.md
22
./commitlog
33
.DS_Store
4-
dist
4+
dist
5+
commitlog

commands/commitlog.go

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,70 @@
11
package commands
22

3-
import "github.com/urfave/cli/v2"
3+
import (
4+
"github.com/barelyhuman/commitlog/pkg"
5+
"github.com/urfave/cli/v2"
6+
)
47

58
func Commitlog(c *cli.Context) (err error) {
6-
return
9+
path := c.String("path")
10+
addPromo := c.Bool("promo")
11+
out := c.String("out")
12+
stdio := c.Bool("stdio")
13+
startRef := c.String("start")
14+
endRef := c.String("end")
15+
categories := c.String("categories")
16+
17+
gOptions := []pkg.GeneratorConfigMod{}
18+
19+
if addPromo {
20+
gOptions = append(gOptions,
21+
pkg.WithPromo(),
22+
)
23+
}
24+
25+
// either write to a file or to the stdio with true by default
26+
if len(out) > 0 {
27+
gOptions = append(gOptions,
28+
pkg.WithOutputToFile(out),
29+
)
30+
} else if stdio {
31+
gOptions = append(gOptions,
32+
pkg.WithOutputToStdio(),
33+
)
34+
}
35+
36+
if len(startRef) > 0 {
37+
gOptions = append(gOptions,
38+
pkg.WithStartReference(startRef),
39+
)
40+
}
41+
42+
if len(endRef) > 0 {
43+
gOptions = append(gOptions,
44+
pkg.WithEndReference(endRef),
45+
)
46+
}
47+
48+
if len(categories) > 0 {
49+
gOptions = append(gOptions,
50+
pkg.WithCategories(categories),
51+
)
52+
}
53+
54+
generator := pkg.CreateGenerator(path,
55+
gOptions...)
56+
57+
err = generator.ReadCommmits()
58+
59+
if err != nil {
60+
return err
61+
}
62+
63+
err = generator.Classify()
64+
65+
if err != nil {
66+
return err
67+
}
68+
69+
return generator.Generate()
770
}

lib/commits.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package lib
2+
3+
import (
4+
"log"
5+
"strings"
6+
7+
"github.com/go-git/go-git/v5"
8+
"github.com/go-git/go-git/v5/plumbing"
9+
"github.com/go-git/go-git/v5/plumbing/object"
10+
)
11+
12+
func IsHashATag(currentRepository *git.Repository, hash plumbing.Hash) bool {
13+
isTag := false
14+
tags, _ := currentRepository.Tags()
15+
tags.ForEach(func(tagRef *plumbing.Reference) error {
16+
revHash, err := currentRepository.ResolveRevision(plumbing.Revision(tagRef.Name()))
17+
if err != nil {
18+
return err
19+
}
20+
if *revHash == hash {
21+
isTag = true
22+
}
23+
return nil
24+
})
25+
return isTag
26+
}
27+
28+
func GetCommitFromString(repo *git.Repository, commitString string) *object.Commit {
29+
if commitString == "" {
30+
return nil
31+
}
32+
33+
hash, err := repo.ResolveRevision(plumbing.Revision(commitString))
34+
if err != nil {
35+
log.Fatal("Unable to get Repo head:", err)
36+
}
37+
38+
commitRef, err := repo.CommitObject(*hash)
39+
if err != nil {
40+
log.Fatal("Unable to get resolve commit:", err)
41+
}
42+
return commitRef
43+
}
44+
45+
func CommitToLog(c *object.Commit) string {
46+
var commitMsg strings.Builder
47+
commitMsg.WriteString(c.Hash.String())
48+
commitMsg.WriteString(" ")
49+
commitMsg.WriteString(strings.Split(c.Message, "\n")[0])
50+
return commitMsg.String()
51+
}

main.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,40 @@ func main() {
3333
return commands.Commitlog(c)
3434
},
3535
Flags: []cli.Flag{
36+
&cli.StringFlag{
37+
Name: "path",
38+
Value: ".",
39+
Aliases: []string{"p"},
40+
Usage: "root with the '.git' folder `PATH`",
41+
},
3642
&cli.BoolFlag{
3743
Name: "promo",
3844
Usage: "add promo text to the end of output",
3945
},
4046
&cli.StringFlag{
41-
Name: "out",
42-
Usage: "path to the output `FILE`",
47+
Name: "out",
48+
Aliases: []string{"o"},
49+
Usage: "path to the output `FILE`",
4350
},
4451
&cli.BoolFlag{
4552
Name: "stdio",
53+
Value: true,
4654
Usage: "print to the stdout",
4755
},
4856
&cli.StringFlag{
49-
Name: "start",
57+
Name: "categories",
58+
Value: "",
59+
Usage: "categories to use, includes all commits by default. any text you add here will be used to create categories out of the commits",
60+
},
61+
&cli.StringFlag{
62+
Name: "start",
63+
Aliases: []string{"s"},
5064
Usage: "`START` reference for the commit to include commits from," +
5165
"This is inclusive of the given commit reference",
5266
},
5367
&cli.StringFlag{
54-
Name: "end",
68+
Name: "end",
69+
Aliases: []string{"e"},
5570
Usage: "`END` reference for the commit to stop including commits at." +
5671
"This is exclusive of the given commit reference",
5772
},

0 commit comments

Comments
 (0)