Skip to content

Commit

Permalink
fix auto bump problem (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
walteh authored May 27, 2024
1 parent 9e1c74b commit 454c600
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 25 deletions.
20 changes: 20 additions & 0 deletions calculate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,26 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
MergeTags: []string{"v0.18.1"},
},
},

{
name: "new bugfix branch",
calculation: &simver.Calculation{
ForcePatch: false,
IsMerged: true,
MostRecentLiveTag: "",
MyMostRecentBuild: 0.000000,
MyMostRecentTag: "",
NextValidTag: "v0.3.0",
PR: 1.000000,
Skip: false,
},
output: &simver.CalculationOutput{
BaseTags: []string{},
HeadTags: []string{},
RootTags: []string{},
MergeTags: []string{"v0.18.1"},
},
},
}

ctx := context.Background()
Expand Down
56 changes: 33 additions & 23 deletions cli/zerolog.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"os"
"reflect"
"runtime"
"runtime/debug"
"strings"
Expand Down Expand Up @@ -43,44 +44,53 @@ func DefaultLogger(opts *DefaultLoggerOpts) *zerolog.Logger {

consoleOutput := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.StampMicro, NoColor: false}

consoleOutput.FormatMessage = func(i interface{}) string {
if i == nil {
return "nil"
}

return color.New(color.FgHiWhite).Sprintf("%s", i.(string))
}

pretty := pp.New()

pretty.SetColorScheme(pp.ColorScheme{})

prettyerr := pp.New()
prettyerr.SetExportedOnly(false)

consoleOutput.FormatFieldName = func(i interface{}) string {
return color.New(color.Faint).Sprintf("%s", i) + color.New(color.FgHiGreen).Sprint("=")
}

consoleOutput.FormatFieldValue = func(i interface{}) string {
consoleOutput.FormatFieldValue = func(i any) string {

switch i := i.(type) {
switch t := i.(type) {
case error:
return prettyerr.Sprint(i)
return t.Error()
case []byte:
var g interface{}
err := json.Unmarshal(i, &g)
var g any
err := json.Unmarshal(t, &g)
if err != nil {
return pretty.Sprint(string(i))
return pretty.Sprint(string(t))
} else {
return fmt.Sprintf("[unmarshaled json of byte array] %s", pretty.Sprint(g))
return pretty.Sprint(g)
}
case string:
return color.New(color.Bold).Sprint(i)
}

return pretty.Sprint(i)
}
switch reflect.TypeOf(i).Kind() {
case reflect.Struct:
return pretty.Sprint(i)
case reflect.Map:
return pretty.Sprint(i)
case reflect.Slice:
return pretty.Sprint(i)
case reflect.Array:
return pretty.Sprint(i)
case reflect.Ptr:
return pretty.Sprint(i)
case reflect.Interface:
return pretty.Sprint(i)
case reflect.Func:
return pretty.Sprint(i)
case reflect.Chan:
return pretty.Sprint(i)
case reflect.UnsafePointer:
return pretty.Sprint(i)
case reflect.Uintptr:
return pretty.Sprint(i)
}

return fmt.Sprintf("%v", i)
}
consoleOutput.FormatLevel = func(i interface{}) string {
switch i := i.(string); i {
case zerolog.LevelDebugValue:
Expand Down
126 changes: 126 additions & 0 deletions execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,132 @@ func TestNewTags(t *testing.T) {
isTargetingRoot: true,
expectedTags: simver.Tags{},
},

// debug 23:06:39.070385 gitexec:tag.go:110 > got tags from branch branch=main dur="2.544345" tags=[unmarshaled json of byte array] []interface {}{
// map[string]interface {}{
// "Name": "v0.2.0-pr1+base",
// "Ref": "70380fda303a70c9f2cabd94b8dc49c73dbb1507",
// },
// map[string]interface {}{
// "Name": "v0.2.0-reserved",
// "Ref": "70380fda303a70c9f2cabd94b8dc49c73dbb1507",
// },
// } tags_len="2"
// debug 23:06:39.070559 gitexec:tag.go:114 > got tags from branch branch=main dur="2.712849" tags=[unmarshaled json of byte array] []interface {}{
// map[string]interface {}{
// "Name": "v0.2.0-pr1+base",
// "Ref": "70380fda303a70c9f2cabd94b8dc49c73dbb1507",
// },
// map[string]interface {}{
// "Name": "v0.2.0-reserved",
// "Ref": "70380fda303a70c9f2cabd94b8dc49c73dbb1507",
// },
// } tags_len="2"
// debug 23:06:39.070818 gitexec:tag.go:24 > getting tags from commit commit=e83d0d624a3b32f524cd1c27db10b9089fba5479
// debug 23:06:39.070848 gitexec:git.go:114 > building git command args=[unmarshaled json of byte array] []interface {}{
// "tag",
// "--points-at",
// "e83d0d624a3b32f524cd1c27db10b9089fba5479",
// } commit=e83d0d624a3b32f524cd1c27db10b9089fba5479 executable=git
// debug 23:06:39.072875 gitexec:tag.go:46 > got tags from commit commit=e83d0d624a3b32f524cd1c27db10b9089fba5479 tags=[unmarshaled json of byte array] nil tags_len="0"
// debug 23:06:39.072913 gitexec:tag.go:57 > getting tags from branch branch=main
// debug 23:06:39.072942 gitexec:git.go:114 > building git command args=[unmarshaled json of byte array] []interface {}{
// "tag",
// "--merged",
// "origin/main",
// "--format='{\"sha\":\"%(objectname)\",\"type\": \"%(objecttype)\", \"ref\": \"%(refname)\"}'",
// } branch=main executable=git
// debug 23:06:39.075177 gitexec:tag.go:110 > got tags from branch branch=main dur="2.259715" tags=[unmarshaled json of byte array] []interface {}{
// map[string]interface {}{
// "Name": "v0.2.0-pr1+base",
// "Ref": "70380fda303a70c9f2cabd94b8dc49c73dbb1507",
// },
// map[string]interface {}{
// "Name": "v0.2.0-reserved",
// "Ref": "70380fda303a70c9f2cabd94b8dc49c73dbb1507",
// },
// } tags_len="2"
// debug 23:06:39.075361 gitexec:tag.go:114 > got tags from branch branch=main dur="2.436455" tags=[unmarshaled json of byte array] []interface {}{
// map[string]interface {}{
// "Name": "v0.2.0-pr1+base",
// "Ref": "70380fda303a70c9f2cabd94b8dc49c73dbb1507",
// },
// map[string]interface {}{
// "Name": "v0.2.0-reserved",
// "Ref": "70380fda303a70c9f2cabd94b8dc49c73dbb1507",
// },
// } tags_len="2"
// debug 23:06:39.075518 gitexec:tag.go:24 > getting tags from commit commit=e83d0d624a3b32f524cd1c27db10b9089fba5479
// debug 23:06:39.075542 gitexec:git.go:114 > building git command args=[unmarshaled json of byte array] []interface {}{
// "tag",
// "--points-at",
// "e83d0d624a3b32f524cd1c27db10b9089fba5479",
// } commit=e83d0d624a3b32f524cd1c27db10b9089fba5479 executable=git
// debug 23:06:39.078151 gitexec:tag.go:46 > got tags from commit commit=e83d0d624a3b32f524cd1c27db10b9089fba5479 tags=[unmarshaled json of byte array] nil tags_len="0"
// debug 23:06:39.078261 github.com/walteh/simver:pr_state.go:133 > loaded tags CurrentBaseBranchTags=[unmarshaled json of byte array] []interface {}{
// "7038...1507 => v0.2.0-pr1+base",
// "7038...1507 => v0.2.0-reserved",
// } CurrentBaseCommitTags=[unmarshaled json of byte array] []interface {}{
// "7038...1507 => v0.2.0-pr1+base",
// "7038...1507 => v0.2.0-reserved",
// } CurrentHeadBranchTags=[unmarshaled json of byte array] []interface {}{} CurrentHeadCommitTags=[unmarshaled json of byte array] []interface {}{} CurrentRootBranchTags=[unmarshaled json of byte array] []interface {}{
// "7038...1507 => v0.2.0-pr1+base",
// "7038...1507 => v0.2.0-reserved",
// } CurrentRootCommitTags=[unmarshaled json of byte array] []interface {}{} IsTargetingRoot=[unmarshaled json of byte array] true
// debug 23:06:39.078556 github.com/walteh/simver:execution.go:258 > calculated next valid tag maj=v0. majmin=v0.2 max=v0.2.0 min=2 minornum="3" patch=0 patchnum="0"
// debug 23:06:39.078665 github.com/walteh/simver:calculate.go:134 > CalculateNewTagsRaw calculation=[unmarshaled json of byte array] map[string]interface {}{
// "ForcePatch": false,
// "IsMerged": true,
// "MostRecentLiveTag": "",
// "MyMostRecentBuild": 0.000000,
// "MyMostRecentTag": "",
// "NextValidTag": "v0.3.0",
// "PR": 1.000000,
// "Skip": false,
// } forcePatch=[unmarshaled json of byte array] false isMerge=[unmarshaled json of byte array] true mmrt=v0.3.0 mrlt=v0.1.0 nvt=v0.3.0 output=[unmarshaled json of byte array] map[string]interface {}{
// "BaseTags": []interface {}{},
// "HeadTags": []interface {}{},
// "MergeTags": []interface {}{
// "v0.3.0",
// },
// "RootTags": []interface {}{},
// } pr=1

{
name: "when merging a branch that already is tagged correctly, on head, do nothing",
baseBranchTags: simver.Tags{
// simver.Tag{Name: "v0.2.0-pr1+1"},
// simver.Tag{Name: "v0.2.0"},
simver.Tag{Name: "v0.2.0-pr1+base"},
simver.Tag{Name: "v0.2.0-reserved"},
// simver.Tag{Name: "v0.3.0"},
},
headBranchTags: simver.Tags{
// simver.Tag{Name: "v0.2.0-pr1+1"},
// simver.Tag{Name: "v0.2.0"},
// simver.Tag{Name: "v0.3.0-pr1+base"},
// simver.Tag{Name: "v0.3.0-reserved"},
simver.Tag{Name: "v0.2.0-pr1+base"},
simver.Tag{Name: "v0.2.0-reserved"},
// simver.Tag{Name: "v0.3.0"},
},
headCommitTags: simver.Tags{
// simver.Tag{Name: "v0.3.0-reserved"},
// simver.Tag{Name: "v0.2.0-pr1+base"},
// simver.Tag{Name: "v0.2.0-reserved"},
// simver.Tag{Name: "v0.3.0"},
},
rootBranchTags: simver.Tags{
simver.Tag{Name: "v0.2.0-pr1+base"},
simver.Tag{Name: "v0.2.0-reserved"},
},
pr: 1,
isMerge: true,
isTargetingRoot: true,
expectedTags: simver.Tags{
simver.Tag{Name: "v0.2.0", Ref: merge_ref},
},
},
}

ctx := context.Background()
Expand Down
2 changes: 0 additions & 2 deletions gitexec/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ func (p *gitProvider) TagsFromBranch(ctx context.Context, branch string) (simver

// tags = tags.ExtractCommitRefs()

zerolog.Ctx(ctx).Debug().Int("tags_len", len(tags)).Any("tags", tags).Dur("dur", time.Since(start)).Msg("got tags from branch")

return tags, nil

}
Expand Down
7 changes: 7 additions & 0 deletions tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ var _ zerolog.LogArrayMarshaler = (*Tags)(nil)

type Tags []Tag

func (t Tags) Copy() Tags {
tags := make(Tags, len(t))
copy(tags, t)

return tags
}

func shortRef(ref string) string {
if len(ref) <= 11 {
return ref
Expand Down

0 comments on commit 454c600

Please sign in to comment.