Skip to content

Commit 742a194

Browse files
author
Nate Smith
authored
Merge pull request cli#720 from cli/fork-rename-remotes
Add "upstream" remote after `repo fork`
2 parents 0e9b48d + fa3d65b commit 742a194

File tree

2 files changed

+56
-28
lines changed

2 files changed

+56
-28
lines changed

command/repo.go

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,7 @@ func repoClone(cmd *cobra.Command, args []string) error {
129129
}
130130

131131
if parentRepo != nil {
132-
// TODO: support SSH remote URLs
133-
upstreamURL := fmt.Sprintf("https://github.com/%s.git", ghrepo.FullName(parentRepo))
134-
cloneDir := path.Base(strings.TrimSuffix(cloneURL, ".git"))
135-
136-
cloneCmd := git.GitCommand("-C", cloneDir, "remote", "add", "upstream", upstreamURL)
137-
cloneCmd.Stdout = os.Stdout
138-
cloneCmd.Stderr = os.Stderr
139-
err := run.PrepareCmd(cloneCmd).Run()
132+
err := addUpstreamRemote(parentRepo, cloneURL)
140133
if err != nil {
141134
return err
142135
}
@@ -145,6 +138,17 @@ func repoClone(cmd *cobra.Command, args []string) error {
145138
return nil
146139
}
147140

141+
func addUpstreamRemote(parentRepo ghrepo.Interface, cloneURL string) error {
142+
// TODO: support SSH remote URLs
143+
upstreamURL := fmt.Sprintf("https://github.com/%s.git", ghrepo.FullName(parentRepo))
144+
cloneDir := path.Base(strings.TrimSuffix(cloneURL, ".git"))
145+
146+
cloneCmd := git.GitCommand("-C", cloneDir, "remote", "add", "upstream", upstreamURL)
147+
cloneCmd.Stdout = os.Stdout
148+
cloneCmd.Stderr = os.Stderr
149+
return run.PrepareCmd(cloneCmd).Run()
150+
}
151+
148152
func repoCreate(cmd *cobra.Command, args []string) error {
149153
projectDir, projectDirErr := git.ToplevelDir()
150154

@@ -389,12 +393,28 @@ func repoFork(cmd *cobra.Command, args []string) error {
389393
}
390394
}
391395
if remoteDesired {
392-
_, err := git.AddRemote("fork", forkedRepo.CloneURL)
396+
remoteName := "origin"
397+
398+
remotes, err := ctx.Remotes()
399+
if err != nil {
400+
return err
401+
}
402+
if _, err := remotes.FindByName(remoteName); err == nil {
403+
renameTarget := "upstream"
404+
renameCmd := git.GitCommand("remote", "rename", remoteName, renameTarget)
405+
err = run.PrepareCmd(renameCmd).Run()
406+
if err != nil {
407+
return err
408+
}
409+
fmt.Fprintf(out, "%s Renamed %s remote to %s\n", greenCheck, utils.Bold(remoteName), utils.Bold(renameTarget))
410+
}
411+
412+
_, err = git.AddRemote(remoteName, forkedRepo.CloneURL)
393413
if err != nil {
394414
return fmt.Errorf("failed to add remote: %w", err)
395415
}
396416

397-
fmt.Fprintf(out, "%s Remote added at %s\n", greenCheck, utils.Bold("fork"))
417+
fmt.Fprintf(out, "%s Added remote %s\n", greenCheck, utils.Bold(remoteName))
398418
}
399419
} else {
400420
cloneDesired := clonePref == "true"
@@ -414,6 +434,11 @@ func repoFork(cmd *cobra.Command, args []string) error {
414434
return fmt.Errorf("failed to clone fork: %w", err)
415435
}
416436

437+
err = addUpstreamRemote(toFork, forkedRepo.CloneURL)
438+
if err != nil {
439+
return err
440+
}
441+
417442
fmt.Fprintf(out, "%s Cloned fork\n", greenCheck)
418443
}
419444
}

command/repo_test.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ func TestRepoFork_in_parent_yes(t *testing.T) {
151151
}
152152

153153
expectedCmds := []string{
154-
"git remote add -f fork https://github.com/someone/repo.git",
155-
"git fetch fork",
154+
"git remote rename origin upstream",
155+
"git remote add -f origin https://github.com/someone/repo.git",
156156
}
157157

158158
for x, cmd := range seenCmds {
@@ -163,19 +163,19 @@ func TestRepoFork_in_parent_yes(t *testing.T) {
163163

164164
test.ExpectLines(t, output.String(),
165165
"Created fork someone/REPO",
166-
"Remote added at fork")
166+
"Added remote origin")
167167
}
168168

169169
func TestRepoFork_outside_yes(t *testing.T) {
170170
defer stubSince(2 * time.Second)()
171171
http := initFakeHTTP()
172172
defer http.StubWithFixture(200, "forkResult.json")()
173173

174-
var seenCmd *exec.Cmd
175-
defer run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
176-
seenCmd = cmd
177-
return &test.OutputStub{}
178-
})()
174+
cs, restore := test.InitCmdStubber()
175+
defer restore()
176+
177+
cs.Stub("") // git clone
178+
cs.Stub("") // git remote add
179179

180180
output, err := RunCommand(repoForkCmd, "repo fork --clone OWNER/REPO")
181181
if err != nil {
@@ -184,7 +184,8 @@ func TestRepoFork_outside_yes(t *testing.T) {
184184

185185
eq(t, output.Stderr(), "")
186186

187-
eq(t, strings.Join(seenCmd.Args, " "), "git clone https://github.com/someone/repo.git")
187+
eq(t, strings.Join(cs.Calls[0].Args, " "), "git clone https://github.com/someone/repo.git")
188+
eq(t, strings.Join(cs.Calls[1].Args, " "), "git -C repo remote add upstream https://github.com/OWNER/REPO.git")
188189

189190
test.ExpectLines(t, output.String(),
190191
"Created fork someone/REPO",
@@ -196,11 +197,11 @@ func TestRepoFork_outside_survey_yes(t *testing.T) {
196197
http := initFakeHTTP()
197198
defer http.StubWithFixture(200, "forkResult.json")()
198199

199-
var seenCmd *exec.Cmd
200-
defer run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
201-
seenCmd = cmd
202-
return &test.OutputStub{}
203-
})()
200+
cs, restore := test.InitCmdStubber()
201+
defer restore()
202+
203+
cs.Stub("") // git clone
204+
cs.Stub("") // git remote add
204205

205206
oldConfirm := Confirm
206207
Confirm = func(_ string, result *bool) error {
@@ -216,7 +217,8 @@ func TestRepoFork_outside_survey_yes(t *testing.T) {
216217

217218
eq(t, output.Stderr(), "")
218219

219-
eq(t, strings.Join(seenCmd.Args, " "), "git clone https://github.com/someone/repo.git")
220+
eq(t, strings.Join(cs.Calls[0].Args, " "), "git clone https://github.com/someone/repo.git")
221+
eq(t, strings.Join(cs.Calls[1].Args, " "), "git -C repo remote add upstream https://github.com/OWNER/REPO.git")
220222

221223
test.ExpectLines(t, output.String(),
222224
"Created fork someone/REPO",
@@ -283,8 +285,8 @@ func TestRepoFork_in_parent_survey_yes(t *testing.T) {
283285
}
284286

285287
expectedCmds := []string{
286-
"git remote add -f fork https://github.com/someone/repo.git",
287-
"git fetch fork",
288+
"git remote rename origin upstream",
289+
"git remote add -f origin https://github.com/someone/repo.git",
288290
}
289291

290292
for x, cmd := range seenCmds {
@@ -295,7 +297,8 @@ func TestRepoFork_in_parent_survey_yes(t *testing.T) {
295297

296298
test.ExpectLines(t, output.String(),
297299
"Created fork someone/REPO",
298-
"Remote added at fork")
300+
"Renamed origin remote to upstream",
301+
"Added remote origin")
299302
}
300303

301304
func TestRepoFork_in_parent_survey_no(t *testing.T) {

0 commit comments

Comments
 (0)