Skip to content

Commit 6152d8a

Browse files
author
Nate Smith
authored
Merge pull request cli#2825 from cli/rename-fork
restore fork rename behavior for nontty case
2 parents 8125cd5 + 26f6761 commit 6152d8a

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

pkg/cmd/repo/fork/fork.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/cli/cli/git"
1313
"github.com/cli/cli/internal/config"
1414
"github.com/cli/cli/internal/ghrepo"
15+
"github.com/cli/cli/internal/run"
1516
"github.com/cli/cli/pkg/cmdutil"
1617
"github.com/cli/cli/pkg/iostreams"
1718
"github.com/cli/cli/pkg/prompt"
@@ -234,8 +235,23 @@ func forkRun(opts *ForkOptions) error {
234235
if err != nil {
235236
return err
236237
}
238+
237239
if _, err := remotes.FindByName(remoteName); err == nil {
238-
return fmt.Errorf("a remote called '%s' already exists. You can rerun this command with --remote-name to specify a different remote name.", remoteName)
240+
if connectedToTerminal {
241+
return fmt.Errorf("a remote called '%s' already exists. You can rerun this command with --remote-name to specify a different remote name.", remoteName)
242+
} else {
243+
// TODO next major version we should break this behavior and force users to opt into
244+
// remote renaming in a scripting context via --remote-name
245+
renameTarget := "upstream"
246+
renameCmd, err := git.GitCommand("remote", "rename", remoteName, renameTarget)
247+
if err != nil {
248+
return err
249+
}
250+
err = run.PrepareCmd(renameCmd).Run()
251+
if err != nil {
252+
return err
253+
}
254+
}
239255
}
240256

241257
forkedRepoCloneURL := ghrepo.FormatRemoteURL(forkedRepo, protocol)

pkg/cmd/repo/fork/fork_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func TestRepoFork_existing_remote_error(t *testing.T) {
104104
defer reg.StubWithFixturePath(200, "./forkResult.json")()
105105
httpClient := &http.Client{Transport: reg}
106106

107-
_, err := runCommand(httpClient, nil, false, "--remote")
107+
_, err := runCommand(httpClient, nil, true, "--remote")
108108
if err == nil {
109109
t.Fatal("expected error running command `repo fork`")
110110
}
@@ -114,7 +114,7 @@ func TestRepoFork_existing_remote_error(t *testing.T) {
114114
reg.Verify(t)
115115
}
116116

117-
func TestRepoFork_no_existing_remote(t *testing.T) {
117+
func TestRepoFork_no_conflicting_remote(t *testing.T) {
118118
remotes := []*context.Remote{
119119
{
120120
Remote: &git.Remote{
@@ -153,9 +153,10 @@ func TestRepoFork_in_parent_nontty(t *testing.T) {
153153
cs, restore := run.Stub()
154154
defer restore(t)
155155

156-
cs.Register(`git remote add -f fork https://github\.com/someone/REPO\.git`, 0, "")
156+
cs.Register("git remote rename origin upstream", 0, "")
157+
cs.Register(`git remote add -f origin https://github\.com/someone/REPO\.git`, 0, "")
157158

158-
output, err := runCommand(httpClient, nil, false, "--remote --remote-name=fork")
159+
output, err := runCommand(httpClient, nil, false, "--remote")
159160
if err != nil {
160161
t.Fatalf("error running command `repo fork`: %v", err)
161162
}

0 commit comments

Comments
 (0)