Skip to content

Commit

Permalink
Merge remote-tracking branch 'giteaofficial/main'
Browse files Browse the repository at this point in the history
* giteaofficial/main:
  Remove the Go version in UI, add a link on Gitea Version to show config details (Go/Git version) (go-gitea#19173)
  [skip ci] Updated translations via Crowdin
  Clean paths when looking in Storage (go-gitea#19124)
  Use the new/choose link for New Issue on project page (go-gitea#19172)
  Ensure that setting.LocalURL always has a trailing slash (go-gitea#19171)
  Use `ctx` instead of `db.DefaultContext` in some packages(routers/services/modules) (go-gitea#19163)
  • Loading branch information
zjjhot committed Mar 23, 2022
2 parents f9c05a9 + 395117d commit 9ebc810
Show file tree
Hide file tree
Showing 60 changed files with 188 additions and 184 deletions.
2 changes: 1 addition & 1 deletion cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ func runChangePassword(c *cli.Context) error {
return err
}

if err = user_model.UpdateUserCols(db.DefaultContext, user, "passwd", "passwd_hash_algo", "salt"); err != nil {
if err = user_model.UpdateUserCols(ctx, user, "passwd", "passwd_hash_algo", "salt"); err != nil {
return err
}

Expand Down
7 changes: 3 additions & 4 deletions modules/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"strings"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
unit_model "code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
Expand Down Expand Up @@ -256,7 +255,7 @@ func RetrieveBaseRepo(ctx *Context, repo *repo_model.Repository) {
}
ctx.ServerError("GetBaseRepo", err)
return
} else if err = repo.BaseRepo.GetOwner(db.DefaultContext); err != nil {
} else if err = repo.BaseRepo.GetOwner(ctx); err != nil {
ctx.ServerError("BaseRepo.GetOwner", err)
return
}
Expand All @@ -273,7 +272,7 @@ func RetrieveTemplateRepo(ctx *Context, repo *repo_model.Repository) {
}
ctx.ServerError("GetTemplateRepo", err)
return
} else if err = templateRepo.GetOwner(db.DefaultContext); err != nil {
} else if err = templateRepo.GetOwner(ctx); err != nil {
ctx.ServerError("TemplateRepo.GetOwner", err)
return
}
Expand Down Expand Up @@ -341,7 +340,7 @@ func RedirectToRepo(ctx *Context, redirectRepoID int64) {

func repoAssignment(ctx *Context, repo *repo_model.Repository) {
var err error
if err = repo.GetOwner(db.DefaultContext); err != nil {
if err = repo.GetOwner(ctx); err != nil {
ctx.ServerError("GetOwner", err)
return
}
Expand Down
2 changes: 1 addition & 1 deletion modules/doctor/checkOldArchives.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func checkOldArchives(ctx context.Context, logger log.Logger, autofix bool) error {
numRepos := 0
numReposUpdated := 0
err := iterateRepositories(func(repo *repo_model.Repository) error {
err := iterateRepositories(ctx, func(repo *repo_model.Repository) error {
if repo.IsEmpty {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion modules/doctor/fix16961.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func fixBrokenRepoUnits16961(ctx context.Context, logger log.Logger, autofix boo
count := 0

err := db.Iterate(
db.DefaultContext,
ctx,
new(RepoUnit),
builder.Gt{
"id": 0,
Expand Down
8 changes: 4 additions & 4 deletions modules/doctor/mergebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
"xorm.io/builder"
)

func iteratePRs(repo *repo_model.Repository, each func(*repo_model.Repository, *models.PullRequest) error) error {
func iteratePRs(ctx context.Context, repo *repo_model.Repository, each func(*repo_model.Repository, *models.PullRequest) error) error {
return db.Iterate(
db.DefaultContext,
ctx,
new(models.PullRequest),
builder.Eq{"base_repo_id": repo.ID},
func(idx int, bean interface{}) error {
Expand All @@ -33,9 +33,9 @@ func checkPRMergeBase(ctx context.Context, logger log.Logger, autofix bool) erro
numRepos := 0
numPRs := 0
numPRsUpdated := 0
err := iterateRepositories(func(repo *repo_model.Repository) error {
err := iterateRepositories(ctx, func(repo *repo_model.Repository) error {
numRepos++
return iteratePRs(repo, func(repo *repo_model.Repository, pr *models.PullRequest) error {
return iteratePRs(ctx, repo, func(repo *repo_model.Repository, pr *models.PullRequest) error {
numPRs++
pr.BaseRepo = repo
repoPath := repo.RepoPath()
Expand Down
12 changes: 6 additions & 6 deletions modules/doctor/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import (
"xorm.io/builder"
)

func iterateRepositories(each func(*repo_model.Repository) error) error {
func iterateRepositories(ctx context.Context, each func(*repo_model.Repository) error) error {
err := db.Iterate(
db.DefaultContext,
ctx,
new(repo_model.Repository),
builder.Gt{"id": 0},
func(idx int, bean interface{}) error {
Expand All @@ -50,7 +50,7 @@ func checkScriptType(ctx context.Context, logger log.Logger, autofix bool) error
}

func checkHooks(ctx context.Context, logger log.Logger, autofix bool) error {
if err := iterateRepositories(func(repo *repo_model.Repository) error {
if err := iterateRepositories(ctx, func(repo *repo_model.Repository) error {
results, err := repository.CheckDelegateHooks(repo.RepoPath())
if err != nil {
logger.Critical("Unable to check delegate hooks for repo %-v. ERROR: %v", repo, err)
Expand Down Expand Up @@ -86,7 +86,7 @@ func checkEnablePushOptions(ctx context.Context, logger log.Logger, autofix bool
numRepos := 0
numNeedUpdate := 0

if err := iterateRepositories(func(repo *repo_model.Repository) error {
if err := iterateRepositories(ctx, func(repo *repo_model.Repository) error {
numRepos++
r, err := git.OpenRepositoryCtx(git.DefaultContext, repo.RepoPath())
if err != nil {
Expand Down Expand Up @@ -132,13 +132,13 @@ func checkDaemonExport(ctx context.Context, logger log.Logger, autofix bool) err
logger.Critical("Unable to create cache: %v", err)
return err
}
if err := iterateRepositories(func(repo *repo_model.Repository) error {
if err := iterateRepositories(ctx, func(repo *repo_model.Repository) error {
numRepos++

if owner, has := cache.Get(repo.OwnerID); has {
repo.Owner = owner.(*user_model.User)
} else {
if err := repo.GetOwner(db.DefaultContext); err != nil {
if err := repo.GetOwner(ctx); err != nil {
return err
}
cache.Add(repo.OwnerID, repo.Owner)
Expand Down
2 changes: 1 addition & 1 deletion modules/repository/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User,
}
}

if err = models.UpdateRepoSize(db.DefaultContext, repo); err != nil {
if err = models.UpdateRepoSize(ctx, repo); err != nil {
log.Error("Failed to update size for repository: %v", err)
}

Expand Down
5 changes: 4 additions & 1 deletion modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,15 @@ var (
// AppDataPath is the default path for storing data.
// It maps to ini:"APP_DATA_PATH" and defaults to AppWorkPath + "/data"
AppDataPath string
// LocalURL is the url for locally running applications to contact Gitea. It always has a '/' suffix
// It maps to ini:"LOCAL_ROOT_URL"
LocalURL string

// Server settings
Protocol Scheme
Domain string
HTTPAddr string
HTTPPort string
LocalURL string
RedirectOtherPort bool
PortToRedirect string
OfflineMode bool
Expand Down Expand Up @@ -747,6 +749,7 @@ func loadFromConf(allowEmpty bool, extraConfig string) {
}
}
LocalURL = sec.Key("LOCAL_ROOT_URL").MustString(defaultLocalURL)
LocalURL = strings.TrimRight(LocalURL, "/") + "/"
RedirectOtherPort = sec.Key("REDIRECT_OTHER_PORT").MustBool(false)
PortToRedirect = sec.Key("PORT_TO_REDIRECT").MustString("80")
OfflineMode = sec.Key("OFFLINE_MODE").MustBool()
Expand Down
34 changes: 8 additions & 26 deletions modules/storage/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package storage

import (
"context"
"errors"
"io"
"net/url"
"os"
Expand All @@ -18,8 +17,6 @@ import (
"code.gitea.io/gitea/modules/util"
)

// ErrLocalPathNotSupported represents an error that path is not supported
var ErrLocalPathNotSupported = errors.New("local path is not supported")
var _ ObjectStorage = &LocalStorage{}

// LocalStorageType is the type descriptor for local storage
Expand Down Expand Up @@ -62,21 +59,18 @@ func NewLocalStorage(ctx context.Context, cfg interface{}) (ObjectStorage, error
}, nil
}

func (l *LocalStorage) buildLocalPath(p string) string {
return filepath.Join(l.dir, path.Clean("/" + strings.ReplaceAll(p, "\\", "/"))[1:])
}

// Open a file
func (l *LocalStorage) Open(path string) (Object, error) {
if !isLocalPathValid(path) {
return nil, ErrLocalPathNotSupported
}
return os.Open(filepath.Join(l.dir, path))
return os.Open(l.buildLocalPath(path))
}

// Save a file
func (l *LocalStorage) Save(path string, r io.Reader, size int64) (int64, error) {
if !isLocalPathValid(path) {
return 0, ErrLocalPathNotSupported
}

p := filepath.Join(l.dir, path)
p := l.buildLocalPath(path)
if err := os.MkdirAll(filepath.Dir(p), os.ModePerm); err != nil {
return 0, err
}
Expand Down Expand Up @@ -116,24 +110,12 @@ func (l *LocalStorage) Save(path string, r io.Reader, size int64) (int64, error)

// Stat returns the info of the file
func (l *LocalStorage) Stat(path string) (os.FileInfo, error) {
return os.Stat(filepath.Join(l.dir, path))
}

func isLocalPathValid(p string) bool {
a := path.Clean(p)
if strings.HasPrefix(a, "../") || strings.HasPrefix(a, "..\\") {
return false
}
return a == p
return os.Stat(l.buildLocalPath(path))
}

// Delete delete a file
func (l *LocalStorage) Delete(path string) error {
if !isLocalPathValid(path) {
return ErrLocalPathNotSupported
}
p := filepath.Join(l.dir, path)
return util.Remove(p)
return util.Remove(l.buildLocalPath(path))
}

// URL gets the redirect URL to a file
Expand Down
34 changes: 21 additions & 13 deletions modules/storage/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,44 @@ import (
"github.com/stretchr/testify/assert"
)

func TestLocalPathIsValid(t *testing.T) {
func TestBuildLocalPath(t *testing.T) {
kases := []struct {
path string
valid bool
localDir string
path string
expected string
}{
{
"a",
"0/a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a14",
"a/0/a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a14",
true,
},
{
"../a/0/a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a14",
false,
"a",
"../0/a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a14",
"a/0/a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a14",
},
{
"a\\0\\a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a14",
true,
"a",
"0\\a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a14",
"a/0/a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a14",
},
{
"b/../a/0/a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a14",
false,
"b",
"a/../0/a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a14",
"b/0/a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a14",
},
{
"..\\a/0/a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a14",
false,
"b",
"a\\..\\0/a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a14",
"b/0/a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a14",
},
}

for _, k := range kases {
t.Run(k.path, func(t *testing.T) {
assert.EqualValues(t, k.valid, isLocalPathValid(k.path))
l := LocalStorage{dir: k.localDir}

assert.EqualValues(t, k.expected, l.buildLocalPath(k.path))
})
}
}
2 changes: 1 addition & 1 deletion modules/storage/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func NewMinioStorage(ctx context.Context, cfg interface{}) (ObjectStorage, error
}

func (m *MinioStorage) buildMinioPath(p string) string {
return strings.TrimPrefix(path.Join(m.basePath, p), "/")
return strings.TrimPrefix(path.Join(m.basePath, path.Clean("/" + strings.ReplaceAll(p, "\\", "/"))[1:]), "/")
}

// Open open a file
Expand Down
3 changes: 1 addition & 2 deletions modules/test/context_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"testing"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
Expand Down Expand Up @@ -87,7 +86,7 @@ func LoadUser(t *testing.T, ctx *context.Context, userID int64) {
// LoadGitRepo load a git repo into a test context. Requires that ctx.Repo has
// already been populated.
func LoadGitRepo(t *testing.T, ctx *context.Context) {
assert.NoError(t, ctx.Repo.Repository.GetOwner(db.DefaultContext))
assert.NoError(t, ctx.Repo.Repository.GetOwner(ctx))
var err error
ctx.Repo.GitRepo, err = git.OpenRepositoryCtx(ctx, ctx.Repo.Repository.RepoPath())
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion options/locale/locale_pl-PL.ini
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ ssh_key_verify=Weryfikuj
ssh_token_required=Musisz podać podpis poniższego tokenu
ssh_token=Token
ssh_token_help=Możesz wygenerować podpis używając:
ssh_token_code=echo -n "%s" | ssh-keygen -Y znak -n gitea -f /ścieżka_do_twojego_klucza_publicznego
ssh_token_code=echo -n "%s" | ssh-keygen -Y sign -n gitea -f /ścieżka_do_twojego_klucza_publicznego
ssh_token_signature=Wzmocniony podpis SSH
key_signature_ssh_placeholder=Zaczyna się od '-----BEGIN SSH SIGNATURE-----'
verify_ssh_key_success=Klucz SSH '%s' został zweryfikowany.
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/org/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func Edit(ctx *context.APIContext) {
if form.RepoAdminChangeTeamAccess != nil {
org.RepoAdminChangeTeamAccess = *form.RepoAdminChangeTeamAccess
}
if err := user_model.UpdateUserCols(db.DefaultContext, org.AsUser(),
if err := user_model.UpdateUserCols(ctx, org.AsUser(),
"full_name", "description", "website", "location",
"visibility", "repo_admin_change_team_access",
); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions routers/api/v1/repo/issue_stopwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net/http"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/routers/api/v1/utils"
Expand Down Expand Up @@ -56,7 +55,7 @@ func StartIssueStopwatch(ctx *context.APIContext) {
return
}

if err := models.CreateIssueStopwatch(db.DefaultContext, ctx.Doer, issue); err != nil {
if err := models.CreateIssueStopwatch(ctx, ctx.Doer, issue); err != nil {
ctx.Error(http.StatusInternalServerError, "CreateOrStopIssueStopwatch", err)
return
}
Expand Down Expand Up @@ -105,7 +104,7 @@ func StopIssueStopwatch(ctx *context.APIContext) {
return
}

if err := models.FinishIssueStopwatch(db.DefaultContext, ctx.Doer, issue); err != nil {
if err := models.FinishIssueStopwatch(ctx, ctx.Doer, issue); err != nil {
ctx.Error(http.StatusInternalServerError, "CreateOrStopIssueStopwatch", err)
return
}
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/repo/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func ListDeployKeys(ctx *context.APIContext) {
Fingerprint: ctx.FormString("fingerprint"),
}

keys, err := asymkey_model.ListDeployKeys(db.DefaultContext, opts)
keys, err := asymkey_model.ListDeployKeys(ctx, opts)
if err != nil {
ctx.InternalServerError(err)
return
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func Search(ctx *context.APIContext) {

results := make([]*api.Repository, len(repos))
for i, repo := range repos {
if err = repo.GetOwner(db.DefaultContext); err != nil {
if err = repo.GetOwner(ctx); err != nil {
ctx.JSON(http.StatusInternalServerError, api.SearchError{
OK: false,
Error: err.Error(),
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/user/gpg_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

func listGPGKeys(ctx *context.APIContext, uid int64, listOptions db.ListOptions) {
keys, err := asymkey_model.ListGPGKeys(db.DefaultContext, uid, listOptions)
keys, err := asymkey_model.ListGPGKeys(ctx, uid, listOptions)
if err != nil {
ctx.Error(http.StatusInternalServerError, "ListGPGKeys", err)
return
Expand Down
Loading

0 comments on commit 9ebc810

Please sign in to comment.