pr merge: pull after switching branches (#4748)

This commit is contained in:
Parth 2021-11-29 12:26:35 -05:00 committed by GitHub
parent 31010f88d1
commit c987c5711d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 0 deletions

View file

@ -307,6 +307,19 @@ func CheckoutBranch(branch string) error {
return run.PrepareCmd(configCmd).Run()
}
// pull changes from remote branch without version history
func Pull(remote, branch string) error {
pullCmd, err := GitCommand("pull", "--ff-only", remote, branch)
if err != nil {
return err
}
pullCmd.Stdout = os.Stdout
pullCmd.Stderr = os.Stderr
pullCmd.Stdin = os.Stdin
return run.PrepareCmd(pullCmd).Run()
}
func parseCloneArgs(extraArgs []string) (args []string, target string) {
args = extraArgs

View file

@ -8,8 +8,10 @@ import (
"github.com/AlecAivazis/survey/v2"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/context"
"github.com/cli/cli/v2/git"
"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/cmd/pr/shared"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
@ -26,6 +28,7 @@ type MergeOptions struct {
HttpClient func() (*http.Client, error)
IO *iostreams.IOStreams
Branch func() (string, error)
Remotes func() (context.Remotes, error)
Finder shared.PRFinder
@ -51,6 +54,7 @@ func NewCmdMerge(f *cmdutil.Factory, runF func(*MergeOptions) error) *cobra.Comm
IO: f.IOStreams,
HttpClient: f.HttpClient,
Branch: f.Branch,
Remotes: f.Remotes,
}
var (
@ -333,10 +337,16 @@ func mergeRun(opts *MergeOptions) error {
if err != nil {
return err
}
err = git.CheckoutBranch(branchToSwitchTo)
if err != nil {
return err
}
err := pullLatestChanges(opts, baseRepo, branchToSwitchTo)
if err != nil {
fmt.Fprintf(opts.IO.ErrOut, "%s warning: not posible to fast-forward to: %q\n", cs.WarningIcon(), branchToSwitchTo)
}
}
if err := git.DeleteLocalBranch(pr.HeadRefName); err != nil {
@ -365,6 +375,25 @@ func mergeRun(opts *MergeOptions) error {
return nil
}
func pullLatestChanges(opts *MergeOptions, repo ghrepo.Interface, branch string) error {
remotes, err := opts.Remotes()
if err != nil {
return err
}
baseRemote, err := remotes.FindByRepo(repo.RepoOwner(), repo.RepoName())
if err != nil {
return err
}
err = git.Pull(baseRemote.Name, branch)
if err != nil {
return err
}
return nil
}
func mergeMethodSurvey(baseRepo *api.Repository) (PullRequestMergeMethod, error) {
type mergeOption struct {
title string

View file

@ -13,6 +13,8 @@ import (
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/context"
"github.com/cli/cli/v2/git"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/internal/run"
"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
Branch: func() (string, error) {
return branch, nil
},
Remotes: func() (context.Remotes, error) {
return []*context.Remote{
{
Remote: &git.Remote{
Name: "origin",
},
Repo: ghrepo.New("OWNER", "REPO"),
},
}, nil
},
}
cmd := NewCmdMerge(factory, nil)
@ -432,6 +444,7 @@ func TestPrMerge_deleteBranch(t *testing.T) {
cs.Register(`git checkout master`, 0, "")
cs.Register(`git rev-parse --verify refs/heads/blueberries`, 0, "")
cs.Register(`git branch -D blueberries`, 0, "")
cs.Register(`git pull --ff-only`, 0, "")
output, err := runCommand(http, "blueberries", true, `pr merge --merge --delete-branch`)
if err != nil {
@ -715,6 +728,7 @@ func TestPrMerge_alreadyMerged(t *testing.T) {
cs.Register(`git checkout master`, 0, "")
cs.Register(`git rev-parse --verify refs/heads/blueberries`, 0, "")
cs.Register(`git branch -D blueberries`, 0, "")
cs.Register(`git pull --ff-only`, 0, "")
as, surveyTeardown := prompt.InitAskStubber()
defer surveyTeardown()
@ -850,6 +864,7 @@ func TestPRMerge_interactiveWithDeleteBranch(t *testing.T) {
cs.Register(`git checkout master`, 0, "")
cs.Register(`git rev-parse --verify refs/heads/blueberries`, 0, "")
cs.Register(`git branch -D blueberries`, 0, "")
cs.Register(`git pull --ff-only`, 0, "")
as, surveyTeardown := prompt.InitAskStubber()
defer surveyTeardown()