Repository: cli/cli
File: pkg/cmd/repo/setdefault/setdefault.go (Go)

Code before:

			# Show more repository options in the interactive picker
			$ git remote add newrepo https://github.com/owner/repo
			$ gh repo set-default
		`),
		Args: cobra.MaximumNArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			if isLocal, err := opts.GitClient.IsLocalGitRepo(cmd.Context()); err != nil {
				return err
			} else if !isLocal {
				return errors.New("must be run from inside a git repository")
			}

			if len(args) > 0 {
				// First, try to find argument as a git remote name
				if !strings.Contains(args[0], "/") && opts.Remotes != nil {
					if remotes, err := opts.Remotes(); err == nil {
						if remote, err := remotes.FindByName(args[0]); err == nil {
							opts.Repo = remote.Repo
						}
					}
				}
				// If not found as remote name, try parsing as OWNER/REPO
				if opts.Repo == nil {
					var err error
					opts.Repo, err = ghrepo.FromFullName(args[0])
					if err != nil {
						return err
					}
				}
			}

			if !opts.ViewMode && !opts.IO.CanPrompt() && opts.Repo == nil {
				return cmdutil.FlagErrorf("repository required when not running interactively")
			}

			if runF != nil {
				return runF(opts)
			}

			return setDefaultRun(opts)
		},
	}

	cmd.Flags().BoolVarP(&opts.ViewMode, "view", "v", false, "View the current default repository")
	cmd.Flags().BoolVarP(&opts.UnsetMode, "unset", "u", false, "Unset the current default repository")

	return cmd
}

func setDefaultRun(opts *SetDefaultOptions) error {

Review comment (suggestion):
Please note that git remotes can have `/` in their names. It's probably rare, but still allowed. The current change will not pick them up.

Also, even if we allow `/` in remote names, the current ordering prioritises remotes over repo names, which can be a breaking change in cases where people already have remote names with `/` in them. Again, it's rare, but still valid. Also, semantically looking, checking for `/` in the given arg is kind of leaking the internals of the `ghrepo.FromFullName` into the command level.

So, I'd suggest we try `ghrepo.FromFullName` first, and if we get an error, then try to check if it's a valid remote name. If both checks returned an error we should return a explanatory error like:

```
given arg is not a valid repo or git remote: <error from FromFullName>
```

Also, to confirm this behaviour will not break in future, we should add a special test case where there's a git remote named `OWNER/REPO` that points to a URL different than `gihtub.com/OWNER/REPO`, and expect to see `github.com/OWNER/REPO` is ultimately gets set as the default repo.

Code after:

			# Show more repository options in the interactive picker
			$ git remote add newrepo https://github.com/owner/repo
			$ gh repo set-default
		`),
		Args: cobra.MaximumNArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			if isLocal, err := opts.GitClient.IsLocalGitRepo(cmd.Context()); err != nil {
				return err
			} else if !isLocal {
				return errors.New("must be run from inside a git repository")
			}

			if len(args) > 0 {
				var err error
				opts.Repo, err = ghrepo.FromFullName(args[0])
				if err != nil {
					remotes, remoteErr := opts.Remotes()
					if remoteErr != nil {
						return remoteErr
					}

					remote, findErr := remotes.FindByName(args[0])
					if findErr != nil {
						return fmt.Errorf("given arg is not a valid repo or git remote: %w", err)
					}
					opts.Repo = remote.Repo
				}
			}

			if !opts.ViewMode && !opts.IO.CanPrompt() && opts.Repo == nil {
				return cmdutil.FlagErrorf("repository required when not running interactively")
			}

			if runF != nil {
				return runF(opts)
			}

			return setDefaultRun(opts)
		},
	}

	cmd.Flags().BoolVarP(&opts.ViewMode, "view", "v", false, "View the current default repository")
	cmd.Flags().BoolVarP(&opts.UnsetMode, "unset", "u", false, "Unset the current default repository")

	return cmd
}

func setDefaultRun(opts *SetDefaultOptions) error {
	remotes, err := opts.Remotes()
	if err != nil {