Skip to content

Commit c987c57

Browse files
authored
pr merge: pull after switching branches (cli#4748)
1 parent 31010f8 commit c987c57

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

git/git.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,19 @@ func CheckoutBranch(branch string) error {
307307
return run.PrepareCmd(configCmd).Run()
308308
}
309309

310+
// pull changes from remote branch without version history
311+
func Pull(remote, branch string) error {
312+
pullCmd, err := GitCommand("pull", "--ff-only", remote, branch)
313+
if err != nil {
314+
return err
315+
}
316+
317+
pullCmd.Stdout = os.Stdout
318+
pullCmd.Stderr = os.Stderr
319+
pullCmd.Stdin = os.Stdin
320+
return run.PrepareCmd(pullCmd).Run()
321+
}
322+
310323
func parseCloneArgs(extraArgs []string) (args []string, target string) {
311324
args = extraArgs
312325

pkg/cmd/pr/merge/merge.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"github.com/AlecAivazis/survey/v2"
99
"github.com/MakeNowJust/heredoc"
1010
"github.com/cli/cli/v2/api"
11+
"github.com/cli/cli/v2/context"
1112
"github.com/cli/cli/v2/git"
1213
"github.com/cli/cli/v2/internal/config"
14+
"github.com/cli/cli/v2/internal/ghrepo"
1315
"github.com/cli/cli/v2/pkg/cmd/pr/shared"
1416
"github.com/cli/cli/v2/pkg/cmdutil"
1517
"github.com/cli/cli/v2/pkg/iostreams"
@@ -26,6 +28,7 @@ type MergeOptions struct {
2628
HttpClient func() (*http.Client, error)
2729
IO *iostreams.IOStreams
2830
Branch func() (string, error)
31+
Remotes func() (context.Remotes, error)
2932

3033
Finder shared.PRFinder
3134

@@ -51,6 +54,7 @@ func NewCmdMerge(f *cmdutil.Factory, runF func(*MergeOptions) error) *cobra.Comm
5154
IO: f.IOStreams,
5255
HttpClient: f.HttpClient,
5356
Branch: f.Branch,
57+
Remotes: f.Remotes,
5458
}
5559

5660
var (
@@ -333,10 +337,16 @@ func mergeRun(opts *MergeOptions) error {
333337
if err != nil {
334338
return err
335339
}
340+
336341
err = git.CheckoutBranch(branchToSwitchTo)
337342
if err != nil {
338343
return err
339344
}
345+
346+
err := pullLatestChanges(opts, baseRepo, branchToSwitchTo)
347+
if err != nil {
348+
fmt.Fprintf(opts.IO.ErrOut, "%s warning: not posible to fast-forward to: %q\n", cs.WarningIcon(), branchToSwitchTo)
349+
}
340350
}
341351

342352
if err := git.DeleteLocalBranch(pr.HeadRefName); err != nil {
@@ -365,6 +375,25 @@ func mergeRun(opts *MergeOptions) error {
365375
return nil
366376
}
367377

378+
func pullLatestChanges(opts *MergeOptions, repo ghrepo.Interface, branch string) error {
379+
remotes, err := opts.Remotes()
380+
if err != nil {
381+
return err
382+
}
383+
384+
baseRemote, err := remotes.FindByRepo(repo.RepoOwner(), repo.RepoName())
385+
if err != nil {
386+
return err
387+
}
388+
389+
err = git.Pull(baseRemote.Name, branch)
390+
if err != nil {
391+
return err
392+
}
393+
394+
return nil
395+
}
396+
368397
func mergeMethodSurvey(baseRepo *api.Repository) (PullRequestMergeMethod, error) {
369398
type mergeOption struct {
370399
title string

pkg/cmd/pr/merge/merge_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313

1414
"github.com/MakeNowJust/heredoc"
1515
"github.com/cli/cli/v2/api"
16+
"github.com/cli/cli/v2/context"
17+
"github.com/cli/cli/v2/git"
1618
"github.com/cli/cli/v2/internal/ghrepo"
1719
"github.com/cli/cli/v2/internal/run"
1820
"github.com/cli/cli/v2/pkg/cmd/pr/shared"
@@ -223,6 +225,16 @@ func runCommand(rt http.RoundTripper, branch string, isTTY bool, cli string) (*t
223225
Branch: func() (string, error) {
224226
return branch, nil
225227
},
228+
Remotes: func() (context.Remotes, error) {
229+
return []*context.Remote{
230+
{
231+
Remote: &git.Remote{
232+
Name: "origin",
233+
},
234+
Repo: ghrepo.New("OWNER", "REPO"),
235+
},
236+
}, nil
237+
},
226238
}
227239

228240
cmd := NewCmdMerge(factory, nil)
@@ -432,6 +444,7 @@ func TestPrMerge_deleteBranch(t *testing.T) {
432444
cs.Register(`git checkout master`, 0, "")
433445
cs.Register(`git rev-parse --verify refs/heads/blueberries`, 0, "")
434446
cs.Register(`git branch -D blueberries`, 0, "")
447+
cs.Register(`git pull --ff-only`, 0, "")
435448

436449
output, err := runCommand(http, "blueberries", true, `pr merge --merge --delete-branch`)
437450
if err != nil {
@@ -715,6 +728,7 @@ func TestPrMerge_alreadyMerged(t *testing.T) {
715728
cs.Register(`git checkout master`, 0, "")
716729
cs.Register(`git rev-parse --verify refs/heads/blueberries`, 0, "")
717730
cs.Register(`git branch -D blueberries`, 0, "")
731+
cs.Register(`git pull --ff-only`, 0, "")
718732

719733
as, surveyTeardown := prompt.InitAskStubber()
720734
defer surveyTeardown()
@@ -850,6 +864,7 @@ func TestPRMerge_interactiveWithDeleteBranch(t *testing.T) {
850864
cs.Register(`git checkout master`, 0, "")
851865
cs.Register(`git rev-parse --verify refs/heads/blueberries`, 0, "")
852866
cs.Register(`git branch -D blueberries`, 0, "")
867+
cs.Register(`git pull --ff-only`, 0, "")
853868

854869
as, surveyTeardown := prompt.InitAskStubber()
855870
defer surveyTeardown()

0 commit comments

Comments
 (0)