From 7a56b571104aa1a9213beed68f0c6fb08563e2cf Mon Sep 17 00:00:00 2001 From: xuthus5 Date: Sat, 22 Jul 2023 16:35:59 +0800 Subject: [PATCH] fix: github repo no version --- git-ls-remote.go | 10 +++++++++- go.mod | 4 ++-- go.sum | 9 +++++---- gomod.go | 13 +++++++++---- gomod_test.go | 7 +++++++ 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/git-ls-remote.go b/git-ls-remote.go index f6a8b01..8edef1c 100644 --- a/git-ls-remote.go +++ b/git-ls-remote.go @@ -5,6 +5,8 @@ import ( "os/exec" "regexp" "strings" + + "github.com/sirupsen/logrus" ) var ( @@ -20,6 +22,11 @@ type _lsRemote struct { } func (lr *_lsRemote) setUrl(url string) *_lsRemote { + url = strings.ReplaceAll(url, "https://", "") + url = strings.ReplaceAll(url, "http://", "") + if v := strings.Split(url, "/"); len(v) > 3 { + url = strings.Join(v[:3], "/") + } if !strings.HasPrefix(url, "https") { url = "https://" + url } @@ -28,9 +35,10 @@ func (lr *_lsRemote) setUrl(url string) *_lsRemote { } func (lr *_lsRemote) command() error { - cmd := exec.Command("git", "ls-remote", "--heads", "--tags", lr.url) + cmd := exec.Command("git", "ls-remote", "--heads", "--tags", "--sort=v:refname", lr.url) output, err := cmd.CombinedOutput() if err != nil { + logrus.WithField("cmd", cmd.String()).Errorf("run command failed: %v", err) return err } lr.output = string(output) diff --git a/go.mod b/go.mod index 378d90f..58b4b58 100644 --- a/go.mod +++ b/go.mod @@ -6,9 +6,9 @@ require ( github.com/google/go-github/v53 v53.2.0 github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.7.0 - github.com/stretchr/testify v1.7.0 + github.com/stretchr/testify v1.8.4 gitter.top/common/lormatter v0.0.0-20230722045844-3f2987f9dd0e - golang.org/x/mod v0.10.0 + golang.org/x/mod v0.12.0 ) require ( diff --git a/go.sum b/go.sum index 7049973..ac5ebdd 100644 --- a/go.sum +++ b/go.sum @@ -33,10 +33,11 @@ github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= gitter.top/common/lormatter v0.0.0-20230722045844-3f2987f9dd0e h1:R/PQxgliGNSKBKIXReaZWShyiPqUjwRbd+1j0+H7APo= @@ -48,8 +49,8 @@ golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A= golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk= -golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= diff --git a/gomod.go b/gomod.go index a745e30..35484f2 100644 --- a/gomod.go +++ b/gomod.go @@ -17,7 +17,7 @@ const ( defaultVersion = "latest" ) -var githubRegexp = regexp.MustCompile(`github.com/(.*)/(.*)`) +var githubRegexp = regexp.MustCompile(`github.com/(.*?)/(.*)`) func upgrade(url string) (newUrl string, err error) { var version = defaultVersion @@ -62,9 +62,9 @@ func AllModUpgrade() { if mod.Indirect { continue } - versionUrl, err := upgrade(mod.Mod.String()) + versionUrl, err := upgrade(mod.Mod.Path) if err != nil { - logrus.WithField("url", mod.Mod.String()).Errorf("upgrade failed: %v", err) + logrus.WithField("url", mod.Mod.Path).Errorf("upgrade failed: %v", err) continue } logrus.WithField("url", versionUrl).Infof("upgrade success") @@ -88,7 +88,12 @@ func parseUrl(url string) (owner, repo string) { if len(res[0]) != 3 { logrus.Panicf(fmt.Sprintf("%s url no match github url rule: %s", url, githubRegexp.String())) } - return res[0][1], res[0][2] + owner = res[0][1] + repo = res[0][2] + if repoA := strings.Split(repo, "/"); len(repoA) > 1 { + repo = repoA[0] + } + return owner, repo } func getCommitID(url string) (string, error) { diff --git a/gomod_test.go b/gomod_test.go index f1aade5..32e062f 100644 --- a/gomod_test.go +++ b/gomod_test.go @@ -1,6 +1,7 @@ package gomod import ( + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -11,3 +12,9 @@ func TestGetTag(t *testing.T) { assert.NoError(t, err) t.Log(value) } + +func TestRegexp(t *testing.T) { + var url = "github.com/google/go-github/v53" + ower, repo := parseUrl(url) + fmt.Println(ower, repo) +}