Skip to content
Merged
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
26 changes: 19 additions & 7 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,33 @@ 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
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
}
}

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
53 changes: 48 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: `given arg is not a valid repo or git remote: expected the "[HOST/]OWNER/REPO" format, got "some_invalid_format"`,
},
{
name: "view flag",
Expand All @@ -74,16 +77,56 @@ 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")},
},
{
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 {
io, _, _, _ := iostreams.Test()
io.SetStdoutTTY(true)
io.SetStdinTTY(true)
io.SetStderrTTY(true)
remotesFunc := tt.remotes
if remotesFunc == nil {
remotesFunc = func() (context.Remotes, error) {
return context.Remotes{}, nil
}
}

f := &cmdutil.Factory{
IOStreams: io,
GitClient: &git.Client{GitPath: "/fake/path/to/git"},
Remotes: remotesFunc,
}

var gotOpts *SetDefaultOptions
Expand Down
Loading