Merge pull request #2588 from cdce8p/gh-clone-fetch

Only fetch default branch when adding upstream remote
This commit is contained in:
Nate Smith 2021-01-21 12:57:10 -08:00 committed by GitHub
commit 29805a4003
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 12 deletions

View file

@ -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
}

View file

@ -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)
}
})
}
}

View file

@ -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
}

View file

@ -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) {

View file

@ -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
}