Skip to content

Commit 5ce21b5

Browse files
authored
Merge branch 'main' into remove-repo-context
2 parents 0773956 + 4ed71eb commit 5ce21b5

File tree

41 files changed

+332
-305
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+332
-305
lines changed

cmd/doctor.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package cmd
55

66
import (
7+
"context"
78
"fmt"
89
golog "log"
910
"os"
@@ -130,8 +131,8 @@ func runRecreateTable(ctx *cli.Context) error {
130131
}
131132
recreateTables := migrate_base.RecreateTables(beans...)
132133

133-
return db.InitEngineWithMigration(stdCtx, func(x *xorm.Engine) error {
134-
if err := migrations.EnsureUpToDate(x); err != nil {
134+
return db.InitEngineWithMigration(stdCtx, func(ctx context.Context, x *xorm.Engine) error {
135+
if err := migrations.EnsureUpToDate(ctx, x); err != nil {
135136
return err
136137
}
137138
return recreateTables(x)

cmd/migrate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"context"
88

99
"code.gitea.io/gitea/models/db"
10-
"code.gitea.io/gitea/models/migrations"
1110
"code.gitea.io/gitea/modules/log"
1211
"code.gitea.io/gitea/modules/setting"
12+
"code.gitea.io/gitea/services/versioned_migration"
1313

1414
"github.com/urfave/cli/v2"
1515
)
@@ -36,7 +36,7 @@ func runMigrate(ctx *cli.Context) error {
3636
log.Info("Log path: %s", setting.Log.RootPath)
3737
log.Info("Configuration file: %s", setting.CustomConf)
3838

39-
if err := db.InitEngineWithMigration(context.Background(), migrations.Migrate); err != nil {
39+
if err := db.InitEngineWithMigration(context.Background(), versioned_migration.Migrate); err != nil {
4040
log.Fatal("Failed to initialize ORM engine: %v", err)
4141
return err
4242
}

cmd/migrate_storage.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import (
1313
actions_model "code.gitea.io/gitea/models/actions"
1414
"code.gitea.io/gitea/models/db"
1515
git_model "code.gitea.io/gitea/models/git"
16-
"code.gitea.io/gitea/models/migrations"
1716
packages_model "code.gitea.io/gitea/models/packages"
1817
repo_model "code.gitea.io/gitea/models/repo"
1918
user_model "code.gitea.io/gitea/models/user"
2019
"code.gitea.io/gitea/modules/log"
2120
packages_module "code.gitea.io/gitea/modules/packages"
2221
"code.gitea.io/gitea/modules/setting"
2322
"code.gitea.io/gitea/modules/storage"
23+
"code.gitea.io/gitea/services/versioned_migration"
2424

2525
"github.com/urfave/cli/v2"
2626
)
@@ -227,7 +227,7 @@ func runMigrateStorage(ctx *cli.Context) error {
227227
log.Info("Log path: %s", setting.Log.RootPath)
228228
log.Info("Configuration file: %s", setting.CustomConf)
229229

230-
if err := db.InitEngineWithMigration(context.Background(), migrations.Migrate); err != nil {
230+
if err := db.InitEngineWithMigration(context.Background(), versioned_migration.Migrate); err != nil {
231231
log.Fatal("Failed to initialize ORM engine: %v", err)
232232
return err
233233
}

models/db/engine_hook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (h *EngineHook) AfterProcess(c *contexts.ContextHook) error {
4141
// 8 is the amount of skips passed to runtime.Caller, so that in the log the correct function
4242
// is being displayed (the function that ultimately wants to execute the query in the code)
4343
// instead of the function of the slow query hook being called.
44-
h.Logger.Log(8, log.WARN, "[Slow SQL Query] %s %v - %v", c.SQL, c.Args, c.ExecuteTime)
44+
h.Logger.Log(8, &log.Event{Level: log.WARN}, "[Slow SQL Query] %s %v - %v", c.SQL, c.Args, c.ExecuteTime)
4545
}
4646
return nil
4747
}

models/db/engine_init.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func UnsetDefaultEngine() {
105105
// When called from the "doctor" command, the migration function is a version check
106106
// that prevents the doctor from fixing anything in the database if the migration level
107107
// is different from the expected value.
108-
func InitEngineWithMigration(ctx context.Context, migrateFunc func(*xorm.Engine) error) (err error) {
108+
func InitEngineWithMigration(ctx context.Context, migrateFunc func(context.Context, *xorm.Engine) error) (err error) {
109109
if err = InitEngine(ctx); err != nil {
110110
return err
111111
}
@@ -122,7 +122,7 @@ func InitEngineWithMigration(ctx context.Context, migrateFunc func(*xorm.Engine)
122122
// Installation should only be being re-run if users want to recover an old database.
123123
// However, we should think carefully about should we support re-install on an installed instance,
124124
// as there may be other problems due to secret reinitialization.
125-
if err = migrateFunc(xormEngine); err != nil {
125+
if err = migrateFunc(ctx, xormEngine); err != nil {
126126
return fmt.Errorf("migrate: %w", err)
127127
}
128128

models/db/log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const stackLevel = 8
2929

3030
// Log a message with defined skip and at logging level
3131
func (l *XORMLogBridge) Log(skip int, level log.Level, format string, v ...any) {
32-
l.logger.Log(skip+1, level, format, v...)
32+
l.logger.Log(skip+1, &log.Event{Level: level}, format, v...)
3333
}
3434

3535
// Debug show debug log

models/migrations/migrations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ func ExpectedDBVersion() int64 {
413413
}
414414

415415
// EnsureUpToDate will check if the db is at the correct version
416-
func EnsureUpToDate(x *xorm.Engine) error {
416+
func EnsureUpToDate(ctx context.Context, x *xorm.Engine) error {
417417
currentDB, err := GetCurrentDBVersion(x)
418418
if err != nil {
419419
return err

modules/git/repo_compare.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,9 @@ func (repo *Repository) GetDiffNumChangedFiles(base, head string, directComparis
174174
return w.numLines, nil
175175
}
176176

177-
// GetDiffShortStat counts number of changed files, number of additions and deletions
178-
func (repo *Repository) GetDiffShortStat(base, head string) (numFiles, totalAdditions, totalDeletions int, err error) {
179-
numFiles, totalAdditions, totalDeletions, err = GetDiffShortStat(repo.Ctx, repo.Path, nil, base+"..."+head)
180-
if err != nil && strings.Contains(err.Error(), "no merge base") {
181-
return GetDiffShortStat(repo.Ctx, repo.Path, nil, base, head)
182-
}
183-
return numFiles, totalAdditions, totalDeletions, err
184-
}
185-
186-
// GetDiffShortStat counts number of changed files, number of additions and deletions
187-
func GetDiffShortStat(ctx context.Context, repoPath string, trustedArgs TrustedCmdArgs, dynamicArgs ...string) (numFiles, totalAdditions, totalDeletions int, err error) {
177+
// GetDiffShortStatByCmdArgs counts number of changed files, number of additions and deletions
178+
// TODO: it can be merged with another "GetDiffShortStat" in the future
179+
func GetDiffShortStatByCmdArgs(ctx context.Context, repoPath string, trustedArgs TrustedCmdArgs, dynamicArgs ...string) (numFiles, totalAdditions, totalDeletions int, err error) {
188180
// Now if we call:
189181
// $ git diff --shortstat 1ebb35b98889ff77299f24d82da426b434b0cca0...788b8b1440462d477f45b0088875
190182
// we get:

modules/log/event_format.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,19 @@ func EventFormatTextMessage(mode *WriterMode, event *Event, msgFormat string, ms
125125
buf = append(buf, resetBytes...)
126126
}
127127
}
128-
if flags&(Lshortfile|Llongfile) != 0 {
128+
if flags&(Lshortfile|Llongfile) != 0 && event.Filename != "" {
129129
if mode.Colorize {
130130
buf = append(buf, fgGreenBytes...)
131131
}
132132
file := event.Filename
133133
if flags&Lmedfile == Lmedfile {
134-
startIndex := len(file) - 20
135-
if startIndex > 0 {
136-
file = "..." + file[startIndex:]
134+
fileLen := len(file)
135+
const softLimit = 20
136+
if fileLen > softLimit {
137+
slashIndex := strings.LastIndexByte(file[:fileLen-softLimit], '/')
138+
if slashIndex != -1 {
139+
file = ".../" + file[slashIndex+1:]
140+
}
137141
}
138142
} else if flags&Lshortfile != 0 {
139143
startIndex := strings.LastIndexByte(file, '/')
@@ -157,14 +161,22 @@ func EventFormatTextMessage(mode *WriterMode, event *Event, msgFormat string, ms
157161
if mode.Colorize {
158162
buf = append(buf, fgGreenBytes...)
159163
}
160-
funcname := event.Caller
164+
funcName := event.Caller
165+
shortFuncName := funcName
161166
if flags&Lshortfuncname != 0 {
162-
lastIndex := strings.LastIndexByte(funcname, '.')
163-
if lastIndex > 0 && len(funcname) > lastIndex+1 {
164-
funcname = funcname[lastIndex+1:]
167+
// funcName = "code.gitea.io/gitea/modules/foo/bar.MyFunc.func1.2()"
168+
slashPos := strings.LastIndexByte(funcName, '/')
169+
dotPos := strings.IndexByte(funcName[slashPos+1:], '.')
170+
if dotPos > 0 {
171+
// shortFuncName = "MyFunc.func1.2()"
172+
shortFuncName = funcName[slashPos+1+dotPos+1:]
173+
if strings.Contains(shortFuncName, ".") {
174+
shortFuncName = strings.ReplaceAll(shortFuncName, ".func", ".")
175+
}
165176
}
177+
funcName = shortFuncName
166178
}
167-
buf = append(buf, funcname...)
179+
buf = append(buf, funcName...)
168180
if mode.Colorize {
169181
buf = append(buf, resetBytes...)
170182
}

modules/log/logger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ package log
2424

2525
// BaseLogger provides the basic logging functions
2626
type BaseLogger interface {
27-
Log(skip int, level Level, format string, v ...any)
27+
Log(skip int, event *Event, format string, v ...any)
2828
GetLevel() Level
2929
}
3030

0 commit comments

Comments
 (0)