Include the port in the git-credential host field · Entire

Include the port in the git-credential host field

8bccb56main· Soph·1mo ago·2 files·+20 added/-1 removed

credentialInput emitted host=, dropping any non-default port. gitcredentials(7) defines host as "host[:port]", so on a non-default-port remote the fill query never matched the stored entry and approve/reject wrote to the default-port entry instead. Emit ep.Host (which keeps the port); the empty-host guard still uses Hostname().

Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com

Sessions

a384992be26eView transcript

Changes

2

176 unmodified lines

177
178
179
180
180
181
182
183
184
185
186

176 unmodified lines

return ""
    }
    var b strings.Builder
    fmt.Fprintf(&b, "protocol=%s\nhost=%s\n", ep.Scheme, ep.Hostname())
    // gitcredentials(7) defines host as "host[:port]" — use ep.Host, which
    // keeps any non-default port. ep.Hostname() drops it, which would store
    // and look up credentials under the default-port entry instead.
    fmt.Fprintf(&b, "protocol=%s\nhost=%s\n", ep.Scheme, ep.Host)
    if path := strings.TrimPrefix(ep.Path, "/"); path != "" {
        fmt.Fprintf(&b, "path=%s\n", path)
    }

Minternal/auth/auth.go+4/-1

161 unmodified lines

162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183

161 unmodified lines

}
}

// A non-default port must appear in the host field (gitcredentials(7) host is
// "host[:port]"); otherwise credentials are stored/looked up under the wrong
// (default-port) entry.
func TestCredentialInput_IncludesPort(t *testing.T) {
    ep := &url.URL{
        Scheme: "https",
        Host:   "example.com:8443",
        Path:   "/repo.git",
    }
    got := credentialInput(ep, "", "")
    want := "protocol=https\nhost=example.com:8443\npath=repo.git\n\n"
    if got != want {
        t.Errorf("credentialInput returned:\n%q\nwant:\n%q", got, want)
    }
}

func TestCredentialInput_ApproveRejectFormatIncludesUserAndPassword(t *testing.T) {
    ep := &url.URL{
        Scheme: "https",