diff --git a/git/git.go b/git/git.go index 3cda262f0..53e7089da 100644 --- a/git/git.go +++ b/git/git.go @@ -308,8 +308,13 @@ func RunClone(cloneURL string, args []string) (target string, err error) { return } -func AddUpstreamRemote(upstreamURL, cloneDir string) error { - cloneCmd, err := GitCommand("-C", cloneDir, "remote", "add", "-f", "upstream", upstreamURL) +func AddUpstreamRemote(upstreamURL, cloneDir string, branches []string) error { + args := []string{"-C", cloneDir, "remote", "add"} + for _, branch := range branches { + args = append(args, "-t", branch) + } + args = append(args, "-f", "upstream", upstreamURL) + cloneCmd, err := GitCommand(args...) if err != nil { return err } diff --git a/git/git_test.go b/git/git_test.go index 34cc9c7cb..16bf7218e 100644 --- a/git/git_test.go +++ b/git/git_test.go @@ -170,5 +170,42 @@ func TestParseExtraCloneArgs(t *testing.T) { } }) } - +} + +func TestAddUpstreamRemote(t *testing.T) { + tests := []struct { + name string + upstreamURL string + cloneDir string + branches []string + want string + }{ + { + name: "fetch all", + upstreamURL: "URL", + cloneDir: "DIRECTORY", + branches: []string{}, + want: "git -C DIRECTORY remote add -f upstream URL", + }, + { + name: "fetch specific branches only", + upstreamURL: "URL", + cloneDir: "DIRECTORY", + branches: []string{"master", "dev"}, + want: "git -C DIRECTORY remote add -t master -t dev -f upstream URL", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cs, cmdTeardown := run.Stub() + defer cmdTeardown(t) + + cs.Register(tt.want, 0, "") + + err := AddUpstreamRemote(tt.upstreamURL, tt.cloneDir, tt.branches) + if err != nil { + t.Fatalf("error running command `git remote add -f`: %v", err) + } + }) + } } diff --git a/pkg/cmd/repo/clone/clone.go b/pkg/cmd/repo/clone/clone.go index ee2a2ecdf..5cf2d14b8 100644 --- a/pkg/cmd/repo/clone/clone.go +++ b/pkg/cmd/repo/clone/clone.go @@ -159,7 +159,7 @@ func cloneRun(opts *CloneOptions) error { } upstreamURL := ghrepo.FormatRemoteURL(canonicalRepo.Parent, protocol) - err = git.AddUpstreamRemote(upstreamURL, cloneDir) + err = git.AddUpstreamRemote(upstreamURL, cloneDir, []string{canonicalRepo.Parent.DefaultBranchRef.Name}) if err != nil { return err } diff --git a/pkg/cmd/repo/clone/clone_test.go b/pkg/cmd/repo/clone/clone_test.go index db40f4f2d..ddb1084ff 100644 --- a/pkg/cmd/repo/clone/clone_test.go +++ b/pkg/cmd/repo/clone/clone_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/cli/cli/internal/config" + "github.com/cli/cli/internal/run" "github.com/cli/cli/pkg/cmdutil" "github.com/cli/cli/pkg/httpmock" "github.com/cli/cli/pkg/iostreams" @@ -229,6 +230,9 @@ func Test_RepoClone_hasParent(t *testing.T) { "name": "ORIG", "owner": { "login": "hubot" + }, + "defaultBranchRef": { + "name": "trunk" } } } } } @@ -236,19 +240,16 @@ func Test_RepoClone_hasParent(t *testing.T) { httpClient := &http.Client{Transport: reg} - cs, restore := test.InitCmdStubber() - defer restore() + cs, cmdTeardown := run.Stub() + defer cmdTeardown(t) - cs.Stub("") // git clone - cs.Stub("") // git remote add + cs.Register(`git clone https://github.com/OWNER/REPO.git`, 0, "") + cs.Register(`git -C REPO remote add -t trunk -f upstream https://github.com/hubot/ORIG.git`, 0, "") _, err := runCloneCommand(httpClient, "OWNER/REPO") if err != nil { t.Fatalf("error running command `repo clone`: %v", err) } - - assert.Equal(t, 2, cs.Count) - assert.Equal(t, "git -C REPO remote add -f upstream https://github.com/hubot/ORIG.git", strings.Join(cs.Calls[1].Args, " ")) } func Test_RepoClone_withoutUsername(t *testing.T) { diff --git a/pkg/cmd/repo/fork/fork.go b/pkg/cmd/repo/fork/fork.go index 0b0daff04..e983976ef 100644 --- a/pkg/cmd/repo/fork/fork.go +++ b/pkg/cmd/repo/fork/fork.go @@ -265,7 +265,7 @@ func forkRun(opts *ForkOptions) error { } upstreamURL := ghrepo.FormatRemoteURL(repoToFork, protocol) - err = git.AddUpstreamRemote(upstreamURL, cloneDir) + err = git.AddUpstreamRemote(upstreamURL, cloneDir, []string{}) if err != nil { return err }