Skip to content

Commit 73c7fd5

Browse files
committed
feat: allow git remote names in gh repo set-default
When specifying a repository for `gh repo set-default`, users can now use a git remote name (e.g., "origin", "upstream") instead of the full OWNER/REPO format. The command first checks if the argument is a git remote name. If found, it uses the corresponding repository. Otherwise, it falls back to parsing the argument as OWNER/REPO format. Example: gh repo set-default origin Fixes cli#9149 Signed-off-by: majiayu000 <1835304752@qq.com>
1 parent 6acf74e commit 73c7fd5

2 files changed

Lines changed: 49 additions & 15 deletions

File tree

pkg/cmd/repo/setdefault/setdefault.go

Lines changed: 24 additions & 10 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,35 @@ 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 {
83-
var err error
84-
opts.Repo, err = ghrepo.FromFullName(args[0])
85-
if err != nil {
86-
return err
92+
// First, try to find argument as a git remote name
93+
if !strings.Contains(args[0], "/") && opts.Remotes != nil {
94+
if remotes, err := opts.Remotes(); err == nil {
95+
if remote, err := remotes.FindByName(args[0]); err == nil {
96+
opts.Repo = remote.Repo
97+
}
98+
}
99+
}
100+
// If not found as remote name, try parsing as OWNER/REPO
101+
if opts.Repo == nil {
102+
var err error
103+
opts.Repo, err = ghrepo.FromFullName(args[0])
104+
if err != nil {
105+
return err
106+
}
87107
}
88108
}
89109

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

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-
100114
if runF != nil {
101115
return runF(opts)
102116
}

pkg/cmd/repo/setdefault/setdefault_test.go

Lines changed: 25 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: `expected the "[HOST/]OWNER/REPO" format, got "some_invalid_format"`,
5154
},
5255
{
5356
name: "view flag",
@@ -74,6 +77,22 @@ 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+
},
7796
}
7897

7998
for _, tt := range tests {
@@ -84,6 +103,7 @@ func TestNewCmdSetDefault(t *testing.T) {
84103
f := &cmdutil.Factory{
85104
IOStreams: io,
86105
GitClient: &git.Client{GitPath: "/fake/path/to/git"},
106+
Remotes: tt.remotes,
87107
}
88108

89109
var gotOpts *SetDefaultOptions

0 commit comments

Comments
 (0)