Make git clone URL could use current signed-in user (#33091)

close #33086

* Add a special value for "SSH_USER" setting: `(DOER_USERNAME)`
* Improve parseRepositoryURL and add tests (now it doesn't have hard
dependency on some setting values)

Many changes are just adding "ctx" and "doer" argument to functions.

By the way, improve app.example.ini, remove all `%(key)s` syntax, it
only makes messy and no user really cares about it.

Document: https://gitea.com/gitea/docs/pulls/138
This commit is contained in:
wxiaoguang 2025-01-07 13:17:44 +08:00 committed by GitHub
parent 98637fe76e
commit 34dfc25b83
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 273 additions and 143 deletions

View file

@ -39,7 +39,7 @@ func GetRemoteURL(ctx context.Context, repoPath, remoteName string) (*giturl.Git
if err != nil {
return nil, err
}
return giturl.Parse(addr)
return giturl.ParseGitURL(addr)
}
// ErrInvalidCloneAddr represents a "InvalidCloneAddr" kind of error.

View file

@ -21,7 +21,7 @@ func (err ErrWrongURLFormat) Error() string {
// GitURL represents a git URL
type GitURL struct {
*stdurl.URL
extraMark int // 0 no extra 1 scp 2 file path with no prefix
extraMark int // 0: standard URL with scheme, 1: scp short syntax (no scheme), 2: file path with no prefix
}
// String returns the URL's string
@ -38,8 +38,11 @@ func (u *GitURL) String() string {
}
}
// Parse parse all kinds of git URL
func Parse(remote string) (*GitURL, error) {
// ParseGitURL parse all kinds of git URL:
// * Full URL: http://git@host/path, http://git@host:port/path
// * SCP short syntax: git@host:/path
// * File path: /dir/repo/path
func ParseGitURL(remote string) (*GitURL, error) {
if strings.Contains(remote, "://") {
u, err := stdurl.Parse(remote)
if err != nil {

View file

@ -157,7 +157,7 @@ func TestParseGitURLs(t *testing.T) {
for _, kase := range kases {
t.Run(kase.kase, func(t *testing.T) {
u, err := Parse(kase.kase)
u, err := ParseGitURL(kase.kase)
assert.NoError(t, err)
assert.EqualValues(t, kase.expected.extraMark, u.extraMark)
assert.EqualValues(t, *kase.expected, *u)

View file

@ -5,6 +5,7 @@ package httplib
import (
"context"
"net"
"net/http"
"net/url"
"strings"
@ -81,6 +82,12 @@ func GuessCurrentHostURL(ctx context.Context) string {
return reqScheme + "://" + req.Host
}
func GuessCurrentHostDomain(ctx context.Context) string {
_, host, _ := strings.Cut(GuessCurrentHostURL(ctx), "://")
domain, _, _ := net.SplitHostPort(host)
return util.IfZero(domain, host)
}
// MakeAbsoluteURL tries to make a link to an absolute URL:
// * If link is empty, it returns the current app URL.
// * If link is absolute, it returns the link.
@ -105,7 +112,7 @@ func IsCurrentGiteaSiteURL(ctx context.Context, s string) bool {
if cleanedPath == "" || cleanedPath == "." {
u.Path = "/"
} else {
u.Path += "/" + cleanedPath + "/"
u.Path = "/" + cleanedPath + "/"
}
}
if urlIsRelative(s, u) {

View file

@ -150,7 +150,7 @@ func mirrorRemoteAddress(ctx context.Context, m *repo_model.Repository, remoteNa
return ret
}
u, err := giturl.Parse(remoteURL)
u, err := giturl.ParseGitURL(remoteURL)
if err != nil {
log.Error("giturl.Parse %v", err)
return ret