-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Allow argument to gh repo set-default to be the name of a remote
#10002
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Conversation
|
@ADKaster : Thanks for opening this PR and apologies for delay following up! π Catching up on the issue, I'm going to dig into this and the related code given the concerns you alluded to. π |
andyfeller
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you again for your patience! π Couple of concerns regarding these changes that I think we can address and get this into ship shape!
| if len(args) > 0 { | ||
| var err error | ||
| opts.Repo, err = parseRepo(args[0], opts) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason this logic needed to be moved to later within RunE? Does this create a problem above because opts.Repo is unset?
Depending on the motivation for moving the logic, this might have to be undone as non-interactive usage will break because opts.Repo is unset.
cli/pkg/cmd/repo/setdefault/setdefault.go
Lines 80 to 83 in b4b8e8a
| RunE: func(cmd *cobra.Command, args []string) error { | |
| if !opts.ViewMode && !opts.IO.CanPrompt() && opts.Repo == nil { | |
| return cmdutil.FlagErrorf("repository required when not running interactively") | |
| } |
| if len(args) > 0 { | ||
| var err error | ||
| opts.Repo, err = ghrepo.FromFullName(args[0]) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to move processing opts.Repo out of RunE and into setDefaultRun?
The spf13/cobra command hooks should be focused on translating cobra arguments and flags into variables that the application logic cares about, however this is no longer as simple as turning owner/repo into ghrepo.Interface. With the changes as-is, we're duplicating logic around remotes and starting to do more application-level behavior outside of the application code.
It might be cleaner and easier to test by changing opts.Repo to a string like opts.RepoOrRemote and defer checking remotes or parsing this as a repo until later in the logic:
cli/pkg/cmd/repo/setdefault/setdefault.go
Lines 172 to 182 in 9bdfa83
| if opts.Repo != nil { | |
| for _, knownRepo := range knownRepos { | |
| if ghrepo.IsSame(opts.Repo, knownRepo) { | |
| selectedRepo = opts.Repo | |
| break | |
| } | |
| } | |
| if selectedRepo == nil { | |
| return fmt.Errorf("%s does not correspond to any git remotes", ghrepo.FullName(opts.Repo)) | |
| } | |
| } |
- Error handling around remotes happened earlier in the logic
- Application has remote and resolved remote information
RunElogic keeps this logic within the testable code
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
|
@ADKaster : I want to give a gentle nudge if this is a PR you are still interested in contributing to the GitHub CLI. I understand you may have other priorities going on, however I'm unsure if you saw my comments above and if this is something you're still interested in. |
|
@andyfeller sorry for the lack of response! I did see the comments, but I didn't have the capacity to re-open my Go IDE and dive back into the code. I mostly opened this PR because I was annoyed that I had to do |
I appreciate pain-driven development is what motivates a lot of OSS development! π Thanks for the context. π Given that, I'm going to see if I can shape this up and get it over the line. I wanted to check in before presuming. β€οΈ |
| remotes, err := opts.Remotes() | ||
| if err != nil { | ||
| return nil, err | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My major concern with the code as-is is that multiple calls to resolve git remotes via opts.Remotes() does not reuse previously cached results as stated in #10103.
Perhaps the expense isn't significant enough to move forward here. It is mostly a question whether instead of providing a function to resolve the remotes if an actual remote resolver is provided π€
Fixes #9149
I'm not sure if this is a complete fix, as the UX for passing a random string (or a string you thought was a remote name) is now a bit worse.
It also doesn't actually check with git whether the remote name actually corresponds with a real git remote, which might give a better experience as well.
But this does seem to be the minimum change to let me type
gh repo set-default upstreaminstead of having to dogh repo set-default $(git remote get-url upstream), which was super annoying :).Also perhaps there should be some magic sauce to add a remote named
upstreamorgithubif you specify it and the only remote isoriginand you've cloned a fork?