update default upstream when forking repo

update default upstream when forking repo

remove unnecessary variable
This commit is contained in:
David Livingston 2025-02-16 16:40:46 +08:00
parent b642da26d0
commit 5004ba2b91
2 changed files with 12 additions and 2 deletions

View file

@ -935,6 +935,7 @@ func handlePush(opts CreateOptions, ctx CreateContext) error {
headRepo := ctx.HeadRepo
headRemote := ctx.HeadRemote
client := ctx.Client
gitClient := ctx.GitClient
var err error
// if a head repository could not be determined so far, automatically create
@ -1002,6 +1003,15 @@ func handlePush(opts CreateOptions, ctx CreateContext) error {
fmt.Fprintf(opts.IO.ErrOut, "Added %s as remote %q\n", ghrepo.FullName(headRepo), remoteName)
if didForkRepo {
gitClient.SetRemoteResolution(context.Background(), "upstream", "base")
if opts.IO.IsStdinTTY() && opts.IO.IsStdoutTTY() {
cs := opts.IO.ColorScheme()
stderr := opts.IO.ErrOut
fmt.Fprintf(stderr, "%s Repository %s set as the default repository. To learn more about the default repository, run: gh repo set-default --help\n", cs.WarningIcon(), cs.Bold(ghrepo.FullName(headRepo)))
}
}
headRemote = &ghContext.Remote{
Remote: gitRemote,
Repo: headRepo,
@ -1013,7 +1023,6 @@ func handlePush(opts CreateOptions, ctx CreateContext) error {
pushBranch := func() error {
w := NewRegexpWriter(opts.IO.ErrOut, gitPushRegexp, "")
defer w.Flush()
gitClient := ctx.GitClient
ref := fmt.Sprintf("HEAD:refs/heads/%s", ctx.HeadBranch)
bo := backoff.NewConstantBackOff(2 * time.Second)
ctx := context.Background()

View file

@ -798,6 +798,7 @@ func Test_createRun(t *testing.T) {
cs.Register("git remote rename origin upstream", 0, "")
cs.Register(`git remote add origin https://github.com/monalisa/REPO.git`, 0, "")
cs.Register(`git push --set-upstream origin HEAD:refs/heads/feature`, 0, "")
cs.Register(`git config --add remote.upstream.gh-resolved base`, 0, "")
},
promptStubs: func(pm *prompter.PrompterMock) {
pm.SelectFunc = func(p, _ string, opts []string) (int, error) {
@ -809,7 +810,7 @@ func Test_createRun(t *testing.T) {
}
},
expectedOut: "https://github.com/OWNER/REPO/pull/12\n",
expectedErrOut: "\nCreating pull request for monalisa:feature into master in OWNER/REPO\n\nChanged OWNER/REPO remote to \"upstream\"\nAdded monalisa/REPO as remote \"origin\"\n",
expectedErrOut: "\nCreating pull request for monalisa:feature into master in OWNER/REPO\n\nChanged OWNER/REPO remote to \"upstream\"\nAdded monalisa/REPO as remote \"origin\"\n! Repository monalisa/REPO set as the default repository. To learn more about the default repository, run: gh repo set-default --help\n",
},
{
name: "pushed to non base repo",