Skip to content

Commit 255527b

Browse files
committed
Merge branch 'main' into lunny/refactor_notfiy_watchers
2 parents 4d42422 + 6f13331 commit 255527b

Some content is hidden

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

50 files changed

+571
-438
lines changed

models/activities/action.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,8 @@ func ActivityQueryCondition(ctx context.Context, opts GetFeedsOptions) (builder.
525525
}
526526

527527
if opts.RequestedTeam != nil {
528-
env := repo_model.AccessibleTeamReposEnv(ctx, organization.OrgFromUser(opts.RequestedUser), opts.RequestedTeam)
529-
teamRepoIDs, err := env.RepoIDs(1, opts.RequestedUser.NumRepos)
528+
env := repo_model.AccessibleTeamReposEnv(organization.OrgFromUser(opts.RequestedUser), opts.RequestedTeam)
529+
teamRepoIDs, err := env.RepoIDs(ctx, 1, opts.RequestedUser.NumRepos)
530530
if err != nil {
531531
return nil, fmt.Errorf("GetTeamRepositories: %w", err)
532532
}

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/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/organization/org_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ func TestAccessibleReposEnv_CountRepos(t *testing.T) {
320320
testSuccess := func(userID, expectedCount int64) {
321321
env, err := repo_model.AccessibleReposEnv(db.DefaultContext, org, userID)
322322
assert.NoError(t, err)
323-
count, err := env.CountRepos()
323+
count, err := env.CountRepos(db.DefaultContext)
324324
assert.NoError(t, err)
325325
assert.EqualValues(t, expectedCount, count)
326326
}
@@ -334,7 +334,7 @@ func TestAccessibleReposEnv_RepoIDs(t *testing.T) {
334334
testSuccess := func(userID int64, expectedRepoIDs []int64) {
335335
env, err := repo_model.AccessibleReposEnv(db.DefaultContext, org, userID)
336336
assert.NoError(t, err)
337-
repoIDs, err := env.RepoIDs(1, 100)
337+
repoIDs, err := env.RepoIDs(db.DefaultContext, 1, 100)
338338
assert.NoError(t, err)
339339
assert.Equal(t, expectedRepoIDs, repoIDs)
340340
}
@@ -348,7 +348,7 @@ func TestAccessibleReposEnv_Repos(t *testing.T) {
348348
testSuccess := func(userID int64, expectedRepoIDs []int64) {
349349
env, err := repo_model.AccessibleReposEnv(db.DefaultContext, org, userID)
350350
assert.NoError(t, err)
351-
repos, err := env.Repos(1, 100)
351+
repos, err := env.Repos(db.DefaultContext, 1, 100)
352352
assert.NoError(t, err)
353353
expectedRepos := make(repo_model.RepositoryList, len(expectedRepoIDs))
354354
for i, repoID := range expectedRepoIDs {
@@ -367,7 +367,7 @@ func TestAccessibleReposEnv_MirrorRepos(t *testing.T) {
367367
testSuccess := func(userID int64, expectedRepoIDs []int64) {
368368
env, err := repo_model.AccessibleReposEnv(db.DefaultContext, org, userID)
369369
assert.NoError(t, err)
370-
repos, err := env.MirrorRepos()
370+
repos, err := env.MirrorRepos(db.DefaultContext)
371371
assert.NoError(t, err)
372372
expectedRepos := make(repo_model.RepositoryList, len(expectedRepoIDs))
373373
for i, repoID := range expectedRepoIDs {

models/repo/org_repo.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ func GetTeamRepositories(ctx context.Context, opts *SearchTeamRepoOptions) (Repo
4747
// AccessibleReposEnvironment operations involving the repositories that are
4848
// accessible to a particular user
4949
type AccessibleReposEnvironment interface {
50-
CountRepos() (int64, error)
51-
RepoIDs(page, pageSize int) ([]int64, error)
52-
Repos(page, pageSize int) (RepositoryList, error)
53-
MirrorRepos() (RepositoryList, error)
50+
CountRepos(ctx context.Context) (int64, error)
51+
RepoIDs(ctx context.Context, page, pageSize int) ([]int64, error)
52+
Repos(ctx context.Context, page, pageSize int) (RepositoryList, error)
53+
MirrorRepos(ctx context.Context) (RepositoryList, error)
5454
AddKeyword(keyword string)
5555
SetSort(db.SearchOrderBy)
5656
}
@@ -60,7 +60,6 @@ type accessibleReposEnv struct {
6060
user *user_model.User
6161
team *org_model.Team
6262
teamIDs []int64
63-
ctx context.Context
6463
keyword string
6564
orderBy db.SearchOrderBy
6665
}
@@ -86,18 +85,16 @@ func AccessibleReposEnv(ctx context.Context, org *org_model.Organization, userID
8685
org: org,
8786
user: user,
8887
teamIDs: teamIDs,
89-
ctx: ctx,
9088
orderBy: db.SearchOrderByRecentUpdated,
9189
}, nil
9290
}
9391

9492
// AccessibleTeamReposEnv an AccessibleReposEnvironment for the repositories in `org`
9593
// that are accessible to the specified team.
96-
func AccessibleTeamReposEnv(ctx context.Context, org *org_model.Organization, team *org_model.Team) AccessibleReposEnvironment {
94+
func AccessibleTeamReposEnv(org *org_model.Organization, team *org_model.Team) AccessibleReposEnvironment {
9795
return &accessibleReposEnv{
9896
org: org,
9997
team: team,
100-
ctx: ctx,
10198
orderBy: db.SearchOrderByRecentUpdated,
10299
}
103100
}
@@ -123,8 +120,8 @@ func (env *accessibleReposEnv) cond() builder.Cond {
123120
return cond
124121
}
125122

126-
func (env *accessibleReposEnv) CountRepos() (int64, error) {
127-
repoCount, err := db.GetEngine(env.ctx).
123+
func (env *accessibleReposEnv) CountRepos(ctx context.Context) (int64, error) {
124+
repoCount, err := db.GetEngine(ctx).
128125
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
129126
Where(env.cond()).
130127
Distinct("`repository`.id").
@@ -135,13 +132,13 @@ func (env *accessibleReposEnv) CountRepos() (int64, error) {
135132
return repoCount, nil
136133
}
137134

138-
func (env *accessibleReposEnv) RepoIDs(page, pageSize int) ([]int64, error) {
135+
func (env *accessibleReposEnv) RepoIDs(ctx context.Context, page, pageSize int) ([]int64, error) {
139136
if page <= 0 {
140137
page = 1
141138
}
142139

143140
repoIDs := make([]int64, 0, pageSize)
144-
return repoIDs, db.GetEngine(env.ctx).
141+
return repoIDs, db.GetEngine(ctx).
145142
Table("repository").
146143
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
147144
Where(env.cond()).
@@ -152,8 +149,8 @@ func (env *accessibleReposEnv) RepoIDs(page, pageSize int) ([]int64, error) {
152149
Find(&repoIDs)
153150
}
154151

155-
func (env *accessibleReposEnv) Repos(page, pageSize int) (RepositoryList, error) {
156-
repoIDs, err := env.RepoIDs(page, pageSize)
152+
func (env *accessibleReposEnv) Repos(ctx context.Context, page, pageSize int) (RepositoryList, error) {
153+
repoIDs, err := env.RepoIDs(ctx, page, pageSize)
157154
if err != nil {
158155
return nil, fmt.Errorf("GetUserRepositoryIDs: %w", err)
159156
}
@@ -163,15 +160,15 @@ func (env *accessibleReposEnv) Repos(page, pageSize int) (RepositoryList, error)
163160
return repos, nil
164161
}
165162

166-
return repos, db.GetEngine(env.ctx).
163+
return repos, db.GetEngine(ctx).
167164
In("`repository`.id", repoIDs).
168165
OrderBy(string(env.orderBy)).
169166
Find(&repos)
170167
}
171168

172-
func (env *accessibleReposEnv) MirrorRepoIDs() ([]int64, error) {
169+
func (env *accessibleReposEnv) MirrorRepoIDs(ctx context.Context) ([]int64, error) {
173170
repoIDs := make([]int64, 0, 10)
174-
return repoIDs, db.GetEngine(env.ctx).
171+
return repoIDs, db.GetEngine(ctx).
175172
Table("repository").
176173
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id AND `repository`.is_mirror=?", true).
177174
Where(env.cond()).
@@ -181,8 +178,8 @@ func (env *accessibleReposEnv) MirrorRepoIDs() ([]int64, error) {
181178
Find(&repoIDs)
182179
}
183180

184-
func (env *accessibleReposEnv) MirrorRepos() (RepositoryList, error) {
185-
repoIDs, err := env.MirrorRepoIDs()
181+
func (env *accessibleReposEnv) MirrorRepos(ctx context.Context) (RepositoryList, error) {
182+
repoIDs, err := env.MirrorRepoIDs(ctx)
186183
if err != nil {
187184
return nil, fmt.Errorf("MirrorRepoIDs: %w", err)
188185
}
@@ -192,7 +189,7 @@ func (env *accessibleReposEnv) MirrorRepos() (RepositoryList, error) {
192189
return repos, nil
193190
}
194191

195-
return repos, db.GetEngine(env.ctx).
192+
return repos, db.GetEngine(ctx).
196193
In("`repository`.id", repoIDs).
197194
Find(&repos)
198195
}

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

modules/log/logger_global.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func GetLevel() Level {
1818
}
1919

2020
func Log(skip int, level Level, format string, v ...any) {
21-
GetLogger(DEFAULT).Log(skip+1, level, format, v...)
21+
GetLogger(DEFAULT).Log(skip+1, &Event{Level: level}, format, v...)
2222
}
2323

2424
func Trace(format string, v ...any) {

modules/log/logger_impl.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -191,28 +191,27 @@ func asLogStringer(v any) LogStringer {
191191
}
192192

193193
// Log prepares the log event, if the level matches, the event will be sent to the writers
194-
func (l *LoggerImpl) Log(skip int, level Level, format string, logArgs ...any) {
195-
if Level(l.level.Load()) > level {
194+
func (l *LoggerImpl) Log(skip int, event *Event, format string, logArgs ...any) {
195+
if Level(l.level.Load()) > event.Level {
196196
return
197197
}
198198

199-
event := &Event{
200-
Time: time.Now(),
201-
Level: level,
202-
Caller: "?()",
199+
if event.Time.IsZero() {
200+
event.Time = time.Now()
203201
}
204-
205-
pc, filename, line, ok := runtime.Caller(skip + 1)
206-
if ok {
207-
fn := runtime.FuncForPC(pc)
208-
if fn != nil {
209-
event.Caller = fn.Name() + "()"
202+
if event.Caller == "" {
203+
pc, filename, line, ok := runtime.Caller(skip + 1)
204+
if ok {
205+
fn := runtime.FuncForPC(pc)
206+
if fn != nil {
207+
fnName := fn.Name()
208+
event.Caller = strings.ReplaceAll(fnName, "[...]", "") + "()" // generic function names are "foo[...]"
209+
}
210+
}
211+
event.Filename, event.Line = strings.TrimPrefix(filename, projectPackagePrefix), line
212+
if l.stacktraceLevel.Load() <= int32(event.Level) {
213+
event.Stacktrace = Stack(skip + 1)
210214
}
211-
}
212-
event.Filename, event.Line = strings.TrimPrefix(filename, projectPackagePrefix), line
213-
214-
if l.stacktraceLevel.Load() <= int32(level) {
215-
event.Stacktrace = Stack(skip + 1)
216215
}
217216

218217
// get a simple text message without color

0 commit comments

Comments
 (0)