Skip to content

Commit 29805a4

Browse files
author
Nate Smith
authored
Merge pull request cli#2588 from cdce8p/gh-clone-fetch
Only fetch default branch when adding upstream remote
2 parents 4860ccc + b906826 commit 29805a4

File tree

5 files changed

+54
-11
lines changed

5 files changed

+54
-11
lines changed

git/git.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,13 @@ func RunClone(cloneURL string, args []string) (target string, err error) {
308308
return
309309
}
310310

311-
func AddUpstreamRemote(upstreamURL, cloneDir string) error {
312-
cloneCmd, err := GitCommand("-C", cloneDir, "remote", "add", "-f", "upstream", upstreamURL)
311+
func AddUpstreamRemote(upstreamURL, cloneDir string, branches []string) error {
312+
args := []string{"-C", cloneDir, "remote", "add"}
313+
for _, branch := range branches {
314+
args = append(args, "-t", branch)
315+
}
316+
args = append(args, "-f", "upstream", upstreamURL)
317+
cloneCmd, err := GitCommand(args...)
313318
if err != nil {
314319
return err
315320
}

git/git_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,5 +170,42 @@ func TestParseExtraCloneArgs(t *testing.T) {
170170
}
171171
})
172172
}
173+
}
173174

175+
func TestAddUpstreamRemote(t *testing.T) {
176+
tests := []struct {
177+
name string
178+
upstreamURL string
179+
cloneDir string
180+
branches []string
181+
want string
182+
}{
183+
{
184+
name: "fetch all",
185+
upstreamURL: "URL",
186+
cloneDir: "DIRECTORY",
187+
branches: []string{},
188+
want: "git -C DIRECTORY remote add -f upstream URL",
189+
},
190+
{
191+
name: "fetch specific branches only",
192+
upstreamURL: "URL",
193+
cloneDir: "DIRECTORY",
194+
branches: []string{"master", "dev"},
195+
want: "git -C DIRECTORY remote add -t master -t dev -f upstream URL",
196+
},
197+
}
198+
for _, tt := range tests {
199+
t.Run(tt.name, func(t *testing.T) {
200+
cs, cmdTeardown := run.Stub()
201+
defer cmdTeardown(t)
202+
203+
cs.Register(tt.want, 0, "")
204+
205+
err := AddUpstreamRemote(tt.upstreamURL, tt.cloneDir, tt.branches)
206+
if err != nil {
207+
t.Fatalf("error running command `git remote add -f`: %v", err)
208+
}
209+
})
210+
}
174211
}

pkg/cmd/repo/clone/clone.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func cloneRun(opts *CloneOptions) error {
159159
}
160160
upstreamURL := ghrepo.FormatRemoteURL(canonicalRepo.Parent, protocol)
161161

162-
err = git.AddUpstreamRemote(upstreamURL, cloneDir)
162+
err = git.AddUpstreamRemote(upstreamURL, cloneDir, []string{canonicalRepo.Parent.DefaultBranchRef.Name})
163163
if err != nil {
164164
return err
165165
}

pkg/cmd/repo/clone/clone_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/cli/cli/internal/config"
9+
"github.com/cli/cli/internal/run"
910
"github.com/cli/cli/pkg/cmdutil"
1011
"github.com/cli/cli/pkg/httpmock"
1112
"github.com/cli/cli/pkg/iostreams"
@@ -229,26 +230,26 @@ func Test_RepoClone_hasParent(t *testing.T) {
229230
"name": "ORIG",
230231
"owner": {
231232
"login": "hubot"
233+
},
234+
"defaultBranchRef": {
235+
"name": "trunk"
232236
}
233237
}
234238
} } }
235239
`))
236240

237241
httpClient := &http.Client{Transport: reg}
238242

239-
cs, restore := test.InitCmdStubber()
240-
defer restore()
243+
cs, cmdTeardown := run.Stub()
244+
defer cmdTeardown(t)
241245

242-
cs.Stub("") // git clone
243-
cs.Stub("") // git remote add
246+
cs.Register(`git clone https://github.com/OWNER/REPO.git`, 0, "")
247+
cs.Register(`git -C REPO remote add -t trunk -f upstream https://github.com/hubot/ORIG.git`, 0, "")
244248

245249
_, err := runCloneCommand(httpClient, "OWNER/REPO")
246250
if err != nil {
247251
t.Fatalf("error running command `repo clone`: %v", err)
248252
}
249-
250-
assert.Equal(t, 2, cs.Count)
251-
assert.Equal(t, "git -C REPO remote add -f upstream https://github.com/hubot/ORIG.git", strings.Join(cs.Calls[1].Args, " "))
252253
}
253254

254255
func Test_RepoClone_withoutUsername(t *testing.T) {

pkg/cmd/repo/fork/fork.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func forkRun(opts *ForkOptions) error {
265265
}
266266

267267
upstreamURL := ghrepo.FormatRemoteURL(repoToFork, protocol)
268-
err = git.AddUpstreamRemote(upstreamURL, cloneDir)
268+
err = git.AddUpstreamRemote(upstreamURL, cloneDir, []string{})
269269
if err != nil {
270270
return err
271271
}

0 commit comments

Comments
 (0)