|
5 | 5 | "net/url" |
6 | 6 | "sort" |
7 | 7 | "strings" |
| 8 | + "time" |
8 | 9 |
|
9 | 10 | "github.com/github/gh-cli/api" |
10 | 11 | "github.com/github/gh-cli/context" |
@@ -51,27 +52,61 @@ func prCreate(cmd *cobra.Command, _ []string) error { |
51 | 52 | baseBranch = baseRepo.DefaultBranchRef.Name |
52 | 53 | } |
53 | 54 |
|
| 55 | + didForkRepo := false |
| 56 | + var headRemote *context.Remote |
54 | 57 | headRepo, err := repoContext.HeadRepo() |
55 | 58 | if err != nil { |
56 | | - // TODO: auto-fork repository and add new git remote |
57 | | - return errors.Wrap(err, "could not determine the head repository") |
| 59 | + if baseRepo.IsPrivate { |
| 60 | + return fmt.Errorf("cannot write to private repository '%s/%s'", baseRepo.RepoOwner(), baseRepo.RepoName()) |
| 61 | + } |
| 62 | + headRepo, err = api.ForkRepo(client, baseRepo) |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("error forking repo: %w", err) |
| 65 | + } |
| 66 | + didForkRepo = true |
| 67 | + // TODO: support non-HTTPS git remote URLs |
| 68 | + baseRepoURL := fmt.Sprintf("https://github.com/%s/%s.git", baseRepo.RepoOwner(), baseRepo.RepoName()) |
| 69 | + headRepoURL := fmt.Sprintf("https://github.com/%s/%s.git", headRepo.RepoOwner(), headRepo.RepoName()) |
| 70 | + // TODO: figure out what to name the new git remote |
| 71 | + gitRemote, err := git.AddRemote("fork", baseRepoURL, headRepoURL) |
| 72 | + if err != nil { |
| 73 | + return fmt.Errorf("error adding remote: %w", err) |
| 74 | + } |
| 75 | + headRemote = &context.Remote{ |
| 76 | + Remote: gitRemote, |
| 77 | + Owner: headRepo.RepoOwner(), |
| 78 | + Repo: headRepo.RepoName(), |
| 79 | + } |
58 | 80 | } |
59 | 81 |
|
60 | 82 | if headBranch == baseBranch && isSameRepo(baseRepo, headRepo) { |
61 | 83 | return fmt.Errorf("must be on a branch named differently than %q", baseBranch) |
62 | 84 | } |
63 | 85 |
|
64 | | - headRemote, err := repoContext.RemoteForRepo(headRepo) |
65 | | - if err != nil { |
66 | | - return errors.Wrap(err, "git remote not found for head repository") |
| 86 | + if headRemote == nil { |
| 87 | + headRemote, err = repoContext.RemoteForRepo(headRepo) |
| 88 | + if err != nil { |
| 89 | + return errors.Wrap(err, "git remote not found for head repository") |
| 90 | + } |
67 | 91 | } |
68 | 92 |
|
69 | 93 | if ucc, err := git.UncommittedChangeCount(); err == nil && ucc > 0 { |
70 | 94 | fmt.Fprintf(cmd.ErrOrStderr(), "Warning: %s\n", utils.Pluralize(ucc, "uncommitted change")) |
71 | 95 | } |
72 | | - // TODO: respect existing upstream configuration of the current branch |
73 | | - if err = git.Push(headRemote.Name, fmt.Sprintf("HEAD:%s", headBranch)); err != nil { |
74 | | - return err |
| 96 | + pushTries := 0 |
| 97 | + maxPushTries := 3 |
| 98 | + for { |
| 99 | + // TODO: respect existing upstream configuration of the current branch |
| 100 | + if err := git.Push(headRemote.Name, fmt.Sprintf("HEAD:%s", headBranch)); err != nil { |
| 101 | + if didForkRepo && pushTries < maxPushTries { |
| 102 | + pushTries++ |
| 103 | + // first wait 2 seconds after forking, then 4s, then 6s |
| 104 | + time.Sleep(time.Duration(2*pushTries) * time.Second) |
| 105 | + continue |
| 106 | + } |
| 107 | + return err |
| 108 | + } |
| 109 | + break |
75 | 110 | } |
76 | 111 |
|
77 | 112 | isWeb, err := cmd.Flags().GetBool("web") |
|
0 commit comments