Skip to content

Commit 3ff9e2d

Browse files
authored
Merge branch 'main' into feature/runner-logs-api-endpoint
2 parents 7f1a461 + 0385e47 commit 3ff9e2d

65 files changed

Lines changed: 662 additions & 595 deletions

Some content is hidden

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

cmd/helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func PrepareConsoleLoggerLevel(defaultLevel log.Level) func(context.Context, *cl
134134
if globalBool(c, "debug") || globalBool(c, "verbose") {
135135
level = log.TRACE
136136
}
137-
log.SetConsoleLogger(log.DEFAULT, "console-default", level)
137+
log.SetupStderrLogger(log.DEFAULT, "console-stderr", level)
138138
return ctx, nil
139139
}
140140
}

modules/git/catfile_batch_command.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import (
77
"context"
88
"os"
99
"path/filepath"
10+
"strings"
1011

1112
"code.gitea.io/gitea/modules/git/gitcmd"
13+
"code.gitea.io/gitea/modules/setting"
1214
"code.gitea.io/gitea/modules/util"
1315
)
1416

@@ -39,6 +41,9 @@ func (b *catFileBatchCommand) getBatch() *catFileBatchCommunicator {
3941
}
4042

4143
func (b *catFileBatchCommand) QueryContent(obj string) (*CatFileObject, BufferedReader, error) {
44+
if strings.Contains(obj, "\n") {
45+
setting.PanicInDevOrTesting("invalid object name with newline: %q", obj)
46+
}
4247
_, err := b.getBatch().reqWriter.Write([]byte("contents " + obj + "\n"))
4348
if err != nil {
4449
return nil, nil, err
@@ -51,6 +56,9 @@ func (b *catFileBatchCommand) QueryContent(obj string) (*CatFileObject, Buffered
5156
}
5257

5358
func (b *catFileBatchCommand) QueryInfo(obj string) (*CatFileObject, error) {
59+
if strings.Contains(obj, "\n") {
60+
setting.PanicInDevOrTesting("invalid object name with newline: %q", obj)
61+
}
5462
_, err := b.getBatch().reqWriter.Write([]byte("info " + obj + "\n"))
5563
if err != nil {
5664
return nil, err

modules/git/catfile_batch_legacy.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"io"
99
"os"
1010
"path/filepath"
11+
"strings"
1112

1213
"code.gitea.io/gitea/modules/git/gitcmd"
14+
"code.gitea.io/gitea/modules/setting"
1315
"code.gitea.io/gitea/modules/util"
1416
)
1517

@@ -50,6 +52,9 @@ func (b *catFileBatchLegacy) getBatchCheck() *catFileBatchCommunicator {
5052
}
5153

5254
func (b *catFileBatchLegacy) QueryContent(obj string) (*CatFileObject, BufferedReader, error) {
55+
if strings.Contains(obj, "\n") {
56+
setting.PanicInDevOrTesting("invalid object name with newline: %q", obj)
57+
}
5358
_, err := io.WriteString(b.getBatchContent().reqWriter, obj+"\n")
5459
if err != nil {
5560
return nil, nil, err
@@ -62,6 +67,9 @@ func (b *catFileBatchLegacy) QueryContent(obj string) (*CatFileObject, BufferedR
6267
}
6368

6469
func (b *catFileBatchLegacy) QueryInfo(obj string) (*CatFileObject, error) {
70+
if strings.Contains(obj, "\n") {
71+
setting.PanicInDevOrTesting("invalid object name with newline: %q", obj)
72+
}
6573
_, err := io.WriteString(b.getBatchCheck().reqWriter, obj+"\n")
6674
if err != nil {
6775
return nil, err

modules/log/logger_global.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,23 @@ func IsLoggerEnabled(name string) bool {
7575
return GetManager().GetLogger(name).IsEnabled()
7676
}
7777

78-
func SetConsoleLogger(loggerName, writerName string, level Level) {
78+
func SetupStderrLogger(loggerName, writerName string, level Level) {
7979
writer := NewEventWriterConsole(writerName, WriterMode{
80-
Level: level,
81-
Flags: FlagsFromBits(LstdFlags),
82-
Colorize: CanColorStdout,
83-
WriterOption: WriterConsoleOption{},
80+
Level: level,
81+
Flags: FlagsFromBits(LstdFlags),
82+
Colorize: CanColorStderr,
83+
84+
// For most CLI commands, it's better to use Stderr as log output:
85+
// this logger is installed early (app.Before), before subcommands like "dump" redirect logging to stderr.
86+
// If Stdout, early log output (e.g.: warning during config loading) goes to stdout
87+
// and corrupts any command that writes data to stdout (e.g. "gitea dump --file -").
88+
//
89+
// It is inconsistent with the web server's default console logger from config
90+
// (which will be initialized later and use Stdout by default), but there is no other way at the moment:
91+
// many existing users depend on such behavior to collect web logs (e.g. fail2ban).
92+
//
93+
// Maybe need to refactor the logger system again in the future.
94+
WriterOption: WriterConsoleOption{Stderr: true},
8495
})
8596
GetManager().GetLogger(loggerName).ReplaceAllWriters(writer)
8697
}

modules/setting/log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func initLoggerByName(manager *log.LoggerManager, rootCfg ConfigProvider, logger
256256
}
257257

258258
func InitSQLLoggersForCli(level log.Level) {
259-
log.SetConsoleLogger("xorm", "console", level)
259+
log.SetupStderrLogger("xorm", "console-stderr", level)
260260
}
261261

262262
func IsAccessLogEnabled() bool {

modules/setting/setting.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ func init() {
4141
AppVer = "dev"
4242
}
4343

44-
// We can rely on log.CanColorStdout being set properly because modules/log/console_windows.go comes before modules/setting/setting.go lexicographically
44+
// FIXME: the logger shouldn't be initialized here, the app entry should initialize the logger
4545
// By default set this logger at Info - we'll change it later, but we need to start with something.
46-
log.SetConsoleLogger(log.DEFAULT, "console", log.INFO)
46+
log.SetupStderrLogger(log.DEFAULT, "console-stderr", log.INFO)
4747
}
4848

4949
// IsRunUserMatchCurrentUser returns false if configured run user does not match

routers/web/repo/setting/protected_branch.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"code.gitea.io/gitea/models/unit"
2121
"code.gitea.io/gitea/modules/base"
2222
"code.gitea.io/gitea/modules/glob"
23+
"code.gitea.io/gitea/modules/json"
2324
"code.gitea.io/gitea/modules/templates"
2425
"code.gitea.io/gitea/modules/web"
2526
"code.gitea.io/gitea/routers/web/repo"
@@ -312,10 +313,14 @@ func DeleteProtectedBranchRulePost(ctx *context.Context) {
312313
}
313314

314315
func UpdateBranchProtectionPriories(ctx *context.Context) {
315-
form := web.GetForm(ctx).(*forms.ProtectBranchPriorityForm)
316-
repo := ctx.Repo.Repository
317-
318-
if err := git_model.UpdateProtectBranchPriorities(ctx, repo, form.IDs); err != nil {
316+
var form struct {
317+
IDs []int64 `json:"ids"`
318+
}
319+
if err := json.NewDecoder(ctx.Req.Body).Decode(&form); err != nil {
320+
ctx.JSONError("invalid argument")
321+
return
322+
}
323+
if err := git_model.UpdateProtectBranchPriorities(ctx, ctx.Repo.Repository, form.IDs); err != nil {
319324
ctx.ServerError("UpdateProtectBranchPriorities", err)
320325
return
321326
}

routers/web/web.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,7 @@ func registerWebRoutes(m *web.Router, webAuth *AuthMiddleware) {
11751175
m.Combo("/edit").Get(repo_setting.SettingsProtectedBranch).
11761176
Post(web.Bind(forms.ProtectBranchForm{}), context.RepoMustNotBeArchived(), repo_setting.SettingsProtectedBranchPost)
11771177
m.Post("/{id}/delete", repo_setting.DeleteProtectedBranchRulePost)
1178-
m.Post("/priority", web.Bind(forms.ProtectBranchPriorityForm{}), context.RepoMustNotBeArchived(), repo_setting.UpdateBranchProtectionPriories)
1178+
m.Post("/priority", context.RepoMustNotBeArchived(), repo_setting.UpdateBranchProtectionPriories)
11791179
})
11801180

11811181
m.Group("/tags", func() {

services/forms/repo_form.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,6 @@ func (f *ProtectBranchForm) Validate(req *http.Request, errs binding.Errors) bin
202202
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
203203
}
204204

205-
type ProtectBranchPriorityForm struct {
206-
IDs []int64
207-
}
208-
209205
// WebhookForm form for changing web hook
210206
type WebhookForm struct {
211207
Name string `binding:"MaxSize(255)"`

services/pull/check.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,18 +339,25 @@ func getMergeCommit(ctx context.Context, pr *issues_model.PullRequest) (*git.Com
339339

340340
objectFormat := git.ObjectFormatFromName(pr.BaseRepo.ObjectFormatName)
341341

342-
// Get the commit from BaseBranch where the pull request got merged
342+
// Get the commit from BaseBranch where the pull request got merged.
343+
// When several PRs targeting the same base are merged in a single push,
344+
// rev-list returns one line per merge commit on the ancestry path; we
345+
// only want the first one (the oldest, with --reverse, i.e. the merge
346+
// commit that actually introduced this PR).
343347
mergeCommit, _, err := gitrepo.RunCmdString(ctx, pr.BaseRepo,
344348
gitcmd.NewCommand("rev-list", "--ancestry-path", "--merges", "--reverse").
345349
AddDynamicArguments(prHeadCommitID+".."+pr.BaseBranch))
346350
if err != nil {
347351
return nil, fmt.Errorf("git rev-list --ancestry-path --merges --reverse: %w", err)
348-
} else if len(mergeCommit) < objectFormat.FullLength() {
352+
}
353+
354+
// only use the latest commit as merge commit if the output contains multiple commits
355+
mergeCommit = strings.TrimSpace(mergeCommit)
356+
mergeCommit, _, _ = strings.Cut(mergeCommit, "\n")
357+
if len(mergeCommit) < objectFormat.FullLength() {
349358
// PR was maybe fast-forwarded, so just use last commit of PR
350359
mergeCommit = prHeadCommitID
351360
}
352-
mergeCommit = strings.TrimSpace(mergeCommit)
353-
354361
commit, err := gitRepo.GetCommit(mergeCommit)
355362
if err != nil {
356363
return nil, fmt.Errorf("GetMergeCommit[%s]: %w", mergeCommit, err)

0 commit comments

Comments
 (0)