Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func runRepoSyncReleases(ctx context.Context, _ *cli.Command) error {
return err
}

if err := git.InitSimple(ctx); err != nil {
if err := git.InitSimple(); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func runRecreateTable(ctx context.Context, cmd *cli.Command) error {
}
recreateTables := migrate_base.RecreateTables(beans...)

return db.InitEngineWithMigration(ctx, func(ctx context.Context, x *xorm.Engine) error {
return db.InitEngineWithMigration(context.Background(), func(ctx context.Context, x *xorm.Engine) error {
if err := migrations.EnsureUpToDate(ctx, x); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/dump_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func runDumpRepository(ctx context.Context, cmd *cli.Command) error {
}

// migrations.GiteaLocalUploader depends on git module
if err := git.InitSimple(context.Background()); err != nil {
if err := git.InitSimple(); err != nil {
return err
}

Expand Down Expand Up @@ -179,7 +179,7 @@ func runDumpRepository(ctx context.Context, cmd *cli.Command) error {
}

if err := migrations.DumpRepository(
context.Background(),
ctx,
repoDir,
cmd.String("owner_name"),
opts,
Expand Down
2 changes: 1 addition & 1 deletion cmd/serv.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func setup(ctx context.Context, debug bool) {
_ = fail(ctx, "Unable to access repository path", "Unable to access repository path %q, err: %v", setting.RepoRootPath, err)
return
}
if err := git.InitSimple(context.Background()); err != nil {
if err := git.InitSimple(); err != nil {
_ = fail(ctx, "Failed to init git", "Failed to init git, err: %v", err)
}
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,16 @@ func serveInstalled(c *cli.Command) error {
}

func servePprof() {
// FIXME: it shouldn't use the global DefaultServeMux, and it should use a proper context
http.DefaultServeMux.Handle("/debug/fgprof", fgprof.Handler())
_, _, finished := process.GetManager().AddTypedContext(context.Background(), "Web: PProf Server", process.SystemProcessType, true)
// The pprof server is for debug purpose only, it shouldn't be exposed on public network. At the moment it's not worth to introduce a configurable option for it.
_, _, finished := process.GetManager().AddTypedContext(context.TODO(), "Web: PProf Server", process.SystemProcessType, true)
// The pprof server is for debug purpose only, it shouldn't be exposed on public network. At the moment, it's not worth introducing a configurable option for it.
log.Info("Starting pprof server on localhost:6060")
log.Info("Stopped pprof server: %v", http.ListenAndServe("localhost:6060", nil))
finished()
}

func runWeb(_ context.Context, cmd *cli.Command) error {
func runWeb(ctx context.Context, cmd *cli.Command) error {
defer func() {
if panicked := recover(); panicked != nil {
log.Fatal("PANIC: %v\n%s", panicked, log.Stack(2))
Expand All @@ -255,7 +256,7 @@ func runWeb(_ context.Context, cmd *cli.Command) error {
return fmt.Errorf("unknown command: %s", subCmdName)
}

managerCtx, cancel := context.WithCancel(context.Background())
managerCtx, cancel := context.WithCancel(ctx)
graceful.InitManager(managerCtx)
defer cancel()

Expand Down
2 changes: 1 addition & 1 deletion models/activities/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func (a *Action) GetCommentHTMLURL(ctx context.Context) string {
return "#"
}

return a.Issue.HTMLURL()
return a.Issue.HTMLURL(ctx)
}

// GetCommentLink returns link to action comment.
Expand Down
6 changes: 3 additions & 3 deletions models/activities/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,11 @@ func (n *Notification) HTMLURL(ctx context.Context) string {
if n.Comment != nil {
return n.Comment.HTMLURL(ctx)
}
return n.Issue.HTMLURL()
return n.Issue.HTMLURL(ctx)
case NotificationSourceCommit:
return n.Repository.HTMLURL() + "/commit/" + url.PathEscape(n.CommitID)
return n.Repository.HTMLURL(ctx) + "/commit/" + url.PathEscape(n.CommitID)
case NotificationSourceRepository:
return n.Repository.HTMLURL()
return n.Repository.HTMLURL(ctx)
}
return ""
}
Expand Down
6 changes: 3 additions & 3 deletions models/issues/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func (c *Comment) HTMLURL(ctx context.Context) string {
log.Error("loadRepo(%d): %v", c.Issue.RepoID, err)
return ""
}
return c.Issue.HTMLURL() + c.hashLink(ctx)
return c.Issue.HTMLURL(ctx) + c.hashLink(ctx)
}

// Link formats a relative URL-string to the issue-comment
Expand Down Expand Up @@ -483,7 +483,7 @@ func (c *Comment) IssueURL(ctx context.Context) string {
log.Error("loadRepo(%d): %v", c.Issue.RepoID, err)
return ""
}
return c.Issue.HTMLURL()
return c.Issue.HTMLURL(ctx)
}

// PRURL formats a URL-string to the pull-request
Expand All @@ -503,7 +503,7 @@ func (c *Comment) PRURL(ctx context.Context) string {
if !c.Issue.IsPull {
return ""
}
return c.Issue.HTMLURL()
return c.Issue.HTMLURL(ctx)
}

// CommentHashTag returns unique hash tag for comment id.
Expand Down
4 changes: 2 additions & 2 deletions models/issues/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,14 +405,14 @@ func (issue *Issue) APIURL(ctx context.Context) string {
}

// HTMLURL returns the absolute URL to this issue.
func (issue *Issue) HTMLURL() string {
func (issue *Issue) HTMLURL(ctx context.Context) string {
var path string
if issue.IsPull {
path = "pulls"
} else {
path = "issues"
}
return fmt.Sprintf("%s/%s/%d", issue.Repo.HTMLURL(), path, issue.Index)
return fmt.Sprintf("%s/%s/%d", issue.Repo.HTMLURL(ctx), path, issue.Index)
}

// Link returns the issue's relative URL.
Expand Down
3 changes: 1 addition & 2 deletions models/migrations/base/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package base

import (
"context"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -124,7 +123,7 @@ func MainTest(m *testing.M) {
setting.AppDataPath = tmpDataPath

unittest.InitSettingsForTesting()
if err = git.InitFull(context.Background()); err != nil {
if err = git.InitFull(); err != nil {
testlogger.Fatalf("Unable to InitFull: %v\n", err)
}
setting.LoadDBSetting()
Expand Down
2 changes: 1 addition & 1 deletion models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ Please try upgrading to a lower version first (suggested v1.6.4), then upgrade t

// Some migration tasks depend on the git command
if git.DefaultContext == nil {
if err = git.InitSimple(context.Background()); err != nil {
if err = git.InitSimple(); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions models/organization/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ func (org *Organization) AvatarLink(ctx context.Context) string {
}

// HTMLURL returns the organization's full link.
func (org *Organization) HTMLURL() string {
return org.AsUser().HTMLURL()
func (org *Organization) HTMLURL(ctx context.Context) string {
return org.AsUser().HTMLURL(ctx)
}

// OrganisationLink returns the organization sub page link.
Expand Down
8 changes: 4 additions & 4 deletions models/packages/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ func (pd *PackageDescriptor) VersionWebLink() string {
}

// PackageHTMLURL returns the absolute package HTML URL
func (pd *PackageDescriptor) PackageHTMLURL() string {
return fmt.Sprintf("%s/-/packages/%s/%s", pd.Owner.HTMLURL(), string(pd.Package.Type), url.PathEscape(pd.Package.LowerName))
func (pd *PackageDescriptor) PackageHTMLURL(ctx context.Context) string {
return fmt.Sprintf("%s/-/packages/%s/%s", pd.Owner.HTMLURL(ctx), string(pd.Package.Type), url.PathEscape(pd.Package.LowerName))
}

// VersionHTMLURL returns the absolute package version HTML URL
func (pd *PackageDescriptor) VersionHTMLURL() string {
return fmt.Sprintf("%s/%s", pd.PackageHTMLURL(), url.PathEscape(pd.Version.LowerVersion))
func (pd *PackageDescriptor) VersionHTMLURL(ctx context.Context) string {
return fmt.Sprintf("%s/%s", pd.PackageHTMLURL(ctx), url.PathEscape(pd.Version.LowerVersion))
}

// CalculateBlobSize returns the total blobs size in bytes
Expand Down
6 changes: 2 additions & 4 deletions models/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,8 @@ func (repo *Repository) FullName() string {

// HTMLURL returns the repository HTML URL
func (repo *Repository) HTMLURL(ctxs ...context.Context) string {
ctx := context.TODO()
if len(ctxs) > 0 {
ctx = ctxs[0]
}
// FIXME: this HTMLURL is still used in mail templates, so the "ctx" is not provided.
ctx := util.OptionalArg(ctxs, context.TODO())
return httplib.MakeAbsoluteURL(ctx, repo.Link())
}

Expand Down
2 changes: 1 addition & 1 deletion models/unittest/testdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func MainTest(m *testing.M, testOptsArg ...*TestOptions) {
fatalTestError("util.SyncDirs: %v\n", err)
}

if err = git.InitFull(context.Background()); err != nil {
if err = git.InitFull(); err != nil {
fatalTestError("git.Init: %v\n", err)
}

Expand Down
5 changes: 3 additions & 2 deletions models/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/httplib"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/optional"
"code.gitea.io/gitea/modules/setting"
Expand Down Expand Up @@ -303,8 +304,8 @@ func (u *User) HomeLink() string {
}

// HTMLURL returns the user or organization's full link.
func (u *User) HTMLURL() string {
return setting.AppURL + url.PathEscape(u.Name)
func (u *User) HTMLURL(ctx context.Context) string {
return httplib.MakeAbsoluteURL(ctx, u.HomeLink())
}

// OrganisationLink returns the organization sub page link.
Expand Down
3 changes: 1 addition & 2 deletions modules/git/attribute/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package attribute

import (
"context"
"fmt"
"os"
"testing"
Expand All @@ -22,7 +21,7 @@ func testRun(m *testing.M) error {
defer util.RemoveAll(gitHomePath)
setting.Git.HomePath = gitHomePath

if err = git.InitFull(context.Background()); err != nil {
if err = git.InitFull(); err != nil {
return fmt.Errorf("failed to call Init: %w", err)
}

Expand Down
11 changes: 6 additions & 5 deletions modules/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func DefaultFeatures() *Features {
if !setting.IsProd || setting.IsInTesting {
log.Warn("git.DefaultFeatures is called before git.InitXxx, initializing with default values")
}
if err := InitSimple(context.Background()); err != nil {
if err := InitSimple(); err != nil {
log.Fatal("git.InitSimple failed: %v", err)
}
}
Expand Down Expand Up @@ -158,7 +158,7 @@ func HomeDir() string {

// InitSimple initializes git module with a very simple step, no config changes, no global command arguments.
// This method doesn't change anything to filesystem. At the moment, it is only used by some Gitea sub-commands.
func InitSimple(ctx context.Context) error {
func InitSimple() error {
if setting.Git.HomePath == "" {
return errors.New("unable to init Git's HomeDir, incorrect initialization of the setting and git modules")
}
Expand All @@ -167,7 +167,8 @@ func InitSimple(ctx context.Context) error {
log.Warn("git module has been initialized already, duplicate init may work but it's better to fix it")
}

DefaultContext = ctx
// FIXME: git context is used across the application, so it should use the global default context, this design is not right but it's hard to change now.
DefaultContext = context.Background()
globalCommandArgs = nil

if setting.Git.Timeout.Default > 0 {
Expand Down Expand Up @@ -196,8 +197,8 @@ func InitSimple(ctx context.Context) error {

// InitFull initializes git module with version check and change global variables, sync gitconfig.
// It should only be called once at the beginning of the program initialization (TestMain/GlobalInitInstalled) as this code makes unsynchronized changes to variables.
func InitFull(ctx context.Context) (err error) {
if err = InitSimple(ctx); err != nil {
func InitFull() (err error) {
if err = InitSimple(); err != nil {
return err
}

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

import (
"context"
"fmt"
"os"
"testing"
Expand All @@ -25,7 +24,7 @@ func testRun(m *testing.M) error {

setting.Git.HomePath = gitHomePath

if err = InitFull(context.Background()); err != nil {
if err = InitFull(); err != nil {
return fmt.Errorf("failed to call Init: %w", err)
}

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

import (
"context"
"fmt"
"os"
"testing"
Expand All @@ -22,7 +21,7 @@ func testRun(m *testing.M) error {
defer util.RemoveAll(gitHomePath)
setting.Git.HomePath = gitHomePath

if err = git.InitFull(context.Background()); err != nil {
if err = git.InitFull(); err != nil {
return fmt.Errorf("failed to call Init: %w", err)
}

Expand Down
9 changes: 8 additions & 1 deletion modules/graceful/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,19 @@ var (

// GetManager returns the Manager
func GetManager() *Manager {
InitManager(context.Background())
initManager(context.Background())
return manager
}

// InitManager creates the graceful manager in the provided context
func InitManager(ctx context.Context) {
if manager != nil {
log.Error("graceful.InitManager called more than once")
}
initManager(ctx) // FIXME: this design is not right, it conflicts with the "Background" context used in GetManager
}

func initManager(ctx context.Context) {
initOnce.Do(func() {
manager = newGracefulManager(ctx)

Expand Down
2 changes: 1 addition & 1 deletion modules/httplib/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type RequestContextKeyStruct struct{}
var RequestContextKey = RequestContextKeyStruct{}

func urlIsRelative(s string, u *url.URL) bool {
// Unfortunately browsers consider a redirect Location with preceding "//", "\\", "/\" and "\/" as meaning redirect to "http(s)://REST_OF_PATH"
// Unfortunately, browsers consider a redirect Location with preceding "//", "\\", "/\" and "\/" as meaning redirect to "http(s)://REST_OF_PATH"
// Therefore we should ignore these redirect locations to prevent open redirects
if len(s) > 1 && (s[0] == '/' || s[0] == '\\') && (s[1] == '/' || s[1] == '\\') {
return false
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/activitypub/person.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func Person(ctx *context.APIContext) {
return
}

person.URL = ap.IRI(ctx.ContextUser.HTMLURL())
person.URL = ap.IRI(ctx.ContextUser.HTMLURL(ctx))

person.Icon = ap.Image{
Type: ap.ImageType,
Expand Down
4 changes: 2 additions & 2 deletions routers/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ func InitWebInstallPage(ctx context.Context) {
mustInit(svg.Init)
}

// InitWebInstalled is for global installed configuration.
// InitWebInstalled is for the global configuration of an installed instance
func InitWebInstalled(ctx context.Context) {
mustInitCtx(ctx, git.InitFull)
mustInit(git.InitFull)
log.Info("Git version: %s (home: %s)", git.DefaultFeatures().VersionInfo(), git.HomeDir())
if !git.DefaultFeatures().SupportHashSha256 {
log.Warn("sha256 hash support is disabled - requires Git >= 2.42." + util.Iif(git.DefaultFeatures().UsingGogit, " Gogit is currently unsupported.", ""))
Expand Down
Loading