Skip to content

Commit c7b0abd

Browse files
authored
Merge pull request cli#708 from cli/detect-existing-fork
repo fork: reuse existing git remote if available
2 parents 988d36d + 265db26 commit c7b0abd

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

command/repo.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -303,14 +303,6 @@ func repoFork(cmd *cobra.Command, args []string) error {
303303
s.FinalMSG = utils.Gray(fmt.Sprintf("- %s\n", loading))
304304
s.Start()
305305

306-
authLogin, err := ctx.AuthLogin()
307-
if err != nil {
308-
s.Stop()
309-
return fmt.Errorf("could not determine current username: %w", err)
310-
}
311-
312-
possibleFork := ghrepo.New(authLogin, toFork.RepoName())
313-
314306
forkedRepo, err := api.ForkRepo(apiClient, toFork)
315307
if err != nil {
316308
s.Stop()
@@ -323,11 +315,11 @@ func repoFork(cmd *cobra.Command, args []string) error {
323315
// returns the fork repo data even if it already exists -- with no change in status code or
324316
// anything. We thus check the created time to see if the repo is brand new or not; if it's not,
325317
// we assume the fork already existed and report an error.
326-
created_ago := Since(forkedRepo.CreatedAt)
327-
if created_ago > time.Minute {
318+
createdAgo := Since(forkedRepo.CreatedAt)
319+
if createdAgo > time.Minute {
328320
fmt.Fprintf(out, "%s %s %s\n",
329321
utils.Yellow("!"),
330-
utils.Bold(ghrepo.FullName(possibleFork)),
322+
utils.Bold(ghrepo.FullName(forkedRepo)),
331323
"already exists")
332324
} else {
333325
fmt.Fprintf(out, "%s Created fork %s\n", greenCheck, utils.Bold(ghrepo.FullName(forkedRepo)))
@@ -338,6 +330,15 @@ func repoFork(cmd *cobra.Command, args []string) error {
338330
}
339331

340332
if inParent {
333+
remotes, err := ctx.Remotes()
334+
if err != nil {
335+
return err
336+
}
337+
if remote, err := remotes.FindByRepo(forkedRepo.RepoOwner(), forkedRepo.RepoName()); err == nil {
338+
fmt.Fprintf(out, "%s Using existing remote %s\n", greenCheck, utils.Bold(remote.Name))
339+
return nil
340+
}
341+
341342
remoteDesired := remotePref == "true"
342343
if remotePref == "prompt" {
343344
err = Confirm("Would you like to add a remote for the fork?", &remoteDesired)

command/repo_test.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ import (
1818
func TestRepoFork_already_forked(t *testing.T) {
1919
initContext = func() context.Context {
2020
ctx := context.NewBlank()
21-
ctx.SetBaseRepo("REPO")
21+
ctx.SetBaseRepo("OWNER/REPO")
2222
ctx.SetBranch("master")
23-
ctx.SetAuthLogin("someone")
2423
ctx.SetRemotes(map[string]string{
2524
"origin": "OWNER/REPO",
2625
})
@@ -41,6 +40,31 @@ func TestRepoFork_already_forked(t *testing.T) {
4140
}
4241
}
4342

43+
func TestRepoFork_reuseRemote(t *testing.T) {
44+
initContext = func() context.Context {
45+
ctx := context.NewBlank()
46+
ctx.SetBaseRepo("OWNER/REPO")
47+
ctx.SetBranch("master")
48+
ctx.SetRemotes(map[string]string{
49+
"upstream": "OWNER/REPO",
50+
"origin": "someone/REPO",
51+
})
52+
return ctx
53+
}
54+
http := initFakeHTTP()
55+
http.StubRepoResponse("OWNER", "REPO")
56+
defer http.StubWithFixture(200, "forkResult.json")()
57+
58+
output, err := RunCommand(repoForkCmd, "repo fork")
59+
if err != nil {
60+
t.Errorf("got unexpected error: %v", err)
61+
}
62+
if !strings.Contains(output.String(), "Using existing remote origin") {
63+
t.Errorf("output did not match: %q", output)
64+
return
65+
}
66+
}
67+
4468
func stubSince(d time.Duration) func() {
4569
originalSince := Since
4670
Since = func(t time.Time) time.Duration {

0 commit comments

Comments
 (0)