diff --git a/git/client.go b/git/client.go index 908ee8c4f33..43568839afa 100644 --- a/git/client.go +++ b/git/client.go @@ -20,6 +20,9 @@ import ( "github.com/cli/safeexec" ) +// MergeBaseConfig is the configuration setting to keep track of the PR target branch. +const MergeBaseConfig = "gh-merge-base" + var remoteRE = regexp.MustCompile(`(.+)\s+(.+)\s+\((push|fetch)\)`) // This regexp exists to match lines of the following form: @@ -376,7 +379,7 @@ func (c *Client) lookupCommit(ctx context.Context, sha, format string) ([]byte, // ReadBranchConfig parses the `branch.BRANCH.(remote|merge)` part of git config. func (c *Client) ReadBranchConfig(ctx context.Context, branch string) (cfg BranchConfig) { prefix := regexp.QuoteMeta(fmt.Sprintf("branch.%s.", branch)) - args := []string{"config", "--get-regexp", fmt.Sprintf("^%s(remote|merge|gh-merge-base)$", prefix)} + args := []string{"config", "--get-regexp", fmt.Sprintf("^%s(remote|merge|%s)$", prefix, MergeBaseConfig)} cmd, err := c.Command(ctx, args...) if err != nil { return @@ -406,13 +409,26 @@ func (c *Client) ReadBranchConfig(ctx context.Context, branch string) (cfg Branc } case "merge": cfg.MergeRef = parts[1] - case "gh-merge-base": + case MergeBaseConfig: cfg.MergeBase = parts[1] } } return } +// SetBranchConfig sets the named value on the given branch. +func (c *Client) SetBranchConfig(ctx context.Context, branch, name, value string) error { + name = fmt.Sprintf("branch.%s.%s", branch, name) + args := []string{"config", name, value} + cmd, err := c.Command(ctx, args...) + if err != nil { + return err + } + // No output expected but check for any printed git error. + _, err = cmd.Output() + return err +} + func (c *Client) DeleteLocalTag(ctx context.Context, tag string) error { args := []string{"tag", "-d", tag} cmd, err := c.Command(ctx, args...) diff --git a/pkg/cmd/issue/develop/develop.go b/pkg/cmd/issue/develop/develop.go index fa01a3dacbf..bbffa60acea 100644 --- a/pkg/cmd/issue/develop/develop.go +++ b/pkg/cmd/issue/develop/develop.go @@ -171,6 +171,14 @@ func developRunCreate(opts *DevelopOptions, apiClient *api.Client, issueRepo ghr return err } + // Remember which branch to target when creating a PR. + if opts.BaseBranch != "" { + err = opts.GitClient.SetBranchConfig(ctx.Background(), branchName, git.MergeBaseConfig, opts.BaseBranch) + if err != nil { + return err + } + } + fmt.Fprintf(opts.IO.Out, "%s/%s/tree/%s\n", branchRepo.RepoHost(), ghrepo.FullName(branchRepo), branchName) return checkoutBranch(opts, branchRepo, branchName) diff --git a/pkg/cmd/issue/develop/develop_test.go b/pkg/cmd/issue/develop/develop_test.go index abdebf0c8e9..831f03fc3bd 100644 --- a/pkg/cmd/issue/develop/develop_test.go +++ b/pkg/cmd/issue/develop/develop_test.go @@ -399,6 +399,7 @@ func TestDevelopRun(t *testing.T) { }, runStubs: func(cs *run.CommandStubber) { cs.Register(`git fetch origin \+refs/heads/my-branch:refs/remotes/origin/my-branch`, 0, "") + cs.Register(`git config branch\.my-branch\.gh-merge-base main`, 0, "") }, expectedOut: "github.com/OWNER/REPO/tree/my-branch\n", },