From 1fa3047b59d07027e233dd6fe8f038718fcf3102 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Fri, 22 Jan 2021 15:52:23 -0800 Subject: [PATCH 1/3] restore renaming behavior for nontty case --- pkg/cmd/repo/fork/fork.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/repo/fork/fork.go b/pkg/cmd/repo/fork/fork.go index e983976ef..a49a0e0f9 100644 --- a/pkg/cmd/repo/fork/fork.go +++ b/pkg/cmd/repo/fork/fork.go @@ -12,6 +12,7 @@ import ( "github.com/cli/cli/git" "github.com/cli/cli/internal/config" "github.com/cli/cli/internal/ghrepo" + "github.com/cli/cli/internal/run" "github.com/cli/cli/pkg/cmdutil" "github.com/cli/cli/pkg/iostreams" "github.com/cli/cli/pkg/prompt" @@ -234,8 +235,21 @@ func forkRun(opts *ForkOptions) error { if err != nil { return err } + if _, err := remotes.FindByName(remoteName); err == nil { - return fmt.Errorf("a remote called '%s' already exists. You can rerun this command with --remote-name to specify a different remote name.", remoteName) + if connectedToTerminal { + return fmt.Errorf("a remote called '%s' already exists. You can rerun this command with --remote-name to specify a different remote name.", remoteName) + } else { + renameTarget := "upstream" + renameCmd, err := git.GitCommand("remote", "rename", remoteName, renameTarget) + if err != nil { + return err + } + err = run.PrepareCmd(renameCmd).Run() + if err != nil { + return err + } + } } forkedRepoCloneURL := ghrepo.FormatRemoteURL(forkedRepo, protocol) From d051f0634fdf085b2387351c8b0471f71ae1e009 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Fri, 22 Jan 2021 16:13:39 -0800 Subject: [PATCH 2/3] update tests --- pkg/cmd/repo/fork/fork_test.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/repo/fork/fork_test.go b/pkg/cmd/repo/fork/fork_test.go index 255e820cd..767c5244e 100644 --- a/pkg/cmd/repo/fork/fork_test.go +++ b/pkg/cmd/repo/fork/fork_test.go @@ -104,7 +104,7 @@ func TestRepoFork_existing_remote_error(t *testing.T) { defer reg.StubWithFixturePath(200, "./forkResult.json")() httpClient := &http.Client{Transport: reg} - _, err := runCommand(httpClient, nil, false, "--remote") + _, err := runCommand(httpClient, nil, true, "--remote") if err == nil { t.Fatal("expected error running command `repo fork`") } @@ -114,7 +114,7 @@ func TestRepoFork_existing_remote_error(t *testing.T) { reg.Verify(t) } -func TestRepoFork_no_existing_remote(t *testing.T) { +func TestRepoFork_no_conflicting_remote(t *testing.T) { remotes := []*context.Remote{ { Remote: &git.Remote{ @@ -153,9 +153,10 @@ func TestRepoFork_in_parent_nontty(t *testing.T) { cs, restore := run.Stub() defer restore(t) - cs.Register(`git remote add -f fork https://github\.com/someone/REPO\.git`, 0, "") + cs.Register("git remote rename origin upstream", 0, "") + cs.Register(`git remote add -f origin https://github\.com/someone/REPO\.git`, 0, "") - output, err := runCommand(httpClient, nil, false, "--remote --remote-name=fork") + output, err := runCommand(httpClient, nil, false, "--remote") if err != nil { t.Fatalf("error running command `repo fork`: %v", err) } From 26f6761481f035325a0776571bf1cb08b9389270 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Mon, 25 Jan 2021 11:29:26 -0800 Subject: [PATCH 3/3] add note about future behavior --- pkg/cmd/repo/fork/fork.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/cmd/repo/fork/fork.go b/pkg/cmd/repo/fork/fork.go index a49a0e0f9..ecd0c20cb 100644 --- a/pkg/cmd/repo/fork/fork.go +++ b/pkg/cmd/repo/fork/fork.go @@ -240,6 +240,8 @@ func forkRun(opts *ForkOptions) error { if connectedToTerminal { return fmt.Errorf("a remote called '%s' already exists. You can rerun this command with --remote-name to specify a different remote name.", remoteName) } else { + // TODO next major version we should break this behavior and force users to opt into + // remote renaming in a scripting context via --remote-name renameTarget := "upstream" renameCmd, err := git.GitCommand("remote", "rename", remoteName, renameTarget) if err != nil {