From 1c373ff00f67460dd0a28f172891951289086a64 Mon Sep 17 00:00:00 2001 From: Ryan D McGuire Date: Wed, 17 Jan 2024 14:10:26 -0500 Subject: [PATCH] Fix recycled pointer for info.RemoteInfo --- cmd/alias_list.go | 1 - cmd/util_init.go | 17 ++++++++++------- internal/cache/projects_alias.go | 4 +++- internal/remotes/info/info.go | 12 ++++++------ 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/cmd/alias_list.go b/cmd/alias_list.go index 03231aa..31d524b 100644 --- a/cmd/alias_list.go +++ b/cmd/alias_list.go @@ -20,7 +20,6 @@ var aliasListCmd = &cobra.Command{ func runListAliasCmd(cmd *cobra.Command, args []string) { remotes := viper.GetStringSlice("remote") - fmt.Println() pterm.DefaultBox. WithLeftPadding(5).WithRightPadding(5). WithBoxStyle(&pterm.Style{pterm.FgLightBlue}). diff --git a/cmd/util_init.go b/cmd/util_init.go index cedc4b8..b116b8c 100644 --- a/cmd/util_init.go +++ b/cmd/util_init.go @@ -27,7 +27,7 @@ func initProjectCache(cmd *cobra.Command, args []string) { conf.Cache.File = conf.ProjectPath + "/.cache.yaml" gitRemotes := remotes.NewRemotes() - gitRemotes.AddRemotes(getRemotes(cmd)...) + gitRemotes.AddRemotes(*getRemotes(cmd)...) cacheOpts := &cache.CacheOpts{ ProjectsPath: conf.ProjectPath, @@ -51,24 +51,27 @@ func initProjectCache(cmd *cobra.Command, args []string) { } // Generically loads remotes from info.RemoteInfo in config.Remotes -func getRemotes(cmd *cobra.Command) []remote.Remote { - gitRemotes := make([]remote.Remote, 0) +func getRemotes(cmd *cobra.Command) *remotes.Remotes { + gitRemotes := new(remotes.Remotes) + *gitRemotes = make([]remote.Remote, 0) for _, r := range conf.Remotes { - r.Ctx = cmd.Context() + // Create a copy, set context + gitRemoteInfo := r + gitRemoteInfo.Ctx = cmd.Context() var gitRemote remote.Remote var err error switch r.Type { case "gitlab": - gitRemote, err = gitlabremote.NewGitlabRemote(&r) + gitRemote, err = gitlabremote.NewGitlabRemote(&gitRemoteInfo) case "gitea": - gitRemote, err = gitearemote.NewGiteaRemote(&r) + gitRemote, err = gitearemote.NewGiteaRemote(&gitRemoteInfo) } if err != nil { plog.Error("Failed to prepare remote", plog.Args( "error", err, "type", r.Type)) } else { - gitRemotes = append(gitRemotes, gitRemote) + *gitRemotes = append(*gitRemotes, gitRemote) } } return gitRemotes diff --git a/internal/cache/projects_alias.go b/internal/cache/projects_alias.go index eb827be..3eacb4c 100644 --- a/internal/cache/projects_alias.go +++ b/internal/cache/projects_alias.go @@ -50,7 +50,9 @@ func (c *Cache) AliasesByProjectString(remotes ...string) string { w.Init(&str, 10, 0, 0, ' ', tabwriter.AlignRight) for _, p := range c.GetProjectsWithAliases() { - if !slices.Contains(remotes, p.Remote) { + if p == nil { + continue + } else if len(remotes) > 0 && !slices.Contains(remotes, p.Remote) { continue } var pa string diff --git a/internal/remotes/info/info.go b/internal/remotes/info/info.go index 50f5905..760ee29 100644 --- a/internal/remotes/info/info.go +++ b/internal/remotes/info/info.go @@ -15,10 +15,10 @@ const ( // Globally shared info for all remote types // Stub package to prevent import cycle type RemoteInfo struct { - Ctx context.Context - Host string - Name string - Token string - Type string - CloneProto CloneProto + Ctx context.Context // Base context for all API calls + Host string // Host as URL with protocol (e.g. https://gitlab.com) + Name string // Human-friendly name for remote + Token string // API token for remote + Type string // Remote type (e.g. gitlab, gitea) + CloneProto CloneProto // CloneProto (ssh or http) determines what url to use for git clone }