Skip to content

Commit

Permalink
Merge pull request #90 from bvieira/#84
Browse files Browse the repository at this point in the history
Feature: support empty overwrite for tag.filter and tag.pattern
  • Loading branch information
bvieira authored Jan 22, 2023
2 parents 4fec01e + 52e2fe5 commit 192daa5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
6 changes: 4 additions & 2 deletions cmd/git-sv/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func readConfig(filepath string) (Config, error) {

func defaultConfig() Config {
skipDetached := false
pattern := "%d.%d.%d"
filter := ""
return Config{
Version: "1.1",
Versioning: sv.VersioningConfig{
Expand All @@ -78,8 +80,8 @@ func defaultConfig() Config {
IgnoreUnknown: false,
},
Tag: sv.TagConfig{
Pattern: "%d.%d.%d",
Filter: "",
Pattern: &pattern,
Filter: &filter,
},
ReleaseNotes: sv.ReleaseNotesConfig{
Sections: []sv.ReleaseNotesSectionConfig{
Expand Down
4 changes: 4 additions & 0 deletions cmd/git-sv/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
func Test_merge(t *testing.T) {
boolFalse := false
boolTrue := true
emptyStr := ""
nonEmptyStr := "something"

tests := []struct {
name string
Expand All @@ -34,6 +36,8 @@ func Test_merge(t *testing.T) {
{"merge empty maps", Config{CommitMessage: sv.CommitMessageConfig{Footer: map[string]sv.CommitMessageFooterConfig{"issue": {Key: "jira"}}}}, Config{CommitMessage: sv.CommitMessageConfig{Footer: map[string]sv.CommitMessageFooterConfig{}}}, Config{CommitMessage: sv.CommitMessageConfig{Footer: map[string]sv.CommitMessageFooterConfig{"issue": {Key: "jira"}}}}, false},

{"overwrite release notes header", Config{ReleaseNotes: sv.ReleaseNotesConfig{Headers: map[string]string{"a": "aa"}}}, Config{ReleaseNotes: sv.ReleaseNotesConfig{Headers: map[string]string{"b": "bb"}}}, Config{ReleaseNotes: sv.ReleaseNotesConfig{Headers: map[string]string{"b": "bb"}}}, false},

{"overwrite tag config", Config{Version: "a", Tag: sv.TagConfig{Pattern: &nonEmptyStr, Filter: &nonEmptyStr}}, Config{Version: "", Tag: sv.TagConfig{Pattern: &emptyStr, Filter: &emptyStr}}, Config{Version: "a", Tag: sv.TagConfig{Pattern: &emptyStr, Filter: &emptyStr}}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/git-sv/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func getTags(git sv.Git, tag string) (string, sv.GitTag, error) {

index := find(tag, tags)
if index < 0 {
return "", sv.GitTag{}, fmt.Errorf("tag: %s not found", tag)
return "", sv.GitTag{}, fmt.Errorf("tag: %s not found, check tag filter", tag)
}

previousTag := ""
Expand Down
14 changes: 7 additions & 7 deletions sv/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ package sv

// CommitMessageConfig config a commit message.
type CommitMessageConfig struct {
Types []string `yaml:"types,flow"`
HeaderSelector string `yaml:"header-selector"`
Scope CommitMessageScopeConfig `yaml:"scope"`
Footer map[string]CommitMessageFooterConfig `yaml:"footer"`
Issue CommitMessageIssueConfig `yaml:"issue"`
Types []string `yaml:"types,flow"`
HeaderSelector string `yaml:"header-selector"`
Scope CommitMessageScopeConfig `yaml:"scope"`
Footer map[string]CommitMessageFooterConfig `yaml:"footer"`
Issue CommitMessageIssueConfig `yaml:"issue"`
}

// IssueFooterConfig config for issue.
Expand Down Expand Up @@ -62,8 +62,8 @@ type VersioningConfig struct {

// TagConfig tag preferences.
type TagConfig struct {
Pattern string `yaml:"pattern"`
Filter string `yaml:"filter"`
Pattern *string `yaml:"pattern"`
Filter *string `yaml:"filter"`
}

// ==== Release Notes ====
Expand Down
6 changes: 3 additions & 3 deletions sv/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func NewGit(messageProcessor MessageProcessor, cfg TagConfig) *GitImpl {

// LastTag get last tag, if no tag found, return empty.
func (g GitImpl) LastTag() string {
cmd := exec.Command("git", "for-each-ref", "refs/tags/"+g.tagCfg.Filter, "--sort", "-creatordate", "--format", "%(refname:short)", "--count", "1")
cmd := exec.Command("git", "for-each-ref", "refs/tags/"+*g.tagCfg.Filter, "--sort", "-creatordate", "--format", "%(refname:short)", "--count", "1")
out, err := cmd.CombinedOutput()
if err != nil {
return ""
Expand Down Expand Up @@ -131,7 +131,7 @@ func (g GitImpl) Commit(header, body, footer string) error {

// Tag create a git tag.
func (g GitImpl) Tag(version semver.Version) (string, error) {
tag := fmt.Sprintf(g.tagCfg.Pattern, version.Major(), version.Minor(), version.Patch())
tag := fmt.Sprintf(*g.tagCfg.Pattern, version.Major(), version.Minor(), version.Patch())
tagMsg := fmt.Sprintf("Version %d.%d.%d", version.Major(), version.Minor(), version.Patch())

tagCommand := exec.Command("git", "tag", "-a", tag, "-m", tagMsg)
Expand All @@ -148,7 +148,7 @@ func (g GitImpl) Tag(version semver.Version) (string, error) {

// Tags list repository tags.
func (g GitImpl) Tags() ([]GitTag, error) {
cmd := exec.Command("git", "for-each-ref", "--sort", "creatordate", "--format", "%(creatordate:iso8601)#%(refname:short)", "refs/tags/"+g.tagCfg.Filter)
cmd := exec.Command("git", "for-each-ref", "--sort", "creatordate", "--format", "%(creatordate:iso8601)#%(refname:short)", "refs/tags/"+*g.tagCfg.Filter)
out, err := cmd.CombinedOutput()
if err != nil {
return nil, combinedOutputErr(err, out)
Expand Down

0 comments on commit 192daa5

Please sign in to comment.