Fix recycled pointer for info.RemoteInfo

This commit is contained in:
Ryan McGuire 2024-01-17 14:10:26 -05:00
parent c36607c62d
commit 1c373ff00f
4 changed files with 19 additions and 15 deletions

View File

@ -20,7 +20,6 @@ var aliasListCmd = &cobra.Command{
func runListAliasCmd(cmd *cobra.Command, args []string) { func runListAliasCmd(cmd *cobra.Command, args []string) {
remotes := viper.GetStringSlice("remote") remotes := viper.GetStringSlice("remote")
fmt.Println()
pterm.DefaultBox. pterm.DefaultBox.
WithLeftPadding(5).WithRightPadding(5). WithLeftPadding(5).WithRightPadding(5).
WithBoxStyle(&pterm.Style{pterm.FgLightBlue}). WithBoxStyle(&pterm.Style{pterm.FgLightBlue}).

View File

@ -27,7 +27,7 @@ func initProjectCache(cmd *cobra.Command, args []string) {
conf.Cache.File = conf.ProjectPath + "/.cache.yaml" conf.Cache.File = conf.ProjectPath + "/.cache.yaml"
gitRemotes := remotes.NewRemotes() gitRemotes := remotes.NewRemotes()
gitRemotes.AddRemotes(getRemotes(cmd)...) gitRemotes.AddRemotes(*getRemotes(cmd)...)
cacheOpts := &cache.CacheOpts{ cacheOpts := &cache.CacheOpts{
ProjectsPath: conf.ProjectPath, ProjectsPath: conf.ProjectPath,
@ -51,24 +51,27 @@ func initProjectCache(cmd *cobra.Command, args []string) {
} }
// Generically loads remotes from info.RemoteInfo in config.Remotes // Generically loads remotes from info.RemoteInfo in config.Remotes
func getRemotes(cmd *cobra.Command) []remote.Remote { func getRemotes(cmd *cobra.Command) *remotes.Remotes {
gitRemotes := make([]remote.Remote, 0) gitRemotes := new(remotes.Remotes)
*gitRemotes = make([]remote.Remote, 0)
for _, r := range conf.Remotes { 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 gitRemote remote.Remote
var err error var err error
switch r.Type { switch r.Type {
case "gitlab": case "gitlab":
gitRemote, err = gitlabremote.NewGitlabRemote(&r) gitRemote, err = gitlabremote.NewGitlabRemote(&gitRemoteInfo)
case "gitea": case "gitea":
gitRemote, err = gitearemote.NewGiteaRemote(&r) gitRemote, err = gitearemote.NewGiteaRemote(&gitRemoteInfo)
} }
if err != nil { if err != nil {
plog.Error("Failed to prepare remote", plog.Args( plog.Error("Failed to prepare remote", plog.Args(
"error", err, "error", err,
"type", r.Type)) "type", r.Type))
} else { } else {
gitRemotes = append(gitRemotes, gitRemote) *gitRemotes = append(*gitRemotes, gitRemote)
} }
} }
return gitRemotes return gitRemotes

View File

@ -50,7 +50,9 @@ func (c *Cache) AliasesByProjectString(remotes ...string) string {
w.Init(&str, 10, 0, 0, ' ', tabwriter.AlignRight) w.Init(&str, 10, 0, 0, ' ', tabwriter.AlignRight)
for _, p := range c.GetProjectsWithAliases() { 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 continue
} }
var pa string var pa string

View File

@ -15,10 +15,10 @@ const (
// Globally shared info for all remote types // Globally shared info for all remote types
// Stub package to prevent import cycle // Stub package to prevent import cycle
type RemoteInfo struct { type RemoteInfo struct {
Ctx context.Context Ctx context.Context // Base context for all API calls
Host string Host string // Host as URL with protocol (e.g. https://gitlab.com)
Name string Name string // Human-friendly name for remote
Token string Token string // API token for remote
Type string Type string // Remote type (e.g. gitlab, gitea)
CloneProto CloneProto CloneProto CloneProto // CloneProto (ssh or http) determines what url to use for git clone
} }