diff --git a/cmd/pkgdashcli/main.go b/cmd/pkgdashcli/main.go index 3e20bfe..ae20e6f 100644 --- a/cmd/pkgdashcli/main.go +++ b/cmd/pkgdashcli/main.go @@ -30,7 +30,7 @@ import ( "go.unistack.org/micro/v4/logger" "go.unistack.org/micro/v4/options" "golang.org/x/mod/modfile" - + "golang.org/x/mod/semver" "gopkg.in/yaml.v2" ) @@ -147,19 +147,28 @@ func main() { mvs := make(map[string]modules.Update) updateOptions := modules.UpdateOptions{ - Pre: false, - Major: false, - Cached: true, + Pre: false, + Major: false, + UpMajor: false, + Cached: true, OnUpdate: func(u modules.Update) { + var modpath string // new mod path with major if u.Err != nil { logger.Errorf(ctx, "%s: failed: %v", u.Module.Path, u.Err) return } - v, ok := modules.ModMajor(u.Module.Path) - if ok && !strings.Contains(u.Version, v) { - u.Module.Path = u.Module.Path[:len(u.Module.Path)-2] + v + modpath = u.Module.Path + v := semver.Major(u.Version) + p := modules.ModPrefix(modpath) + if !strings.HasPrefix(u.Module.Version, v) && v != "v1" && v != "v0" { + switch strings.HasPrefix(u.Module.Path, "gopkg.in") { + case true: + modpath = p + "." + v + case false: + modpath = p + "/" + v + } } - mvs[u.Module.Path] = u + mvs[modpath] = u }, } diff --git a/go.mod b/go.mod index 9070f54..19c2b0f 100644 --- a/go.mod +++ b/go.mod @@ -31,7 +31,7 @@ require ( ) require ( - dario.cat/mergo v1.0.0 // indirect + dario.cat/mergo v1.0.0 github.com/Microsoft/go-winio v0.6.1 // indirect github.com/ProtonMail/go-crypto v0.0.0-20230717121422-5aa5874ade95 // indirect github.com/VictoriaMetrics/metrics v1.24.0 // indirect diff --git a/internal/modules/modproxy.go b/internal/modules/modproxy.go index 6a8423d..2b50f18 100644 --- a/internal/modules/modproxy.go +++ b/internal/modules/modproxy.go @@ -250,7 +250,8 @@ type UpdateOptions struct { Modules []module.Version Pre bool Cached bool - Major bool + Major bool // Major true compare only major + UpMajor bool // UpMajor module up with major } // Updates finds updates for a set of specified modules. @@ -276,13 +277,13 @@ func Updates(opt UpdateOptions) { ch <- Update{Module: m, Err: err} return nil } - prefix, ok := ModMajor(mod.Path) + major := semver.Major(m.Version) var v string - switch ok { + switch opt.UpMajor { case true: - v = mod.MaxVersion(prefix, opt.Pre) - default: v = mod.MaxVersion("", opt.Pre) + case false: + v = mod.MaxVersion(major, opt.Pre) } if IsNewerVersion(m.Version, v, opt.Major) { ch <- Update{Module: m, Version: v} diff --git a/internal/modules/packages_test.go b/internal/modules/packages_test.go new file mode 100644 index 0000000..a2ec779 --- /dev/null +++ b/internal/modules/packages_test.go @@ -0,0 +1,55 @@ +package modules + +import "testing" + +func TestModMajor(t *testing.T) { + type args struct { + modpath string + } + var tests = []struct { + name string + args args + want string + want1 bool + }{ + {"Test #1", + args{ + "github.com/jackc/chunkreader/v2", + }, + "v2", + true, + }, + {"Test #2", + args{ + "github.com/jackc/chunkreader", + }, + "", + true, + }, + {"Test #3", + args{ + "gopkg.in/yaml.v2", + }, + "v2", + true, + }, + {"Test #4", + args{ + "github.com/jackc/chunkreader/v1", + }, + "", + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, got1 := ModMajor(tt.args.modpath) + if got != tt.want { + t.Errorf("ModMajor() got = %v, want %v", got, tt.want) + } + if got1 != tt.want1 { + t.Errorf("ModMajor() got1 = %v, want %v", got1, tt.want1) + } + }) + } +} diff --git a/internal/source/gitea/gitea.go b/internal/source/gitea/gitea.go index 32613d3..494cb79 100644 --- a/internal/source/gitea/gitea.go +++ b/internal/source/gitea/gitea.go @@ -210,6 +210,11 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod var cmd *exec.Cmd var out []byte + cmd = exec.CommandContext(ctx, epath, "mod", "edit", fmt.Sprintf("-droprequire=%s", mod.Module.Path)) + if out, err = cmd.CombinedOutput(); err != nil { + logger.Fatalf(ctx, "failed to run go mod edit: %s err: %v", out, err) + } + cmd = exec.CommandContext(ctx, epath, "mod", "edit", fmt.Sprintf("-require=%s@%s", path, mod.Version)) if out, err = cmd.CombinedOutput(); err != nil { logger.Fatalf(ctx, "failed to run go mod edit: %s err: %v", out, err)