(remote *cliContext.Remote, pr *api.PullRequest, opts *CheckoutOptions)
| 168 | } |
| 169 | |
| 170 | func cmdsForExistingRemote(remote *cliContext.Remote, pr *api.PullRequest, opts *CheckoutOptions) [][]string { |
| 171 | var cmds [][]string |
| 172 | remoteBranch := fmt.Sprintf("%s/%s", remote.Name, pr.HeadRefName) |
| 173 | |
| 174 | refSpec := fmt.Sprintf("+refs/heads/%s", pr.HeadRefName) |
| 175 | if !opts.Detach { |
| 176 | refSpec += fmt.Sprintf(":refs/remotes/%s", remoteBranch) |
| 177 | } |
| 178 | |
| 179 | cmds = append(cmds, []string{"fetch", remote.Name, refSpec, "--no-tags"}) |
| 180 | |
| 181 | localBranch := pr.HeadRefName |
| 182 | if opts.BranchName != "" { |
| 183 | localBranch = opts.BranchName |
| 184 | } |
| 185 | |
| 186 | switch { |
| 187 | case opts.Detach: |
| 188 | cmds = append(cmds, []string{"checkout", "--detach", "FETCH_HEAD"}) |
| 189 | case localBranchExists(opts.GitClient, localBranch): |
| 190 | cmds = append(cmds, []string{"checkout", localBranch}) |
| 191 | if opts.Force { |
| 192 | cmds = append(cmds, []string{"reset", "--hard", fmt.Sprintf("refs/remotes/%s", remoteBranch)}) |
| 193 | } else { |
| 194 | // TODO: check if non-fast-forward and suggest to use `--force` |
| 195 | cmds = append(cmds, []string{"merge", "--ff-only", fmt.Sprintf("refs/remotes/%s", remoteBranch)}) |
| 196 | } |
| 197 | default: |
| 198 | cmds = append(cmds, []string{"checkout", "-b", localBranch, "--track", remoteBranch}) |
| 199 | } |
| 200 | |
| 201 | return cmds |
| 202 | } |
| 203 | |
| 204 | func cmdsForMissingRemote(pr *api.PullRequest, baseURLOrName, repoHost, defaultBranch, protocol string, opts *CheckoutOptions) [][]string { |
| 205 | var cmds [][]string |
no test coverage detected