Commit 2e0ff1095d for golang

commit 2e0ff1095d0d07be949abda0aa74aed2f7308d06
Author: yh2237 <hi@yh.contact>
Date:   Sat Nov 28 21:58:09 2026 +0000

    cmd/go/internal/auth: run 'git credential approve/reject' in GOAUTH dir

    With GOAUTH=git, runGitAuth runs 'git credential fill' in the directory
    given in GOAUTH to avoid picking up configuration from an arbitrary
    repository, as discussed in go.dev/issue/29230. However, the follow-up
    'git credential approve' or 'git credential reject' command started by
    updateGitCredentialHelper did not set cmd.Dir, so it ran in the current
    directory and used whatever credential helpers that directory's git
    configuration specifies, passing them the credential obtained from
    the helper in the GOAUTH directory.

    Pass the directory down and run git there as well.

    Fixes #83290

    Change-Id: Iad48c39b67a26da637dcc32f21e4ff512dee197d
    GitHub-Last-Rev: 4e5d2fbc956df825102f394076e053f745950b28
    GitHub-Pull-Request: golang/go#83287
    Reviewed-on: https://go-review.googlesource.com/c/go/+/865010
    Auto-Submit: Sean Liao <sean@liao.dev>
    LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
    Reviewed-by: David Chase <drchase@google.com>
    Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
    Reviewed-by: Sean Liao <sean@liao.dev>

diff --git a/src/cmd/go/internal/auth/gitauth.go b/src/cmd/go/internal/auth/gitauth.go
index f11cd2fbf0..eec11b9220 100644
--- a/src/cmd/go/internal/auth/gitauth.go
+++ b/src/cmd/go/internal/auth/gitauth.go
@@ -70,7 +70,7 @@ func runGitAuth(client *http.Client, dir, url string) (string, http.Header, erro
 	// The request is intercepted for testing purposes to simulate interactions
 	// with the credential helper.
 	intercept.Request(req)
-	go updateGitCredentialHelper(client, req, out)
+	go updateGitCredentialHelper(client, req, dir, out)

 	// Return the parsed prefix and headers, even if credential validation fails.
 	// The caller is responsible for the primary validation.
@@ -119,7 +119,7 @@ func parseGitAuth(data []byte) (parsedPrefix, username, password string) {
 // updateGitCredentialHelper validates the given credentials by sending a HEAD request
 // and updates the git credential helper's cache accordingly. It retries the
 // request up to maxTries times.
-func updateGitCredentialHelper(client *http.Client, req *http.Request, credentialOutput []byte) {
+func updateGitCredentialHelper(client *http.Client, req *http.Request, dir string, credentialOutput []byte) {
 	for range maxTries {
 		release, err := base.AcquireNet()
 		if err != nil {
@@ -133,7 +133,7 @@ func updateGitCredentialHelper(client *http.Client, req *http.Request, credentia
 		res.Body.Close()
 		release()
 		if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusUnauthorized {
-			approveOrRejectCredential(credentialOutput, res.StatusCode == http.StatusOK)
+			approveOrRejectCredential(dir, credentialOutput, res.StatusCode == http.StatusOK)
 			break
 		}
 	}
@@ -141,12 +141,13 @@ func updateGitCredentialHelper(client *http.Client, req *http.Request, credentia

 // approveOrRejectCredential approves or rejects the provided credential using
 // 'git credential approve/reject'.
-func approveOrRejectCredential(credentialOutput []byte, approve bool) {
+func approveOrRejectCredential(dir string, credentialOutput []byte, approve bool) {
 	action := "reject"
 	if approve {
 		action = "approve"
 	}
 	cmd := exec.Command("git", "credential", action)
+	cmd.Dir = dir
 	cmd.Stdin = bytes.NewReader(credentialOutput)
 	cmd.Run() // ignore error
 }
diff --git a/src/cmd/go/internal/auth/gitauth_test.go b/src/cmd/go/internal/auth/gitauth_test.go
index 2ba93ad2c2..78ce8f1db0 100644
--- a/src/cmd/go/internal/auth/gitauth_test.go
+++ b/src/cmd/go/internal/auth/gitauth_test.go
@@ -5,10 +5,48 @@
 package auth

 import (
+	"internal/testenv"
+	"os"
+	"os/exec"
+	"path/filepath"
 	"strings"
 	"testing"
 )

+// TestApproveOrRejectCredentialDir checks that 'git credential approve'
+// runs in the GOAUTH=git directory rather than in the current directory.
+func TestApproveOrRejectCredentialDir(t *testing.T) {
+	testenv.MustHaveExecPath(t, "git")
+
+	// Ignore credential helpers from the user's or the system's configuration.
+	t.Setenv("GIT_CONFIG_GLOBAL", filepath.Join(t.TempDir(), "gitconfig"))
+	t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
+
+	dir := t.TempDir()
+	store := filepath.Join(t.TempDir(), "git-credentials")
+	for _, args := range [][]string{
+		{"init"},
+		{"config", "credential.helper", "store --file=" + filepath.ToSlash(store)},
+	} {
+		cmd := exec.Command("git", args...)
+		cmd.Dir = dir
+		if out, err := cmd.CombinedOutput(); err != nil {
+			t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out)
+		}
+	}
+
+	credential := "protocol=https\nhost=example.com\nusername=bob\npassword=secr3t\n"
+	approveOrRejectCredential(dir, []byte(credential), true)
+
+	data, err := os.ReadFile(store)
+	if err != nil {
+		t.Fatalf("credential helper in %s was not used: %v", dir, err)
+	}
+	if got, want := strings.TrimSpace(string(data)), "https://bob:secr3t@example.com"; got != want {
+		t.Errorf("stored credential = %q, want %q", got, want)
+	}
+}
+
 func TestParseGitAuth(t *testing.T) {
 	testCases := []struct {
 		gitauth      string // contents of 'git credential fill'