From f536901bc1ac39a4771af570c193732c73eecaaf Mon Sep 17 00:00:00 2001 From: cdce8p <30130371+cdce8p@users.noreply.github.com> Date: Wed, 9 Dec 2020 20:10:59 +0100 Subject: [PATCH 1/3] Only fetch DefaultBranchRef when adding upstream remote during gh repo clone --- git/git.go | 9 +++++-- git/git_test.go | 44 +++++++++++++++++++++++++++++++- pkg/cmd/repo/clone/clone.go | 2 +- pkg/cmd/repo/clone/clone_test.go | 5 +++- pkg/cmd/repo/fork/fork.go | 2 +- 5 files changed, 56 insertions(+), 6 deletions(-) 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..a432f74b5 100644 --- a/git/git_test.go +++ b/git/git_test.go @@ -3,10 +3,12 @@ package git import ( "os/exec" "reflect" + "strings" "testing" "github.com/cli/cli/internal/run" "github.com/cli/cli/test" + "github.com/stretchr/testify/assert" ) func Test_UncommittedChangeCount(t *testing.T) { @@ -170,5 +172,45 @@ 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, restore := test.InitCmdStubber() + defer restore() + + cs.Stub("") // git remote add -f + + err := AddUpstreamRemote(tt.upstreamURL, tt.cloneDir, tt.branches) + if err != nil { + t.Fatalf("error running command `git remote add -f`: %v", err) + } + + assert.Equal(t, 1, cs.Count) + assert.Equal(t, tt.want, strings.Join(cs.Calls[0].Args, " ")) + }) + } } diff --git a/pkg/cmd/repo/clone/clone.go b/pkg/cmd/repo/clone/clone.go index 73f4d7187..e6a10ead0 100644 --- a/pkg/cmd/repo/clone/clone.go +++ b/pkg/cmd/repo/clone/clone.go @@ -144,7 +144,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 e7aa57b08..916c94304 100644 --- a/pkg/cmd/repo/clone/clone_test.go +++ b/pkg/cmd/repo/clone/clone_test.go @@ -218,6 +218,9 @@ func Test_RepoClone_hasParent(t *testing.T) { "name": "ORIG", "owner": { "login": "hubot" + }, + "defaultBranchRef": { + "name": "master" } } } } } @@ -237,7 +240,7 @@ func Test_RepoClone_hasParent(t *testing.T) { } 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, " ")) + assert.Equal(t, "git -C REPO remote add -t master -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 760bb6537..63cbf5129 100644 --- a/pkg/cmd/repo/fork/fork.go +++ b/pkg/cmd/repo/fork/fork.go @@ -273,7 +273,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 } From dcedd32249111fc62eb7a16579c39df36e4b3c8f Mon Sep 17 00:00:00 2001 From: vilmibm Date: Thu, 21 Jan 2021 12:32:40 -0800 Subject: [PATCH 2/3] use newer command stubbing in tests --- git/git_test.go | 11 +++-------- pkg/cmd/repo/clone/clone_test.go | 12 +++++------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/git/git_test.go b/git/git_test.go index a432f74b5..16bf7218e 100644 --- a/git/git_test.go +++ b/git/git_test.go @@ -3,12 +3,10 @@ package git import ( "os/exec" "reflect" - "strings" "testing" "github.com/cli/cli/internal/run" "github.com/cli/cli/test" - "github.com/stretchr/testify/assert" ) func Test_UncommittedChangeCount(t *testing.T) { @@ -199,18 +197,15 @@ func TestAddUpstreamRemote(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - cs, restore := test.InitCmdStubber() - defer restore() + cs, cmdTeardown := run.Stub() + defer cmdTeardown(t) - cs.Stub("") // git remote add -f + 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) } - - assert.Equal(t, 1, cs.Count) - assert.Equal(t, tt.want, strings.Join(cs.Calls[0].Args, " ")) }) } } diff --git a/pkg/cmd/repo/clone/clone_test.go b/pkg/cmd/repo/clone/clone_test.go index 916c94304..7647ca4c0 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" @@ -228,19 +229,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 master -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 -t master -f upstream https://github.com/hubot/ORIG.git", strings.Join(cs.Calls[1].Args, " ")) } func Test_RepoClone_withoutUsername(t *testing.T) { From b906826a6835598f7d5026cb60b462b903e83e77 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Thu, 21 Jan 2021 12:56:53 -0800 Subject: [PATCH 3/3] i like trunk --- pkg/cmd/repo/clone/clone_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/repo/clone/clone_test.go b/pkg/cmd/repo/clone/clone_test.go index 7647ca4c0..d6c34ac16 100644 --- a/pkg/cmd/repo/clone/clone_test.go +++ b/pkg/cmd/repo/clone/clone_test.go @@ -221,7 +221,7 @@ func Test_RepoClone_hasParent(t *testing.T) { "login": "hubot" }, "defaultBranchRef": { - "name": "master" + "name": "trunk" } } } } } @@ -233,7 +233,7 @@ func Test_RepoClone_hasParent(t *testing.T) { defer cmdTeardown(t) cs.Register(`git clone https://github.com/OWNER/REPO.git`, 0, "") - cs.Register(`git -C REPO remote add -t master -f upstream https://github.com/hubot/ORIG.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 {