Skip to content

Commit 559e729

Browse files
authored
Merge pull request cli#12377 from majiayu000/fix-9149-set-default-remote-name
feat: allow git remote names in gh repo set-default
2 parents 722acf0 + 1d506f5 commit 559e729

2 files changed

Lines changed: 67 additions & 12 deletions

File tree

pkg/cmd/repo/setdefault/setdefault.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ func NewCmdSetDefault(f *cmdutil.Factory, runF func(*SetDefaultOptions) error) *
7070
# Set a repository explicitly
7171
$ gh repo set-default owner/repo
7272
73+
# Set a repository using a git remote name
74+
$ gh repo set-default origin
75+
7376
# View the current default repository
7477
$ gh repo set-default --view
7578
@@ -79,24 +82,33 @@ func NewCmdSetDefault(f *cmdutil.Factory, runF func(*SetDefaultOptions) error) *
7982
`),
8083
Args: cobra.MaximumNArgs(1),
8184
RunE: func(cmd *cobra.Command, args []string) error {
85+
if isLocal, err := opts.GitClient.IsLocalGitRepo(cmd.Context()); err != nil {
86+
return err
87+
} else if !isLocal {
88+
return errors.New("must be run from inside a git repository")
89+
}
90+
8291
if len(args) > 0 {
8392
var err error
8493
opts.Repo, err = ghrepo.FromFullName(args[0])
8594
if err != nil {
86-
return err
95+
remotes, remoteErr := opts.Remotes()
96+
if remoteErr != nil {
97+
return remoteErr
98+
}
99+
100+
remote, findErr := remotes.FindByName(args[0])
101+
if findErr != nil {
102+
return fmt.Errorf("given arg is not a valid repo or git remote: %w", err)
103+
}
104+
opts.Repo = remote.Repo
87105
}
88106
}
89107

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

94-
if isLocal, err := opts.GitClient.IsLocalGitRepo(cmd.Context()); err != nil {
95-
return err
96-
} else if !isLocal {
97-
return errors.New("must be run from inside a git repository")
98-
}
99-
100112
if runF != nil {
101113
return runF(opts)
102114
}

pkg/cmd/repo/setdefault/setdefault_test.go

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func TestNewCmdSetDefault(t *testing.T) {
2121
tests := []struct {
2222
name string
2323
gitStubs func(*run.CommandStubber)
24+
remotes func() (context.Remotes, error)
2425
input string
2526
output SetDefaultOptions
2627
wantErr bool
@@ -43,11 +44,13 @@ func TestNewCmdSetDefault(t *testing.T) {
4344
output: SetDefaultOptions{Repo: ghrepo.New("cli", "cli")},
4445
},
4546
{
46-
name: "invalid repo argument",
47-
gitStubs: func(cs *run.CommandStubber) {},
48-
input: "some_invalid_format",
49-
wantErr: true,
50-
errMsg: `expected the "[HOST/]OWNER/REPO" format, got "some_invalid_format"`,
47+
name: "invalid repo argument",
48+
gitStubs: func(cs *run.CommandStubber) {
49+
cs.Register(`git rev-parse --git-dir`, 0, ".git")
50+
},
51+
input: "some_invalid_format",
52+
wantErr: true,
53+
errMsg: `given arg is not a valid repo or git remote: expected the "[HOST/]OWNER/REPO" format, got "some_invalid_format"`,
5154
},
5255
{
5356
name: "view flag",
@@ -74,16 +77,56 @@ func TestNewCmdSetDefault(t *testing.T) {
7477
wantErr: true,
7578
errMsg: "must be run from inside a git repository",
7679
},
80+
{
81+
name: "remote name argument",
82+
gitStubs: func(cs *run.CommandStubber) {
83+
cs.Register(`git rev-parse --git-dir`, 0, ".git")
84+
},
85+
remotes: func() (context.Remotes, error) {
86+
return context.Remotes{
87+
{
88+
Remote: &git.Remote{Name: "origin"},
89+
Repo: ghrepo.New("OWNER", "REPO"),
90+
},
91+
}, nil
92+
},
93+
input: "origin",
94+
output: SetDefaultOptions{Repo: ghrepo.New("OWNER", "REPO")},
95+
},
96+
{
97+
name: "repo argument despite remote name matching owner/repo",
98+
gitStubs: func(cs *run.CommandStubber) {
99+
cs.Register(`git rev-parse --git-dir`, 0, ".git")
100+
},
101+
remotes: func() (context.Remotes, error) {
102+
return context.Remotes{
103+
{
104+
Remote: &git.Remote{Name: "OWNER/REPO"},
105+
Repo: ghrepo.New("OTHER", "REPO"),
106+
},
107+
}, nil
108+
},
109+
input: "OWNER/REPO",
110+
output: SetDefaultOptions{Repo: ghrepo.New("OWNER", "REPO")},
111+
},
77112
}
78113

79114
for _, tt := range tests {
80115
io, _, _, _ := iostreams.Test()
81116
io.SetStdoutTTY(true)
82117
io.SetStdinTTY(true)
83118
io.SetStderrTTY(true)
119+
remotesFunc := tt.remotes
120+
if remotesFunc == nil {
121+
remotesFunc = func() (context.Remotes, error) {
122+
return context.Remotes{}, nil
123+
}
124+
}
125+
84126
f := &cmdutil.Factory{
85127
IOStreams: io,
86128
GitClient: &git.Client{GitPath: "/fake/path/to/git"},
129+
Remotes: remotesFunc,
87130
}
88131

89132
var gotOpts *SetDefaultOptions

0 commit comments

Comments
 (0)