Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allows for multiple directory options #188

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ func DescribeTag(tagMode string, pattern string) (string, error) {
return "", fmt.Errorf("no tags match '%s'", pattern)
}

func Changelog(tag string, dir string) (string, error) {
func Changelog(tag string, dirs ...string) (string, error) {
if tag == "" {
return gitLog(dir, "HEAD")
return gitLog(dirs, "HEAD")
} else {
return gitLog(dir, fmt.Sprintf("tags/%s..HEAD", tag))
return gitLog(dirs, fmt.Sprintf("tags/%s..HEAD", tag))
}
}

Expand All @@ -81,11 +81,14 @@ func run(args ...string) (string, error) {
return string(bts), nil
}

func gitLog(dir string, refs ...string) (string, error) {
func gitLog(dirs []string, refs ...string) (string, error) {
args := []string{"log", "--no-decorate", "--no-color"}
args = append(args, refs...)
if dir != "" {
args = append(args, "--", dir)
// dirs was changed from string to []string, so we handle the case where a single empty string is passed for
// backwards compatibility
if len(dirs) > 1 || (len(dirs) == 1 && dirs[0] != "") {
args = append(args, "--")
args = append(args, dirs...)
}
return run(args...)
}
61 changes: 61 additions & 0 deletions internal/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,67 @@ func TestChangelogWithDirectory(t *testing.T) {
is.True(!strings.Contains(log, "feat: foobar"))
}

func TestChangelogWithMultipleDirectories(t *testing.T) {
const (
featCommitMsg = "feat: new feature"
fixCommitMsg = "fix: some fix"
choreCommitMsg = "chore: foobar"
)

tempDir := tempdir(t)

is := is.New(t)

addFileToDir := func(dir string) {
err := os.Mkdir(path.Join(tempDir, dir), 0755)
is.NoErr(err)
file := tempfile(t, dir)
gitAdd(t, file)
}

// Setup git with chore commit at root, a feat commit in dir1, and a fix commit in dir2
gitInit(t)
gitCommit(t, "initial commit")
gitTag(t, "v1.2.3")
gitCommit(t, choreCommitMsg)

dir1 := "dir1"
addFileToDir(dir1)
gitCommit(t, featCommitMsg)

dir2 := "dir2"
addFileToDir(dir2)
gitCommit(t, fixCommitMsg)

// Test only dir1
log1, err := Changelog("v1.2.3", dir1)
is.NoErr(err)
is.True(strings.Contains(log1, featCommitMsg))
is.True(!strings.Contains(log1, fixCommitMsg))
is.True(!strings.Contains(log1, choreCommitMsg))

// Test only dir2
log2, err := Changelog("v1.2.3", dir2)
is.NoErr(err)
is.True(!strings.Contains(log2, featCommitMsg))
is.True(strings.Contains(log2, fixCommitMsg))
is.True(!strings.Contains(log2, choreCommitMsg))

// Test dir1 and dir2
log1and2, err := Changelog("v1.2.3", dir1, dir2)
is.NoErr(err)
is.True(strings.Contains(log1and2, featCommitMsg))
is.True(strings.Contains(log1and2, fixCommitMsg))
is.True(!strings.Contains(log1, choreCommitMsg))

// Regression test empty string
logAll, err := Changelog("v1.2.3", "")
is.NoErr(err)
is.True(strings.Contains(logAll, choreCommitMsg))
is.True(strings.Contains(logAll, featCommitMsg))
is.True(strings.Contains(logAll, fixCommitMsg))
}

func switchToBranch(tb testing.TB, branch string) {
is := is.New(tb)
_, err := fakeGitRun("switch", branch)
Expand Down
12 changes: 6 additions & 6 deletions internal/svu/svu.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Options struct {
StripPrefix bool
PreRelease string
Build string
Directory string
Directories []string
TagMode string
ForcePatchIncrement bool
PreventMajorIncrementOnV0 bool
Expand All @@ -56,7 +56,7 @@ func Version(opts Options) (string, error) {
tag,
opts.PreRelease,
opts.Build,
opts.Directory,
opts.Directories,
opts.PreventMajorIncrementOnV0,
opts.ForcePatchIncrement,
)
Expand All @@ -70,7 +70,7 @@ func Version(opts Options) (string, error) {
return opts.Prefix + result.String(), nil
}

func nextVersion(cmd string, current *semver.Version, tag, preRelease, build, directory string, preventMajorIncrementOnV0, forcePatchIncrement bool) (semver.Version, error) {
func nextVersion(cmd string, current *semver.Version, tag, preRelease, build string, directories []string, preventMajorIncrementOnV0, forcePatchIncrement bool) (semver.Version, error) {
if cmd == CurrentCmd {
return *current, nil
}
Expand All @@ -91,7 +91,7 @@ func nextVersion(cmd string, current *semver.Version, tag, preRelease, build, di
var err error
switch cmd {
case NextCmd, PreReleaseCmd:
result, err = findNextWithGitLog(current, tag, directory, preventMajorIncrementOnV0, forcePatchIncrement)
result, err = findNextWithGitLog(current, tag, directories, preventMajorIncrementOnV0, forcePatchIncrement)
case MajorCmd:
result = current.IncMajor()
case MinorCmd:
Expand Down Expand Up @@ -181,8 +181,8 @@ func getCurrentVersion(tag, prefix string) (*semver.Version, error) {
return current, err
}

func findNextWithGitLog(current *semver.Version, tag string, directory string, preventMajorIncrementOnV0, forcePatchIncrement bool) (semver.Version, error) {
log, err := git.Changelog(tag, directory)
func findNextWithGitLog(current *semver.Version, tag string, directories []string, preventMajorIncrementOnV0, forcePatchIncrement bool) (semver.Version, error) {
log, err := git.Changelog(tag, directories...)
if err != nil {
return semver.Version{}, fmt.Errorf("failed to get changelog: %w", err)
}
Expand Down
40 changes: 20 additions & 20 deletions internal/svu/svu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ func TestCmd(t *testing.T) {
cmd := CurrentCmd
t.Run("version has meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "", "", false, false)
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "", nil, false, false)
is.NoErr(err)
is.Equal("1.2.3-pre+123", v.String())
})
t.Run("version is clean", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, semver.MustParse("v1.2.3"), "v1.2.3", "doesnt matter", "nope", "", false, true)
v, err := nextVersion(cmd, semver.MustParse("v1.2.3"), "v1.2.3", "doesnt matter", "nope", nil, false, true)
is.NoErr(err)
is.Equal("1.2.3", v.String())
})
Expand All @@ -123,25 +123,25 @@ func TestCmd(t *testing.T) {
cmd := MinorCmd
t.Run("no meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "", "", false, false)
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "", nil, false, false)
is.NoErr(err)
is.Equal("1.3.0", v.String())
})
t.Run("build", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "124", "", false, false)
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "124", nil, false, false)
is.NoErr(err)
is.Equal("1.3.0+124", v.String())
})
t.Run("prerel", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "alpha.1", "", "", false, false)
v, err := nextVersion(cmd, ver(), "v1.2.3", "alpha.1", "", nil, false, false)
is.NoErr(err)
is.Equal("1.3.0-alpha.1", v.String())
})
t.Run("all meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "alpha.2", "125", "", false, false)
v, err := nextVersion(cmd, ver(), "v1.2.3", "alpha.2", "125", nil, false, false)
is.NoErr(err)
is.Equal("1.3.0-alpha.2+125", v.String())
})
Expand All @@ -151,49 +151,49 @@ func TestCmd(t *testing.T) {
cmd := PatchCmd
t.Run("no meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, semver.MustParse("1.2.3"), "v1.2.3", "", "", "", false, false)
v, err := nextVersion(cmd, semver.MustParse("1.2.3"), "v1.2.3", "", "", nil, false, false)
is.NoErr(err)
is.Equal("1.2.4", v.String())
})
t.Run("previous had meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, semver.MustParse("1.2.3-alpha.1+1"), "v1.2.3", "", "", "", false, false)
v, err := nextVersion(cmd, semver.MustParse("1.2.3-alpha.1+1"), "v1.2.3", "", "", nil, false, false)
is.NoErr(err)
is.Equal("1.2.3", v.String())
})
t.Run("previous had meta, force", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, semver.MustParse("1.2.3-alpha.1+1"), "v1.2.3", "", "", "", false, true)
v, err := nextVersion(cmd, semver.MustParse("1.2.3-alpha.1+1"), "v1.2.3", "", "", nil, false, true)
is.NoErr(err)
is.Equal("1.2.4", v.String())
})
t.Run("previous had meta, force, add meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, semver.MustParse("1.2.3-alpha.1+1"), "v1.2.3-alpha.1+1", "alpha.2", "10", "", false, true)
v, err := nextVersion(cmd, semver.MustParse("1.2.3-alpha.1+1"), "v1.2.3-alpha.1+1", "alpha.2", "10", nil, false, true)
is.NoErr(err)
is.Equal("1.2.4-alpha.2+10", v.String())
})
t.Run("previous had meta, change it", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, semver.MustParse("1.2.3-alpha.1+1"), "v1.2.3-alpha.1+1", "alpha.2", "10", "", false, false)
v, err := nextVersion(cmd, semver.MustParse("1.2.3-alpha.1+1"), "v1.2.3-alpha.1+1", "alpha.2", "10", nil, false, false)
is.NoErr(err)
is.Equal("1.2.3-alpha.2+10", v.String())
})
t.Run("build", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, semver.MustParse("1.2.3"), "v1.2.3", "", "124", "", false, false)
v, err := nextVersion(cmd, semver.MustParse("1.2.3"), "v1.2.3", "", "124", nil, false, false)
is.NoErr(err)
is.Equal("1.2.4+124", v.String())
})
t.Run("prerel", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, semver.MustParse("1.2.3"), "v1.2.3", "alpha.1", "", "", false, false)
v, err := nextVersion(cmd, semver.MustParse("1.2.3"), "v1.2.3", "alpha.1", "", nil, false, false)
is.NoErr(err)
is.Equal("1.2.4-alpha.1", v.String())
})
t.Run("all meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, semver.MustParse("1.2.3"), "v1.2.3", "alpha.2", "125", "", false, false)
v, err := nextVersion(cmd, semver.MustParse("1.2.3"), "v1.2.3", "alpha.2", "125", nil, false, false)
is.NoErr(err)
is.Equal("1.2.4-alpha.2+125", v.String())
})
Expand All @@ -203,25 +203,25 @@ func TestCmd(t *testing.T) {
cmd := MajorCmd
t.Run("no meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "", "", false, false)
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "", nil, false, false)
is.NoErr(err)
is.Equal("2.0.0", v.String())
})
t.Run("build", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "124", "", false, false)
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "124", nil, false, false)
is.NoErr(err)
is.Equal("2.0.0+124", v.String())
})
t.Run("prerel", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "alpha.1", "", "", false, false)
v, err := nextVersion(cmd, ver(), "v1.2.3", "alpha.1", "", nil, false, false)
is.NoErr(err)
is.Equal("2.0.0-alpha.1", v.String())
})
t.Run("all meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "alpha.2", "125", "", false, false)
v, err := nextVersion(cmd, ver(), "v1.2.3", "alpha.2", "125", nil, false, false)
is.NoErr(err)
is.Equal("2.0.0-alpha.2+125", v.String())
})
Expand All @@ -230,12 +230,12 @@ func TestCmd(t *testing.T) {
t.Run("errors", func(t *testing.T) {
t.Run("invalid build", func(t *testing.T) {
is := is.New(t)
_, err := nextVersion(MinorCmd, semver.MustParse("1.2.3"), "v1.2.3", "", "+125", "", false, false)
_, err := nextVersion(MinorCmd, semver.MustParse("1.2.3"), "v1.2.3", "", "+125", nil, false, false)
is.True(err != nil)
})
t.Run("invalid prerelease", func(t *testing.T) {
is := is.New(t)
_, err := nextVersion(MinorCmd, semver.MustParse("1.2.3"), "v1.2.3", "+aaa", "", "", false, false)
_, err := nextVersion(MinorCmd, semver.MustParse("1.2.3"), "v1.2.3", "+aaa", "", nil, false, false)
is.True(err != nil)
})
})
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
stripPrefix = app.Flag("strip-prefix", "strips the prefix from the tag").Default("false").Bool()
build = app.Flag("build", "adds a build suffix to the version, without the semver mandatory plug prefix").
String()
directory = app.Flag("directory", "specifies directory to filter commit messages by").Default("").String()
directories = app.Flag("directory", "specifies directory to filter commit messages by").Default("").Strings()
tagMode = app.Flag("tag-mode", "determines if latest tag of the current or all branches will be used").
Default("current-branch").
Enum("current-branch", "all-branches")
Expand All @@ -51,7 +51,7 @@ func main() {
StripPrefix: *stripPrefix,
PreRelease: *preRelease,
Build: *build,
Directory: *directory,
Directories: *directories,
TagMode: *tagMode,
ForcePatchIncrement: *forcePatchIncrement,
PreventMajorIncrementOnV0: *preventMajorIncrementOnV0,
Expand Down
2 changes: 1 addition & 1 deletion pkg/svu/svu.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func WithBuild(build string) option {

func WithDirectory(directory string) option {
return func(o *svu.Options) {
o.Directory = directory
o.Directories = append(o.Directories, directory)
}
}

Expand Down