From 965f2704f34acf03482d25eb08adcf5408d5e441 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Fri, 27 Mar 2020 13:27:22 +0100 Subject: [PATCH] repo fork: reuse existing git remote Avoid adding a new git remote for a fork if an existing remote is found that points to the exact forked repo. --- command/repo.go | 9 +++++++++ command/repo_test.go | 26 +++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/command/repo.go b/command/repo.go index 6c77300da..7f31b816c 100644 --- a/command/repo.go +++ b/command/repo.go @@ -328,6 +328,15 @@ func repoFork(cmd *cobra.Command, args []string) error { } if inParent { + remotes, err := ctx.Remotes() + if err != nil { + return err + } + if remote, err := remotes.FindByRepo(forkedRepo.RepoOwner(), forkedRepo.RepoName()); err == nil { + fmt.Fprintf(out, "%s Using existing remote %s\n", greenCheck, utils.Bold(remote.Name)) + return nil + } + remoteDesired := remotePref == "true" if remotePref == "prompt" { err = Confirm("Would you like to add a remote for the fork?", &remoteDesired) diff --git a/command/repo_test.go b/command/repo_test.go index cc5cefe84..6f4591b60 100644 --- a/command/repo_test.go +++ b/command/repo_test.go @@ -40,6 +40,31 @@ func TestRepoFork_already_forked(t *testing.T) { } } +func TestRepoFork_reuseRemote(t *testing.T) { + initContext = func() context.Context { + ctx := context.NewBlank() + ctx.SetBaseRepo("OWNER/REPO") + ctx.SetBranch("master") + ctx.SetRemotes(map[string]string{ + "upstream": "OWNER/REPO", + "origin": "someone/REPO", + }) + return ctx + } + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + defer http.StubWithFixture(200, "forkResult.json")() + + output, err := RunCommand(repoForkCmd, "repo fork") + if err != nil { + t.Errorf("got unexpected error: %v", err) + } + if !strings.Contains(output.String(), "Using existing remote origin") { + t.Errorf("output did not match: %q", output) + return + } +} + func stubSince(d time.Duration) func() { originalSince := Since Since = func(t time.Time) time.Duration { @@ -579,7 +604,6 @@ func TestRepoCreate_orgWithTeam(t *testing.T) { } } - func TestRepoView(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP()