Merge branch 'forgejo' into upload_with_path_structure

This commit is contained in:
davrot 2025-01-25 21:42:30 +00:00
commit ac3aa6abcd
24 changed files with 161 additions and 30 deletions

View file

@ -195,7 +195,7 @@ func serveInstalled(ctx *cli.Context) error {
publicFilesSet.Remove(".well-known")
publicFilesSet.Remove("assets")
publicFilesSet.Remove("robots.txt")
for _, fn := range publicFilesSet.Values() {
for fn := range publicFilesSet.Seq() {
log.Error("Found legacy public asset %q in CustomPath. Please move it to %s/public/assets/%s", fn, setting.CustomPath, fn)
}
if _, err := os.Stat(filepath.Join(setting.CustomPath, "robots.txt")); err == nil {

View file

@ -715,7 +715,7 @@ func TestDisabledUserFeatures(t *testing.T) {
// no features should be disabled with a plain login type
assert.LessOrEqual(t, user.LoginType, auth.Plain)
assert.Empty(t, user_model.DisabledFeaturesWithLoginType(user).Values())
for _, f := range testValues.Values() {
for f := range testValues.Seq() {
assert.False(t, user_model.IsFeatureDisabledWithLoginType(user, f))
}
@ -724,7 +724,7 @@ func TestDisabledUserFeatures(t *testing.T) {
// all features should be disabled
assert.NotEmpty(t, user_model.DisabledFeaturesWithLoginType(user).Values())
for _, f := range testValues.Values() {
for f := range testValues.Seq() {
assert.True(t, user_model.IsFeatureDisabledWithLoginType(user, f))
}
}

View file

@ -11,7 +11,7 @@ import (
"net/http"
"os"
"path/filepath"
"sort"
"slices"
"time"
"code.gitea.io/gitea/modules/container"
@ -143,8 +143,7 @@ func (l *LayeredFS) ListFiles(name string, fileMode ...bool) ([]string, error) {
}
}
}
files := fileSet.Values()
sort.Strings(files)
files := slices.Sorted(fileSet.Seq())
return files, nil
}
@ -184,8 +183,7 @@ func listAllFiles(layers []*Layer, name string, fileMode ...bool) ([]string, err
if err := list(name); err != nil {
return nil, err
}
files := fileSet.Values()
sort.Strings(files)
files := slices.Sorted(fileSet.Seq())
return files, nil
}

View file

@ -3,6 +3,11 @@
package container
import (
"iter"
"maps"
)
type Set[T comparable] map[T]struct{}
// SetOf creates a set and adds the specified elements to it.
@ -63,3 +68,9 @@ func (s Set[T]) Values() []T {
}
return keys
}
// Seq returns a iterator over the elements in the set.
// It returns a single-use iterator.
func (s Set[T]) Seq() iter.Seq[T] {
return maps.Keys(s)
}

View file

@ -4,6 +4,7 @@
package container
import (
"slices"
"testing"
"github.com/stretchr/testify/assert"
@ -29,6 +30,14 @@ func TestSet(t *testing.T) {
assert.True(t, s.Contains("key4"))
assert.True(t, s.Contains("key5"))
values := s.Values()
called := 0
for value := range s.Seq() {
called++
assert.True(t, slices.Contains(values, value))
}
assert.EqualValues(t, len(values), called)
s = SetOf("key6", "key7")
assert.False(t, s.Contains("key1"))
assert.True(t, s.Contains("key6"))

View file

@ -77,6 +77,12 @@ func newFilePreview(ctx *RenderContext, node *html.Node, locale translation.Loca
commitSha := node.Data[m[4]:m[5]]
filePath := node.Data[m[6]:m[7]]
urlFullSource := urlFull
if strings.HasSuffix(filePath, "?display=source") {
filePath = strings.TrimSuffix(filePath, "?display=source")
} else if Type(filePath) != "" {
urlFullSource = node.Data[m[0]:m[6]] + filePath + "?display=source#" + node.Data[m[8]:m[1]]
}
hash := node.Data[m[8]:m[9]]
preview.start = m[0]
@ -113,7 +119,7 @@ func newFilePreview(ctx *RenderContext, node *html.Node, locale translation.Loca
titleBuffer.WriteString(" – ")
}
err = html.Render(titleBuffer, createLink(urlFull, filePath, "muted"))
err = html.Render(titleBuffer, createLink(urlFullSource, filePath, "muted"))
if err != nil {
log.Error("failed to render filepathLink: %v", err)
}

View file

@ -1026,4 +1026,107 @@ func TestRender_FilePreview(t *testing.T) {
localMetas,
)
})
commitFileURL := util.URLJoin(markup.TestRepoURL, "src", "commit", "c9913120ed2c1e27c1d7752ecdb7a504dc7cf6be", "path", "to", "file.md")
t.Run("rendered file with ?display=source", func(t *testing.T) {
testRender(
commitFileURL+"?display=source"+"#L1-L2",
`<p></p>`+
`<div class="file-preview-box">`+
`<div class="header">`+
`<div>`+
`<a href="http://localhost:3000/gogits/gogs/src/commit/c9913120ed2c1e27c1d7752ecdb7a504dc7cf6be/path/to/file.md?display=source#L1-L2" class="muted" rel="nofollow">path/to/file.md</a>`+
`</div>`+
`<span class="text small grey">`+
`Lines 1 to 2 in <a href="http://localhost:3000/gogits/gogs/src/commit/c9913120ed2c1e27c1d7752ecdb7a504dc7cf6be" class="text black" rel="nofollow">c991312</a>`+
`</span>`+
`</div>`+
`<div class="ui table">`+
`<table class="file-preview">`+
`<tbody>`+
`<tr>`+
`<td class="lines-num"><span data-line-number="1"></span></td>`+
`<td class="lines-code chroma"><code class="code-inner"><span class="gh"># A`+"\n"+`</span></code></td>`+
`</tr>`+
`<tr>`+
`<td class="lines-num"><span data-line-number="2"></span></td>`+
`<td class="lines-code chroma"><code class="code-inner"><span class="gh"></span>B`+"\n"+`</code></td>`+
`</tr>`+
`</tbody>`+
`</table>`+
`</div>`+
`</div>`+
`<p></p>`,
localMetas,
)
})
t.Run("rendered file without ?display=source", func(t *testing.T) {
testRender(
commitFileURL+"#L1-L2",
`<p></p>`+
`<div class="file-preview-box">`+
`<div class="header">`+
`<div>`+
`<a href="http://localhost:3000/gogits/gogs/src/commit/c9913120ed2c1e27c1d7752ecdb7a504dc7cf6be/path/to/file.md?display=source#L1-L2" class="muted" rel="nofollow">path/to/file.md</a>`+
`</div>`+
`<span class="text small grey">`+
`Lines 1 to 2 in <a href="http://localhost:3000/gogits/gogs/src/commit/c9913120ed2c1e27c1d7752ecdb7a504dc7cf6be" class="text black" rel="nofollow">c991312</a>`+
`</span>`+
`</div>`+
`<div class="ui table">`+
`<table class="file-preview">`+
`<tbody>`+
`<tr>`+
`<td class="lines-num"><span data-line-number="1"></span></td>`+
`<td class="lines-code chroma"><code class="code-inner"><span class="gh"># A`+"\n"+`</span></code></td>`+
`</tr>`+
`<tr>`+
`<td class="lines-num"><span data-line-number="2"></span></td>`+
`<td class="lines-code chroma"><code class="code-inner"><span class="gh"></span>B`+"\n"+`</code></td>`+
`</tr>`+
`</tbody>`+
`</table>`+
`</div>`+
`</div>`+
`<p></p>`,
localMetas,
)
})
commitFileURL = util.URLJoin(markup.TestRepoURL, "src", "commit", "190d9492934af498c3f669d6a2431dc5459e5b20", "path", "to", "file.go")
t.Run("normal file with ?display=source", func(t *testing.T) {
testRender(
commitFileURL+"?display=source"+"#L2-L3",
`<p></p>`+
`<div class="file-preview-box">`+
`<div class="header">`+
`<div>`+
`<a href="http://localhost:3000/gogits/gogs/src/commit/190d9492934af498c3f669d6a2431dc5459e5b20/path/to/file.go?display=source#L2-L3" class="muted" rel="nofollow">path/to/file.go</a>`+
`</div>`+
`<span class="text small grey">`+
`Lines 2 to 3 in <a href="http://localhost:3000/gogits/gogs/src/commit/190d9492934af498c3f669d6a2431dc5459e5b20" class="text black" rel="nofollow">190d949</a>`+
`</span>`+
`</div>`+
`<div class="ui table">`+
`<table class="file-preview">`+
`<tbody>`+
`<tr>`+
`<td class="lines-num"><span data-line-number="2"></span></td>`+
`<td class="lines-code chroma"><code class="code-inner"><span class="nx">B</span>`+"\n"+`</code></td>`+
`</tr>`+
`<tr>`+
`<td class="lines-num"><span data-line-number="3"></span></td>`+
`<td class="lines-code chroma"><code class="code-inner"><span class="nx">C</span>`+"\n"+`</code></td>`+
`</tr>`+
`</tbody>`+
`</table>`+
`</div>`+
`</div>`+
`<p></p>`,
localMetas,
)
})
}

View file

@ -0,0 +1 @@
x•ŽKŠ1@]çµ$¿J¥aæz€JRÁ@w+éØsýõ®ÞâñàåÛ²´ÖÛÃè"@VL&J3%f-ÑGDÒq2>FçjBOEݹË:ÀgÃ\1¤œ¦ê¦kÀêªEM6DÔ,Ÿ\‚âǸÞ:\6é¾OülmÈ©­;Ï­|ƒ!GäŒE£6Z«üzòY¥Î² ¨m¸üÂÿi.x-o³ò"›úŒLÌ

View file

@ -1 +1 @@
4c1aaf56bcb9f39dcf65f3f250726850aed13cd6
c9913120ed2c1e27c1d7752ecdb7a504dc7cf6be

View file

@ -140,6 +140,11 @@ func CompileEmailGlobList(sec ConfigSection, keys ...string) (globs []glob.Glob)
return globs
}
// LoadServiceSetting loads the service settings
func LoadServiceSetting() {
loadServiceFrom(CfgProvider)
}
func loadServiceFrom(rootCfg ConfigProvider) {
sec := rootCfg.Section("service")
Service.ActiveCodeLives = sec.Key("ACTIVE_CODE_LIVE_MINUTES").MustInt(180)

View file

@ -4,7 +4,6 @@
package util
import (
"cmp"
"slices"
"strings"
)
@ -47,13 +46,6 @@ func SliceRemoveAll[T comparable](slice []T, target T) []T {
return slices.DeleteFunc(slice, func(t T) bool { return t == target })
}
// Sorted returns the sorted slice
// Note: The parameter is sorted inline.
func Sorted[S ~[]E, E cmp.Ordered](values S) S {
slices.Sort(values)
return values
}
// TODO: Replace with "maps.Values" once available, current it only in golang.org/x/exp/maps but not in standard library
func ValuesOfMap[K comparable, V any](m map[K]V) []V {
values := make([]V, 0, len(m))

View file

@ -644,6 +644,7 @@ team_name_been_taken = The team name is already taken.
team_no_units_error = Allow access to at least one repository section.
email_been_used = The email address is already used.
email_invalid = The email address is invalid.
email_domain_is_not_allowed = The domain of the user's email address <b>%s</b> conflicts with EMAIL_DOMAIN_ALLOWLIST or EMAIL_DOMAIN_BLOCKLIST. Make sure you have set the email address correctly.
openid_been_used = The OpenID address "%s" is already used.
username_password_incorrect = Username or password is incorrect.
password_complexity = Password does not pass complexity requirements:

View file

@ -6,6 +6,7 @@ package user
import (
"fmt"
"net/http"
"slices"
"code.gitea.io/gitea/models/db"
org_model "code.gitea.io/gitea/models/organization"
@ -23,7 +24,6 @@ import (
debian_module "code.gitea.io/gitea/modules/packages/debian"
rpm_module "code.gitea.io/gitea/modules/packages/rpm"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web"
packages_helper "code.gitea.io/gitea/routers/api/packages/helper"
shared_user "code.gitea.io/gitea/routers/web/shared/user"
@ -200,9 +200,9 @@ func ViewPackageVersion(ctx *context.Context) {
}
}
ctx.Data["Branches"] = util.Sorted(branches.Values())
ctx.Data["Repositories"] = util.Sorted(repositories.Values())
ctx.Data["Architectures"] = util.Sorted(architectures.Values())
ctx.Data["Branches"] = slices.Sorted(branches.Seq())
ctx.Data["Repositories"] = slices.Sorted(repositories.Seq())
ctx.Data["Architectures"] = slices.Sorted(architectures.Seq())
case packages_model.TypeArch:
ctx.Data["SignMail"] = fmt.Sprintf("%s@noreply.%s", ctx.Package.Owner.Name, setting.Packages.RegistryHost)
groups := make(container.Set[string])
@ -213,7 +213,7 @@ func ViewPackageVersion(ctx *context.Context) {
}
}
}
ctx.Data["Groups"] = util.Sorted(groups.Values())
ctx.Data["Groups"] = slices.Sorted(groups.Seq())
case packages_model.TypeDebian:
distributions := make(container.Set[string])
components := make(container.Set[string])
@ -232,9 +232,9 @@ func ViewPackageVersion(ctx *context.Context) {
}
}
ctx.Data["Distributions"] = util.Sorted(distributions.Values())
ctx.Data["Components"] = util.Sorted(components.Values())
ctx.Data["Architectures"] = util.Sorted(architectures.Values())
ctx.Data["Distributions"] = slices.Sorted(distributions.Seq())
ctx.Data["Components"] = slices.Sorted(components.Seq())
ctx.Data["Architectures"] = slices.Sorted(architectures.Seq())
case packages_model.TypeRpm, packages_model.TypeAlt:
groups := make(container.Set[string])
architectures := make(container.Set[string])
@ -250,8 +250,8 @@ func ViewPackageVersion(ctx *context.Context) {
}
}
ctx.Data["Groups"] = util.Sorted(groups.Values())
ctx.Data["Architectures"] = util.Sorted(architectures.Values())
ctx.Data["Groups"] = slices.Sorted(groups.Seq())
ctx.Data["Architectures"] = slices.Sorted(architectures.Seq())
}
var (

View file

@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/validation"
"xorm.io/builder"
@ -30,6 +31,8 @@ func iterateUserAccounts(ctx context.Context, each func(*user.User) error) error
// addresses would be currently facing a error due to their invalid email address.
// Ref: https://github.com/go-gitea/gitea/pull/19085 & https://github.com/go-gitea/gitea/pull/17688
func checkUserEmail(ctx context.Context, logger log.Logger, _ bool) error {
setting.LoadServiceSetting()
// We could use quirky SQL to get all users that start without a [a-zA-Z0-9], but that would mean
// DB provider-specific SQL and only works _now_. So instead we iterate through all user accounts
// and use the validation.ValidateEmail function to be future-proof.
@ -61,6 +64,8 @@ func checkUserEmail(ctx context.Context, logger log.Logger, _ bool) error {
// are allowed for various reasons. This check helps with detecting users that, according
// to our reserved names, don't have a valid username.
func checkUserName(ctx context.Context, logger log.Logger, _ bool) error {
setting.LoadServiceSetting()
var invalidUserCount int64
if err := iterateUserAccounts(ctx, func(u *user.User) error {
if err := user.IsUsableUsername(u.Name); err != nil {

View file

@ -711,7 +711,7 @@ func buildRelease(ctx context.Context, pv *packages_model.PackageVersion, pfs []
architectures.Add(pd.FileMetadata.Architecture)
}
for architecture := range architectures {
for architecture := range architectures.Seq() {
version := time.Now().Unix()
label := setting.AppName
data := fmt.Sprintf(`Archive: Alt Linux Team

View file

@ -372,7 +372,7 @@ func UpdateRelease(ctx context.Context, doer *user_model.User, gitRepo *git.Repo
return err
}
for _, uuid := range delAttachmentUUIDs.Values() {
for uuid := range delAttachmentUUIDs.Seq() {
if err := storage.Attachments.Delete(repo_model.AttachmentRelativePath(uuid)); err != nil {
// Even delete files failed, but the attachments has been removed from database, so we
// should not return error but only record the error on logs.