Skip to content

Commit

Permalink
Merge branch 'main' into repo-common-jquery-ajax
Browse files Browse the repository at this point in the history
  • Loading branch information
yardenshoham authored Feb 25, 2024
2 parents be2252c + f4b9257 commit 9bb9b1d
Show file tree
Hide file tree
Showing 97 changed files with 963 additions and 346 deletions.
2 changes: 1 addition & 1 deletion .stylelintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ rules:
at-rule-allowed-list: null
at-rule-disallowed-list: null
at-rule-empty-line-before: null
at-rule-no-unknown: true
at-rule-no-unknown: [true, {ignoreAtRules: [tailwind]}]
at-rule-no-vendor-prefix: true
at-rule-property-required-list: null
block-no-empty: true
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ GO_TEST_PACKAGES ?= $(filter-out $(shell $(GO) list code.gitea.io/gitea/models/m
FOMANTIC_WORK_DIR := web_src/fomantic

WEBPACK_SOURCES := $(shell find web_src/js web_src/css -type f)
WEBPACK_CONFIGS := webpack.config.js
WEBPACK_CONFIGS := webpack.config.js tailwind.config.js
WEBPACK_DEST := public/assets/js/index.js public/assets/css/index.css
WEBPACK_DEST_ENTRIES := public/assets/js public/assets/css public/assets/fonts public/assets/img/webpack

Expand Down
2 changes: 1 addition & 1 deletion docs/content/administration/mail-templates.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ the messages. Here's a list of some of them:
| `AppDomain` | - | Any | Gitea's host name |
| `EllipsisString` | string, int | Any | Truncates a string to the specified length; adds ellipsis as needed |
| `Str2html` | string | Body only | Sanitizes text by removing any HTML tags from it. |
| `Safe` | string | Body only | Takes the input as HTML; can be used for `.ReviewComments.RenderedContent`. |
| `SafeHTML` | string | Body only | Takes the input as HTML; can be used for `.ReviewComments.RenderedContent`. |

These are _functions_, not metadata, so they have to be used:

Expand Down
16 changes: 8 additions & 8 deletions docs/content/administration/mail-templates.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,14 @@ _主题_ 和 _邮件正文_ 由 [Golang的模板引擎](https://go.dev/pkg/text/

模板系统包含一些函数,可用于进一步处理和格式化消息。以下是其中一些函数的列表:

| 函数名 | 参数 | 可用于 | 用法 |
| ----------------- | ----------- | ------------ | --------------------------------------------------------------------------------- |
| `AppUrl` | - | 任何地方 | Gitea 的 URL |
| `AppName` | - | 任何地方 |`app.ini` 中设置,通常为 "Gitea" |
| `AppDomain` | - | 任何地方 | Gitea 的主机名 |
| `EllipsisString` | string, int | 任何地方 | 将字符串截断为指定长度;根据需要添加省略号 |
| `Str2html` | string | 仅正文部分 | 通过删除其中的 HTML 标签对文本进行清理 |
| `Safe` | string | 仅正文部分 | 将输入作为 HTML 处理;可用于 `.ReviewComments.RenderedContent` 等字段 |
| 函数名 | 参数 | 可用于 | 用法 |
|------------------| ----------- | ------------ | --------------------------------------------------------------------------------- |
| `AppUrl` | - | 任何地方 | Gitea 的 URL |
| `AppName` | - | 任何地方 |`app.ini` 中设置,通常为 "Gitea" |
| `AppDomain` | - | 任何地方 | Gitea 的主机名 |
| `EllipsisString` | string, int | 任何地方 | 将字符串截断为指定长度;根据需要添加省略号 |
| `Str2html` | string | 仅正文部分 | 通过删除其中的 HTML 标签对文本进行清理 |
| `SafeHTML` | string | 仅正文部分 | 将输入作为 HTML 处理;可用于 `.ReviewComments.RenderedContent` 等字段 |

这些都是 _函数_,而不是元数据,因此必须按以下方式使用:

Expand Down
4 changes: 4 additions & 0 deletions docs/content/contributing/guidelines-backend.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ i.e. `services/user`, `models/repository`.
Since there are some packages which use the same package name, it is possible that you find packages like `modules/user`, `models/user`, and `services/user`. When these packages are imported in one Go file, it's difficult to know which package we are using and if it's a variable name or an import name. So, we always recommend to use import aliases. To differ from package variables which are commonly in camelCase, just use **snake_case** for import aliases.
i.e. `import user_service "code.gitea.io/gitea/services/user"`

### Implementing `io.Closer`

If a type implements `io.Closer`, calling `Close` multiple times must not fail or `panic` but return an error or `nil`.

### Important Gotchas

- Never write `x.Update(exemplar)` without an explicit `WHERE` clause:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ require (
github.com/mholt/archiver/v3 v3.5.1
github.com/microcosm-cc/bluemonday v1.0.26
github.com/minio/minio-go/v7 v7.0.66
github.com/minio/sha256-simd v1.0.1
github.com/msteinert/pam v1.2.0
github.com/nektos/act v0.2.52
github.com/niklasfasching/go-org v1.7.0
Expand Down Expand Up @@ -230,6 +229,7 @@ require (
github.com/mholt/acmez v1.2.0 // indirect
github.com/miekg/dns v1.1.58 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
Expand Down
2 changes: 1 addition & 1 deletion models/auth/twofactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package auth
import (
"context"
"crypto/md5"
"crypto/sha256"
"crypto/subtle"
"encoding/base32"
"encoding/base64"
Expand All @@ -18,7 +19,6 @@ import (
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"

"github.com/minio/sha256-simd"
"github.com/pquerna/otp/totp"
"golang.org/x/crypto/pbkdf2"
)
Expand Down
2 changes: 1 addition & 1 deletion models/migrations/base/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
package base

import (
"crypto/sha256"
"encoding/hex"

"github.com/minio/sha256-simd"
"golang.org/x/crypto/pbkdf2"
)

Expand Down
2 changes: 1 addition & 1 deletion models/migrations/v1_14/v166.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
package v1_14 //nolint

import (
"crypto/sha256"
"encoding/hex"

"github.com/minio/sha256-simd"
"golang.org/x/crypto/argon2"
"golang.org/x/crypto/bcrypt"
"golang.org/x/crypto/pbkdf2"
Expand Down
2 changes: 1 addition & 1 deletion modules/auth/password/hash/pbkdf2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
package hash

import (
"crypto/sha256"
"encoding/hex"
"strings"

"code.gitea.io/gitea/modules/log"

"github.com/minio/sha256-simd"
"golang.org/x/crypto/pbkdf2"
)

Expand Down
3 changes: 1 addition & 2 deletions modules/avatar/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
package avatar

import (
"crypto/sha256"
"encoding/hex"
"strconv"

"github.com/minio/sha256-simd"
)

// HashAvatar will generate a unique string, which ensures that when there's a
Expand Down
3 changes: 1 addition & 2 deletions modules/avatar/identicon/identicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
package identicon

import (
"crypto/sha256"
"fmt"
"image"
"image/color"

"github.com/minio/sha256-simd"
)

const minImageSize = 16
Expand Down
2 changes: 1 addition & 1 deletion modules/base/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package base

import (
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
Expand All @@ -22,7 +23,6 @@ import (
"code.gitea.io/gitea/modules/setting"

"github.com/dustin/go-humanize"
"github.com/minio/sha256-simd"
)

// EncodeSha1 string to sha1 hex value.
Expand Down
4 changes: 4 additions & 0 deletions modules/git/blame.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ func (r *BlameReader) NextPart() (*BlamePart, error) {

// Close BlameReader - don't run NextPart after invoking that
func (r *BlameReader) Close() error {
if r.bufferedReader == nil {
return nil
}

err := <-r.done
r.bufferedReader = nil
_ = r.reader.Close()
Expand Down
3 changes: 1 addition & 2 deletions modules/git/last_commit_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
package git

import (
"crypto/sha256"
"fmt"

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"

"github.com/minio/sha256-simd"
)

// Cache represents a caching interface
Expand Down
7 changes: 4 additions & 3 deletions modules/git/repo_base_gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,17 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
}

// Close this repository, in particular close the underlying gogitStorage if this is not nil
func (repo *Repository) Close() (err error) {
func (repo *Repository) Close() error {
if repo == nil || repo.gogitStorage == nil {
return
return nil
}
if err := repo.gogitStorage.Close(); err != nil {
gitealog.Error("Error closing storage: %v", err)
}
repo.gogitStorage = nil
repo.LastCommitCache = nil
repo.tagCache = nil
return
return nil
}

// GoGitRepo gets the go-git repo representation
Expand Down
4 changes: 2 additions & 2 deletions modules/git/repo_base_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError
}
}

func (repo *Repository) Close() (err error) {
func (repo *Repository) Close() error {
if repo == nil {
return nil
}
Expand All @@ -123,5 +123,5 @@ func (repo *Repository) Close() (err error) {
}
repo.LastCommitCache = nil
repo.tagCache = nil
return err
return nil
}
2 changes: 1 addition & 1 deletion modules/indexer/internal/bleve/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (i *Indexer) Ping(_ context.Context) error {
}

func (i *Indexer) Close() {
if i == nil {
if i == nil || i.Indexer == nil {
return
}

Expand Down
3 changes: 0 additions & 3 deletions modules/indexer/internal/meilisearch/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,5 @@ func (i *Indexer) Close() {
if i == nil {
return
}
if i.Client == nil {
return
}
i.Client = nil
}
3 changes: 1 addition & 2 deletions modules/lfs/content_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package lfs

import (
"crypto/sha256"
"encoding/hex"
"errors"
"hash"
Expand All @@ -12,8 +13,6 @@ import (

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/storage"

"github.com/minio/sha256-simd"
)

var (
Expand Down
3 changes: 1 addition & 2 deletions modules/lfs/pointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package lfs

import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
Expand All @@ -12,8 +13,6 @@ import (
"regexp"
"strconv"
"strings"

"github.com/minio/sha256-simd"
)

const (
Expand Down
2 changes: 2 additions & 0 deletions modules/packages/alpine/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const (

RepositoryPackage = "_alpine"
RepositoryVersion = "_repository"

NoArch = "noarch"
)

// https://wiki.alpinelinux.org/wiki/Apk_spec
Expand Down
3 changes: 1 addition & 2 deletions modules/secret/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"io"

"github.com/minio/sha256-simd"
)

// AesEncrypt encrypts text and given key with AES.
Expand Down
29 changes: 24 additions & 5 deletions modules/templates/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"html"
"html/template"
"net/url"
"slices"
"strings"
"time"

Expand All @@ -34,8 +35,9 @@ func NewFuncMap() template.FuncMap {
// html/template related functions
"dict": dict, // it's lowercase because this name has been widely used. Our other functions should have uppercase names.
"Eval": Eval,
"Safe": Safe,
"Escape": Escape,
"SafeHTML": SafeHTML,
"HTMLFormat": HTMLFormat,
"HTMLEscape": HTMLEscape,
"QueryEscape": url.QueryEscape,
"JSEscape": JSEscapeSafe,
"Str2html": Str2html, // TODO: rename it to SanitizeHTML
Expand Down Expand Up @@ -177,8 +179,25 @@ func NewFuncMap() template.FuncMap {
}
}

// Safe render raw as HTML
func Safe(s any) template.HTML {
func HTMLFormat(s string, rawArgs ...any) template.HTML {
args := slices.Clone(rawArgs)
for i, v := range args {
switch v := v.(type) {
case nil, bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, template.HTML:
// for most basic types (including template.HTML which is safe), just do nothing and use it
case string:
args[i] = template.HTMLEscapeString(v)
case fmt.Stringer:
args[i] = template.HTMLEscapeString(v.String())
default:
args[i] = template.HTMLEscapeString(fmt.Sprint(v))
}
}
return template.HTML(fmt.Sprintf(s, args...))
}

// SafeHTML render raw as HTML
func SafeHTML(s any) template.HTML {
switch v := s.(type) {
case string:
return template.HTML(v)
Expand All @@ -199,7 +218,7 @@ func Str2html(s any) template.HTML {
panic(fmt.Sprintf("unexpected type %T", s))
}

func Escape(s any) template.HTML {
func HTMLEscape(s any) template.HTML {
switch v := s.(type) {
case string:
return template.HTML(html.EscapeString(v))
Expand Down
5 changes: 5 additions & 0 deletions modules/templates/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package templates

import (
"html/template"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -56,3 +57,7 @@ func TestSubjectBodySeparator(t *testing.T) {
func TestJSEscapeSafe(t *testing.T) {
assert.EqualValues(t, `\u0026\u003C\u003E\'\"`, JSEscapeSafe(`&<>'"`))
}

func TestHTMLFormat(t *testing.T) {
assert.Equal(t, template.HTML("<a>&lt; < 1</a>"), HTMLFormat("<a>%s %s %d</a>", "<", template.HTML("<"), 1))
}
1 change: 1 addition & 0 deletions modules/util/filebuffer/file_backed_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func (b *FileBackedBuffer) Close() error {
if b.file != nil {
err := b.file.Close()
os.Remove(b.file.Name())
b.file = nil
return err
}
return nil
Expand Down
Loading

0 comments on commit 9bb9b1d

Please sign in to comment.