@@ -167,26 +167,14 @@ func cmdsForExistingRemote(remote *context.Remote, pr *api.PullRequest, opts *Ch
167167 cmds = append (cmds , []string {"git" , "merge" , "--ff-only" , fmt .Sprintf ("refs/remotes/%s" , remoteBranch )})
168168 }
169169 default :
170- cmds = append (
171- cmds ,
172- []string {"git" , "checkout" , "-b" , localBranch , "--no-track" , remoteBranch },
173- []string {"git" , "config" , fmt .Sprintf ("branch.%s.remote" , localBranch ), remote .Name },
174- []string {"git" , "config" , fmt .Sprintf ("branch.%s.merge" , pr .HeadRefName ), "refs/heads/" + pr .HeadRefName },
175- )
170+ cmds = append (cmds , []string {"git" , "checkout" , "-b" , localBranch , "--track" , remoteBranch })
176171 }
177172
178173 return cmds
179174}
180175
181176func cmdsForMissingRemote (pr * api.PullRequest , baseURLOrName , repoHost , defaultBranch , protocol string , opts * CheckoutOptions ) [][]string {
182177 var cmds [][]string
183-
184- newBranchName := pr .HeadRefName
185- // avoid naming the new branch the same as the default branch
186- if newBranchName == defaultBranch {
187- newBranchName = fmt .Sprintf ("%s/%s" , pr .HeadRepositoryOwner .Login , newBranchName )
188- }
189-
190178 ref := fmt .Sprintf ("refs/pull/%d/head" , pr .Number )
191179
192180 if opts .Detach {
@@ -195,8 +183,16 @@ func cmdsForMissingRemote(pr *api.PullRequest, baseURLOrName, repoHost, defaultB
195183 return cmds
196184 }
197185
186+ localBranch := pr .HeadRefName
187+ if opts .BranchName != "" {
188+ localBranch = opts .BranchName
189+ } else if pr .HeadRefName == defaultBranch {
190+ // avoid naming the new branch the same as the default branch
191+ localBranch = fmt .Sprintf ("%s/%s" , pr .HeadRepositoryOwner .Login , localBranch )
192+ }
193+
198194 currentBranch , _ := opts .Branch ()
199- if newBranchName == currentBranch {
195+ if localBranch == currentBranch {
200196 // PR head matches currently checked out branch
201197 cmds = append (cmds , []string {"git" , "fetch" , baseURLOrName , ref })
202198 if opts .Force {
@@ -206,19 +202,14 @@ func cmdsForMissingRemote(pr *api.PullRequest, baseURLOrName, repoHost, defaultB
206202 cmds = append (cmds , []string {"git" , "merge" , "--ff-only" , "FETCH_HEAD" })
207203 }
208204 } else {
209- // create a new branch
210205 if opts .Force {
211- cmds = append (cmds , []string {"git" , "fetch" , baseURLOrName , fmt .Sprintf ("%s:%s" , ref , newBranchName ), "--force" })
206+ cmds = append (cmds , []string {"git" , "fetch" , baseURLOrName , fmt .Sprintf ("%s:%s" , ref , localBranch ), "--force" })
212207 } else {
213208 // TODO: check if non-fast-forward and suggest to use `--force`
214- cmds = append (cmds , []string {"git" , "fetch" , baseURLOrName , fmt .Sprintf ("%s:%s" , ref , newBranchName )})
209+ cmds = append (cmds , []string {"git" , "fetch" , baseURLOrName , fmt .Sprintf ("%s:%s" , ref , localBranch )})
215210 }
216211
217- if opts .BranchName != "" {
218- cmds = append (cmds , []string {"git" , "checkout" , "-b" , opts .BranchName , "--track" , newBranchName })
219- } else {
220- cmds = append (cmds , []string {"git" , "checkout" , newBranchName })
221- }
212+ cmds = append (cmds , []string {"git" , "checkout" , localBranch })
222213 }
223214
224215 remote := baseURLOrName
@@ -228,9 +219,9 @@ func cmdsForMissingRemote(pr *api.PullRequest, baseURLOrName, repoHost, defaultB
228219 remote = ghrepo .FormatRemoteURL (headRepo , protocol )
229220 mergeRef = fmt .Sprintf ("refs/heads/%s" , pr .HeadRefName )
230221 }
231- if missingMergeConfigForBranch (newBranchName ) {
232- cmds = append (cmds , []string {"git" , "config" , fmt .Sprintf ("branch.%s.remote" , newBranchName ), remote })
233- cmds = append (cmds , []string {"git" , "config" , fmt .Sprintf ("branch.%s.merge" , newBranchName ), mergeRef })
222+ if missingMergeConfigForBranch (localBranch ) {
223+ cmds = append (cmds , []string {"git" , "config" , fmt .Sprintf ("branch.%s.remote" , localBranch ), remote })
224+ cmds = append (cmds , []string {"git" , "config" , fmt .Sprintf ("branch.%s.merge" , localBranch ), mergeRef })
234225 }
235226
236227 return cmds
0 commit comments