Handle repo argument before remote name
This commit is contained in:
parent
73c7fd59ef
commit
5f305e81c2
2 changed files with 32 additions and 14 deletions
|
|
@ -89,21 +89,23 @@ func NewCmdSetDefault(f *cmdutil.Factory, runF func(*SetDefaultOptions) error) *
|
|||
}
|
||||
|
||||
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
|
||||
}
|
||||
var err error
|
||||
opts.Repo, err = ghrepo.FromFullName(args[0])
|
||||
if err != nil {
|
||||
if opts.Remotes == nil {
|
||||
return fmt.Errorf("given arg is not a valid repo or git remote: %w", err)
|
||||
}
|
||||
}
|
||||
// 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
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ func TestNewCmdSetDefault(t *testing.T) {
|
|||
},
|
||||
input: "some_invalid_format",
|
||||
wantErr: true,
|
||||
errMsg: `expected the "[HOST/]OWNER/REPO" format, got "some_invalid_format"`,
|
||||
errMsg: `given arg is not a valid repo or git remote: expected the "[HOST/]OWNER/REPO" format, got "some_invalid_format"`,
|
||||
},
|
||||
{
|
||||
name: "view flag",
|
||||
|
|
@ -93,6 +93,22 @@ func TestNewCmdSetDefault(t *testing.T) {
|
|||
input: "origin",
|
||||
output: SetDefaultOptions{Repo: ghrepo.New("OWNER", "REPO")},
|
||||
},
|
||||
{
|
||||
name: "repo argument despite remote name matching owner/repo",
|
||||
gitStubs: func(cs *run.CommandStubber) {
|
||||
cs.Register(`git rev-parse --git-dir`, 0, ".git")
|
||||
},
|
||||
remotes: func() (context.Remotes, error) {
|
||||
return context.Remotes{
|
||||
{
|
||||
Remote: &git.Remote{Name: "OWNER/REPO"},
|
||||
Repo: ghrepo.New("OTHER", "REPO"),
|
||||
},
|
||||
}, nil
|
||||
},
|
||||
input: "OWNER/REPO",
|
||||
output: SetDefaultOptions{Repo: ghrepo.New("OWNER", "REPO")},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue