Skip to content

Commit 6557289

Browse files
author
Nate Smith
authored
Merge pull request cli#1882 from dfireBird/remote-renaming-847
feat: implement prompt for remote renaming
2 parents 5430728 + 509e5dd commit 6557289

File tree

2 files changed

+72
-50
lines changed

2 files changed

+72
-50
lines changed

pkg/cmd/repo/fork/fork.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/cli/cli/git"
1313
"github.com/cli/cli/internal/config"
1414
"github.com/cli/cli/internal/ghrepo"
15-
"github.com/cli/cli/internal/run"
1615
"github.com/cli/cli/pkg/cmdutil"
1716
"github.com/cli/cli/pkg/iostreams"
1817
"github.com/cli/cli/pkg/prompt"
@@ -32,6 +31,7 @@ type ForkOptions struct {
3231
Remote bool
3332
PromptClone bool
3433
PromptRemote bool
34+
RemoteName string
3535
}
3636

3737
var Since = func(t time.Time) time.Duration {
@@ -77,6 +77,7 @@ With no argument, creates a fork of the current repository. Otherwise, forks the
7777

7878
cmd.Flags().BoolVar(&opts.Clone, "clone", false, "Clone the fork {true|false}")
7979
cmd.Flags().BoolVar(&opts.Remote, "remote", false, "Add remote for fork {true|false}")
80+
cmd.Flags().StringVar(&opts.RemoteName, "remote-name", "origin", "Specify a name for a fork's new remote.")
8081

8182
return cmd
8283
}
@@ -225,25 +226,13 @@ func forkRun(opts *ForkOptions) error {
225226
}
226227
}
227228
if remoteDesired {
228-
remoteName := "origin"
229-
229+
remoteName := opts.RemoteName
230230
remotes, err := opts.Remotes()
231231
if err != nil {
232232
return err
233233
}
234234
if _, err := remotes.FindByName(remoteName); err == nil {
235-
renameTarget := "upstream"
236-
renameCmd, err := git.GitCommand("remote", "rename", remoteName, renameTarget)
237-
if err != nil {
238-
return err
239-
}
240-
err = run.PrepareCmd(renameCmd).Run()
241-
if err != nil {
242-
return err
243-
}
244-
if connectedToTerminal {
245-
fmt.Fprintf(stderr, "%s Renamed %s remote to %s\n", cs.SuccessIcon(), cs.Bold(remoteName), cs.Bold(renameTarget))
246-
}
235+
return fmt.Errorf("a remote called '%s' already exists. You can rerun this command with --remote-name to specify a different remote name.", remoteName)
247236
}
248237

249238
forkedRepoCloneURL := ghrepo.FormatRemoteURL(forkedRepo, protocol)

pkg/cmd/repo/fork/fork_test.go

Lines changed: 68 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,56 @@ func TestRepoFork_nontty(t *testing.T) {
102102
reg.Verify(t)
103103
}
104104

105+
func TestRepoFork_existing_remote_error(t *testing.T) {
106+
defer stubSince(2 * time.Second)()
107+
reg := &httpmock.Registry{}
108+
defer reg.StubWithFixturePath(200, "./forkResult.json")()
109+
httpClient := &http.Client{Transport: reg}
110+
111+
_, err := runCommand(httpClient, nil, false, "--remote")
112+
if err == nil {
113+
t.Fatal("expected error running command `repo fork`")
114+
}
115+
116+
assert.Equal(t, "a remote called 'origin' already exists. You can rerun this command with --remote-name to specify a different remote name.", err.Error())
117+
118+
reg.Verify(t)
119+
}
120+
121+
func TestRepoFork_no_existing_remote(t *testing.T) {
122+
remotes := []*context.Remote{
123+
{
124+
Remote: &git.Remote{
125+
Name: "upstream",
126+
FetchURL: &url.URL{},
127+
},
128+
Repo: ghrepo.New("OWNER", "REPO"),
129+
},
130+
}
131+
defer stubSince(2 * time.Second)()
132+
reg := &httpmock.Registry{}
133+
defer reg.StubWithFixturePath(200, "./forkResult.json")()
134+
httpClient := &http.Client{Transport: reg}
135+
136+
cs, restore := test.InitCmdStubber()
137+
defer restore()
138+
139+
cs.Stub("") // git remote rename
140+
cs.Stub("") // git remote add
141+
142+
output, err := runCommand(httpClient, remotes, false, "--remote")
143+
if err != nil {
144+
t.Fatalf("error running command `repo fork`: %v", err)
145+
}
146+
147+
assert.Equal(t, 1, len(cs.Calls))
148+
assert.Equal(t, "git remote add -f origin https://github.com/someone/REPO.git", strings.Join(cs.Calls[0].Args, " "))
149+
150+
assert.Equal(t, "", output.String())
151+
assert.Equal(t, "", output.Stderr())
152+
reg.Verify(t)
153+
}
154+
105155
func TestRepoFork_in_parent_nontty(t *testing.T) {
106156
defer stubSince(2 * time.Second)()
107157
reg := &httpmock.Registry{}
@@ -114,14 +164,13 @@ func TestRepoFork_in_parent_nontty(t *testing.T) {
114164
cs.Stub("") // git remote rename
115165
cs.Stub("") // git remote add
116166

117-
output, err := runCommand(httpClient, nil, false, "--remote")
167+
output, err := runCommand(httpClient, nil, false, "--remote --remote-name=fork")
118168
if err != nil {
119169
t.Fatalf("error running command `repo fork`: %v", err)
120170
}
121171

122-
assert.Equal(t, 2, len(cs.Calls))
123-
assert.Equal(t, "git remote rename origin upstream", strings.Join(cs.Calls[0].Args, " "))
124-
assert.Equal(t, "git remote add -f origin https://github.com/someone/REPO.git", strings.Join(cs.Calls[1].Args, " "))
172+
assert.Equal(t, 1, len(cs.Calls))
173+
assert.Equal(t, "git remote add -f fork https://github.com/someone/REPO.git", strings.Join(cs.Calls[0].Args, " "))
125174

126175
assert.Equal(t, "", output.String())
127176
assert.Equal(t, "", output.Stderr())
@@ -285,25 +334,20 @@ func TestRepoFork_in_parent_yes(t *testing.T) {
285334
return &test.OutputStub{}
286335
})()
287336

288-
output, err := runCommand(httpClient, nil, true, "--remote")
337+
output, err := runCommand(httpClient, nil, true, "--remote --remote-name=fork")
289338
if err != nil {
290339
t.Errorf("error running command `repo fork`: %v", err)
291340
}
292341

293-
expectedCmds := []string{
294-
"git remote rename origin upstream",
295-
"git remote add -f origin https://github.com/someone/REPO.git",
296-
}
297-
298-
for x, cmd := range seenCmds {
299-
assert.Equal(t, expectedCmds[x], strings.Join(cmd.Args, " "))
300-
}
342+
assert.Equal(t, 1, len(seenCmds))
343+
expectedCmd := "git remote add -f fork https://github.com/someone/REPO.git"
344+
assert.Equal(t, expectedCmd, strings.Join(seenCmds[0].Args, " "))
301345

302346
assert.Equal(t, "", output.String())
303347

304348
test.ExpectLines(t, output.Stderr(),
305349
"Created fork.*someone/REPO",
306-
"Added remote.*origin")
350+
"Added remote.*fork")
307351
reg.Verify(t)
308352
}
309353

@@ -414,26 +458,20 @@ func TestRepoFork_in_parent_survey_yes(t *testing.T) {
414458

415459
defer prompt.StubConfirm(true)()
416460

417-
output, err := runCommand(httpClient, nil, true, "")
461+
output, err := runCommand(httpClient, nil, true, "--remote-name=fork")
418462
if err != nil {
419463
t.Errorf("error running command `repo fork`: %v", err)
420464
}
421465

422-
expectedCmds := []string{
423-
"git remote rename origin upstream",
424-
"git remote add -f origin https://github.com/someone/REPO.git",
425-
}
426-
427-
for x, cmd := range seenCmds {
428-
assert.Equal(t, expectedCmds[x], strings.Join(cmd.Args, " "))
429-
}
466+
assert.Equal(t, 1, len(seenCmds))
467+
expectedCmd := "git remote add -f fork https://github.com/someone/REPO.git"
468+
assert.Equal(t, expectedCmd, strings.Join(seenCmds[0].Args, " "))
430469

431470
assert.Equal(t, "", output.String())
432471

433472
test.ExpectLines(t, output.Stderr(),
434473
"Created fork.*someone/REPO",
435-
"Renamed.*origin.*remote to.*upstream",
436-
"Added remote.*origin")
474+
"Added remote.*fork")
437475
reg.Verify(t)
438476
}
439477

@@ -491,25 +529,20 @@ func TestRepoFork_in_parent_match_protocol(t *testing.T) {
491529
},
492530
}
493531

494-
output, err := runCommand(httpClient, remotes, true, "--remote")
532+
output, err := runCommand(httpClient, remotes, true, "--remote --remote-name=fork")
495533
if err != nil {
496534
t.Errorf("error running command `repo fork`: %v", err)
497535
}
498536

499-
expectedCmds := []string{
500-
"git remote rename origin upstream",
501-
"git remote add -f origin git@github.com:someone/REPO.git",
502-
}
503-
504-
for x, cmd := range seenCmds {
505-
assert.Equal(t, expectedCmds[x], strings.Join(cmd.Args, " "))
506-
}
537+
assert.Equal(t, 1, len(seenCmds))
538+
expectedCmd := "git remote add -f fork git@github.com:someone/REPO.git"
539+
assert.Equal(t, expectedCmd, strings.Join(seenCmds[0].Args, " "))
507540

508541
assert.Equal(t, "", output.String())
509542

510543
test.ExpectLines(t, output.Stderr(),
511544
"Created fork.*someone/REPO",
512-
"Added remote.*origin")
545+
"Added remote.*fork")
513546
reg.Verify(t)
514547
}
515548

0 commit comments

Comments
 (0)