Skip to content

Commit f536901

Browse files
committed
Only fetch DefaultBranchRef when adding upstream remote during gh repo clone
1 parent f415245 commit f536901

File tree

5 files changed

+55
-5
lines changed

5 files changed

+55
-5
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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package git
33
import (
44
"os/exec"
55
"reflect"
6+
"strings"
67
"testing"
78

89
"github.com/cli/cli/internal/run"
910
"github.com/cli/cli/test"
11+
"github.com/stretchr/testify/assert"
1012
)
1113

1214
func Test_UncommittedChangeCount(t *testing.T) {
@@ -170,5 +172,45 @@ func TestParseExtraCloneArgs(t *testing.T) {
170172
}
171173
})
172174
}
175+
}
176+
177+
func TestAddUpstreamRemote(t *testing.T) {
178+
tests := []struct {
179+
name string
180+
upstreamURL string
181+
cloneDir string
182+
branches []string
183+
want string
184+
}{
185+
{
186+
name: "fetch all",
187+
upstreamURL: "URL",
188+
cloneDir: "DIRECTORY",
189+
branches: []string{},
190+
want: "git -C DIRECTORY remote add -f upstream URL",
191+
},
192+
{
193+
name: "fetch specific branches only",
194+
upstreamURL: "URL",
195+
cloneDir: "DIRECTORY",
196+
branches: []string{"master", "dev"},
197+
want: "git -C DIRECTORY remote add -t master -t dev -f upstream URL",
198+
},
199+
}
200+
for _, tt := range tests {
201+
t.Run(tt.name, func(t *testing.T) {
202+
cs, restore := test.InitCmdStubber()
203+
defer restore()
204+
205+
cs.Stub("") // git remote add -f
173206

207+
err := AddUpstreamRemote(tt.upstreamURL, tt.cloneDir, tt.branches)
208+
if err != nil {
209+
t.Fatalf("error running command `git remote add -f`: %v", err)
210+
}
211+
212+
assert.Equal(t, 1, cs.Count)
213+
assert.Equal(t, tt.want, strings.Join(cs.Calls[0].Args, " "))
214+
})
215+
}
174216
}

pkg/cmd/repo/clone/clone.go

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

147-
err = git.AddUpstreamRemote(upstreamURL, cloneDir)
147+
err = git.AddUpstreamRemote(upstreamURL, cloneDir, []string{canonicalRepo.Parent.DefaultBranchRef.Name})
148148
if err != nil {
149149
return err
150150
}

pkg/cmd/repo/clone/clone_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ func Test_RepoClone_hasParent(t *testing.T) {
218218
"name": "ORIG",
219219
"owner": {
220220
"login": "hubot"
221+
},
222+
"defaultBranchRef": {
223+
"name": "master"
221224
}
222225
}
223226
} } }
@@ -237,7 +240,7 @@ func Test_RepoClone_hasParent(t *testing.T) {
237240
}
238241

239242
assert.Equal(t, 2, cs.Count)
240-
assert.Equal(t, "git -C REPO remote add -f upstream https://github.com/hubot/ORIG.git", strings.Join(cs.Calls[1].Args, " "))
243+
assert.Equal(t, "git -C REPO remote add -t master -f upstream https://github.com/hubot/ORIG.git", strings.Join(cs.Calls[1].Args, " "))
241244
}
242245

243246
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
@@ -273,7 +273,7 @@ func forkRun(opts *ForkOptions) error {
273273
}
274274

275275
upstreamURL := ghrepo.FormatRemoteURL(repoToFork, protocol)
276-
err = git.AddUpstreamRemote(upstreamURL, cloneDir)
276+
err = git.AddUpstreamRemote(upstreamURL, cloneDir, []string{})
277277
if err != nil {
278278
return err
279279
}

0 commit comments

Comments
 (0)