Skip to content

Commit

Permalink
fix: fix git args in the git diff command
Browse files Browse the repository at this point in the history
  • Loading branch information
joselitofilho committed Apr 6, 2024
1 parent 1ac0eee commit dcbe1a8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/go-conventional-commits/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func main() {

// Running git command to get logs
git := gitlog.New(&gitlog.Config{Path: *repoPath})
commits, err := git.Log(&gitargs.GitLogArgs{LatestVersion: *latestVersion}, nil)
commits, err := git.Log(gitargs.NewGitLogArgs(*latestVersion, *newVersion), nil)
if err != nil {
log.Fatalln(err)
}
Expand Down
20 changes: 17 additions & 3 deletions internal/gitargs/gitargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,30 @@ package gitargs

import "fmt"

const Head = "HEAD"

// GitLogArgs a struct to help us to build a git log command arguments
type GitLogArgs struct {
LatestVersion string
latestVersion string
currentVersion string
}

func NewGitLogArgs(latestVersion, currentVersion string) *GitLogArgs {
return &GitLogArgs{latestVersion: latestVersion, currentVersion: currentVersion}
}

// Args return a list of git log command arguments to get logs from the latest version
func (a *GitLogArgs) Args() []string {
args := []string{"-s"}
if a.LatestVersion != "" {
args = append(args, fmt.Sprintf("%s..HEAD", a.LatestVersion))

if a.latestVersion != "" {
currVer := a.currentVersion
if currVer == "" {
currVer = Head
}

args = append(args, fmt.Sprintf("%s..%s", a.latestVersion, currVer))
}

return args
}
29 changes: 19 additions & 10 deletions internal/gitargs/gitargs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,36 @@ import (

func TestArgs(t *testing.T) {
tests := []struct {
name string
latestVersion string
expected []string
name string
latestVersion string
currentVersion string
expected []string
}{
{
name: "with latest version",
latestVersion: "v0.1.2",
expected: []string{"-s", "v0.1.2..HEAD"},
name: "with latest version",
latestVersion: "v0.1.2",
currentVersion: "",
expected: []string{"-s", "v0.1.2..HEAD"},
},
{
name: "without latest version",
latestVersion: "",
expected: []string{"-s"},
name: "with latest and current versions",
latestVersion: "v0.1.2",
currentVersion: "v0.2.0",
expected: []string{"-s", "v0.1.2..v0.2.0"},
},
{
name: "without latest version",
latestVersion: "",
currentVersion: "",
expected: []string{"-s"},
},
}

for i := range tests {
tc := tests[i]

t.Run(tc.name, func(t *testing.T) {
logArgs := gitargs.GitLogArgs{LatestVersion: tc.latestVersion}
logArgs := gitargs.NewGitLogArgs(tc.latestVersion, tc.currentVersion)
actual := logArgs.Args()
require.Equal(t, tc.expected, actual)
})
Expand Down

0 comments on commit dcbe1a8

Please sign in to comment.