From 5004ba2b9172487d1966301447217e780f093c09 Mon Sep 17 00:00:00 2001 From: David Livingston Date: Sun, 16 Feb 2025 16:40:46 +0800 Subject: [PATCH 1/2] update default upstream when forking repo update default upstream when forking repo remove unnecessary variable --- pkg/cmd/pr/create/create.go | 11 ++++++++++- pkg/cmd/pr/create/create_test.go | 3 ++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/pr/create/create.go b/pkg/cmd/pr/create/create.go index b2abe0938..2badeda0f 100644 --- a/pkg/cmd/pr/create/create.go +++ b/pkg/cmd/pr/create/create.go @@ -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() diff --git a/pkg/cmd/pr/create/create_test.go b/pkg/cmd/pr/create/create_test.go index 6df3f9880..4d7c294db 100644 --- a/pkg/cmd/pr/create/create_test.go +++ b/pkg/cmd/pr/create/create_test.go @@ -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", From af99c9d49572f934af98cd3215e51107a1600174 Mon Sep 17 00:00:00 2001 From: Andy Feller Date: Tue, 18 Feb 2025 16:02:50 -0500 Subject: [PATCH 2/2] Suggestions to enhance upstream default handling This commit brings this code more into alignment with other places: 1. Using a local variable for upstream remote name instead of hardcoding everywhere 2. Raising error raised by setting default remote 3. Removing condition for displaying upstream default message 4. Bring message logic more inline with other places within function --- pkg/cmd/pr/create/create.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/pkg/cmd/pr/create/create.go b/pkg/cmd/pr/create/create.go index 2badeda0f..0066bbc6e 100644 --- a/pkg/cmd/pr/create/create.go +++ b/pkg/cmd/pr/create/create.go @@ -977,7 +977,8 @@ func handlePush(opts CreateOptions, ctx CreateContext) error { headRepoURL := ghrepo.FormatRemoteURL(headRepo, cloneProtocol) gitClient := ctx.GitClient origin, _ := remotes.FindByName("origin") - upstream, _ := remotes.FindByName("upstream") + upstreamName := "upstream" + upstream, _ := remotes.FindByName(upstreamName) remoteName := "origin" if origin != nil { @@ -985,7 +986,7 @@ func handlePush(opts CreateOptions, ctx CreateContext) error { } if origin != nil && upstream == nil && ghrepo.IsSame(origin, ctx.BaseRepo) { - renameCmd, err := gitClient.Command(context.Background(), "remote", "rename", "origin", "upstream") + renameCmd, err := gitClient.Command(context.Background(), "remote", "rename", "origin", upstreamName) if err != nil { return err } @@ -993,7 +994,7 @@ func handlePush(opts CreateOptions, ctx CreateContext) error { return fmt.Errorf("error renaming origin remote: %w", err) } remoteName = "origin" - fmt.Fprintf(opts.IO.ErrOut, "Changed %s remote to %q\n", ghrepo.FullName(ctx.BaseRepo), "upstream") + fmt.Fprintf(opts.IO.ErrOut, "Changed %s remote to %q\n", ghrepo.FullName(ctx.BaseRepo), upstreamName) } gitRemote, err := gitClient.AddRemote(context.Background(), remoteName, headRepoURL, []string{}) @@ -1003,12 +1004,16 @@ func handlePush(opts CreateOptions, ctx CreateContext) error { fmt.Fprintf(opts.IO.ErrOut, "Added %s as remote %q\n", ghrepo.FullName(headRepo), remoteName) + // Only mark `upstream` remote as default if `gh pr create` created the remote. if didForkRepo { - gitClient.SetRemoteResolution(context.Background(), "upstream", "base") - if opts.IO.IsStdinTTY() && opts.IO.IsStdoutTTY() { + err := gitClient.SetRemoteResolution(context.Background(), upstreamName, "base") + if err != nil { + return fmt.Errorf("error setting upstream as default: %w", err) + } + + if 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))) + fmt.Fprintf(opts.IO.ErrOut, "%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))) } }