Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions pkg/cmd/repo/setdefault/setdefault.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func NewCmdSetDefault(f *cmdutil.Factory, runF func(*SetDefaultOptions) error) *
# Set a repository explicitly
$ gh repo set-default owner/repo

# Set a repository using a git remote name
$ gh repo set-default origin

# View the current default repository
$ gh repo set-default --view

Expand All @@ -79,24 +82,35 @@ func NewCmdSetDefault(f *cmdutil.Factory, runF func(*SetDefaultOptions) error) *
`),
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 {
return err
// 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 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 runF != nil {
return runF(opts)
}
Expand Down
30 changes: 25 additions & 5 deletions pkg/cmd/repo/setdefault/setdefault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestNewCmdSetDefault(t *testing.T) {
tests := []struct {
name string
gitStubs func(*run.CommandStubber)
remotes func() (context.Remotes, error)
input string
output SetDefaultOptions
wantErr bool
Expand All @@ -43,11 +44,13 @@ func TestNewCmdSetDefault(t *testing.T) {
output: SetDefaultOptions{Repo: ghrepo.New("cli", "cli")},
},
{
name: "invalid repo argument",
gitStubs: func(cs *run.CommandStubber) {},
input: "some_invalid_format",
wantErr: true,
errMsg: `expected the "[HOST/]OWNER/REPO" format, got "some_invalid_format"`,
name: "invalid repo argument",
gitStubs: func(cs *run.CommandStubber) {
cs.Register(`git rev-parse --git-dir`, 0, ".git")
},
input: "some_invalid_format",
wantErr: true,
errMsg: `expected the "[HOST/]OWNER/REPO" format, got "some_invalid_format"`,
},
{
name: "view flag",
Expand All @@ -74,6 +77,22 @@ func TestNewCmdSetDefault(t *testing.T) {
wantErr: true,
errMsg: "must be run from inside a git repository",
},
{
name: "remote name argument",
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: "origin"},
Repo: ghrepo.New("OWNER", "REPO"),
},
}, nil
},
input: "origin",
output: SetDefaultOptions{Repo: ghrepo.New("OWNER", "REPO")},
},
}

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

var gotOpts *SetDefaultOptions
Expand Down