Skip to content

Commit 2e77047

Browse files
authored
Merge branch 'main' into lunny/fix_git_config_conflict
2 parents da34762 + e19d0e4 commit 2e77047

File tree

5 files changed

+26
-65
lines changed

5 files changed

+26
-65
lines changed

models/db/context.go

Lines changed: 16 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,8 @@ type engineContextKeyType struct{}
2121

2222
var engineContextKey = engineContextKeyType{}
2323

24-
type xormContextType struct {
25-
context.Context
26-
engine Engine
27-
}
28-
29-
var xormContext *xormContextType
30-
31-
func newContext(ctx context.Context, e Engine) *xormContextType {
32-
return &xormContextType{Context: ctx, engine: e}
33-
}
34-
35-
// Value shadows Value for context.Context but allows us to get ourselves and an Engined object
36-
func (ctx *xormContextType) Value(key any) any {
37-
if key == engineContextKey {
38-
return ctx
39-
}
40-
return ctx.Context.Value(key)
41-
}
42-
43-
// WithContext returns this engine tied to this context
44-
func (ctx *xormContextType) WithContext(other context.Context) *xormContextType {
45-
return newContext(ctx, ctx.engine.Context(other))
24+
func withContextEngine(ctx context.Context, e Engine) context.Context {
25+
return context.WithValue(ctx, engineContextKey, e)
4626
}
4727

4828
var (
@@ -89,8 +69,8 @@ func contextSafetyCheck(e Engine) {
8969
// GetEngine gets an existing db Engine/Statement or creates a new Session
9070
func GetEngine(ctx context.Context) (e Engine) {
9171
defer func() { contextSafetyCheck(e) }()
92-
if e := getExistingEngine(ctx); e != nil {
93-
return e
72+
if engine, ok := ctx.Value(engineContextKey).(Engine); ok {
73+
return engine
9474
}
9575
return xormEngine.Context(ctx)
9676
}
@@ -99,17 +79,6 @@ func GetXORMEngineForTesting() *xorm.Engine {
9979
return xormEngine
10080
}
10181

102-
// getExistingEngine gets an existing db Engine/Statement from this context or returns nil
103-
func getExistingEngine(ctx context.Context) (e Engine) {
104-
if engined, ok := ctx.(*xormContextType); ok {
105-
return engined.engine
106-
}
107-
if engined, ok := ctx.Value(engineContextKey).(*xormContextType); ok {
108-
return engined.engine
109-
}
110-
return nil
111-
}
112-
11382
// Committer represents an interface to Commit or Close the Context
11483
type Committer interface {
11584
Commit() error
@@ -152,24 +121,23 @@ func (c *halfCommitter) Close() error {
152121
// And all operations submitted by the caller stack will be rollbacked as well, not only the operations in the current function.
153122
// d. It doesn't mean rollback is forbidden, but always do it only when there is an error, and you do want to rollback.
154123
func TxContext(parentCtx context.Context) (context.Context, Committer, error) {
155-
if sess, ok := inTransaction(parentCtx); ok {
156-
return newContext(parentCtx, sess), &halfCommitter{committer: sess}, nil
124+
if sess := getTransactionSession(parentCtx); sess != nil {
125+
return withContextEngine(parentCtx, sess), &halfCommitter{committer: sess}, nil
157126
}
158127

159128
sess := xormEngine.NewSession()
160129
if err := sess.Begin(); err != nil {
161130
_ = sess.Close()
162131
return nil, nil, err
163132
}
164-
165-
return newContext(xormContext, sess), sess, nil
133+
return withContextEngine(parentCtx, sess), sess, nil
166134
}
167135

168136
// WithTx represents executing database operations on a transaction, if the transaction exist,
169137
// this function will reuse it otherwise will create a new one and close it when finished.
170138
func WithTx(parentCtx context.Context, f func(ctx context.Context) error) error {
171-
if sess, ok := inTransaction(parentCtx); ok {
172-
err := f(newContext(parentCtx, sess))
139+
if sess := getTransactionSession(parentCtx); sess != nil {
140+
err := f(withContextEngine(parentCtx, sess))
173141
if err != nil {
174142
// rollback immediately, in case the caller ignores returned error and tries to commit the transaction.
175143
_ = sess.Close()
@@ -195,7 +163,7 @@ func txWithNoCheck(parentCtx context.Context, f func(ctx context.Context) error)
195163
return err
196164
}
197165

198-
if err := f(newContext(parentCtx, sess)); err != nil {
166+
if err := f(withContextEngine(parentCtx, sess)); err != nil {
199167
return err
200168
}
201169

@@ -340,25 +308,13 @@ func TableName(bean any) string {
340308

341309
// InTransaction returns true if the engine is in a transaction otherwise return false
342310
func InTransaction(ctx context.Context) bool {
343-
_, ok := inTransaction(ctx)
344-
return ok
311+
return getTransactionSession(ctx) != nil
345312
}
346313

347-
func inTransaction(ctx context.Context) (*xorm.Session, bool) {
348-
e := getExistingEngine(ctx)
349-
if e == nil {
350-
return nil, false
351-
}
352-
353-
switch t := e.(type) {
354-
case *xorm.Engine:
355-
return nil, false
356-
case *xorm.Session:
357-
if t.IsInTx() {
358-
return t, true
359-
}
360-
return nil, false
361-
default:
362-
return nil, false
314+
func getTransactionSession(ctx context.Context) *xorm.Session {
315+
e, _ := ctx.Value(engineContextKey).(Engine)
316+
if sess, ok := e.(*xorm.Session); ok && sess.IsInTx() {
317+
return sess
363318
}
319+
return nil
364320
}

models/db/engine_init.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ func InitEngine(ctx context.Context) error {
8686
func SetDefaultEngine(ctx context.Context, eng *xorm.Engine) {
8787
xormEngine = eng
8888
xormEngine.SetDefaultContext(ctx)
89-
xormContext = &xormContextType{Context: ctx, engine: xormEngine}
9089
}
9190

9291
// UnsetDefaultEngine closes and unsets the default engine
@@ -98,7 +97,6 @@ func UnsetDefaultEngine() {
9897
_ = xormEngine.Close()
9998
xormEngine = nil
10099
}
101-
xormContext = nil
102100
}
103101

104102
// InitEngineWithMigration initializes a new xorm.Engine and sets it as the XORM's default context

modules/git/foreachref/parser.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ type Parser struct {
3030
func NewParser(r io.Reader, format Format) *Parser {
3131
scanner := bufio.NewScanner(r)
3232

33+
// default MaxScanTokenSize = 64 kiB may be too small for some references,
34+
// so allow the buffer to grow up to 4x if needed
35+
scanner.Buffer(nil, 4*bufio.MaxScanTokenSize)
36+
3337
// in addition to the reference delimiter we specified in the --format,
3438
// `git for-each-ref` will always add a newline after every reference.
3539
refDelim := make([]byte, 0, len(format.refDelim)+1)
@@ -70,6 +74,9 @@ func NewParser(r io.Reader, format Format) *Parser {
7074
// { "objecttype": "tag", "refname:short": "v1.16.4", "object": "f460b7543ed500e49c133c2cd85c8c55ee9dbe27" }
7175
func (p *Parser) Next() map[string]string {
7276
if !p.scanner.Scan() {
77+
if err := p.scanner.Err(); err != nil {
78+
p.err = err
79+
}
7380
return nil
7481
}
7582
fields, err := p.parseRef(p.scanner.Text())

routers/api/v1/repo/repo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,14 +736,14 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
736736
// Default branch only updated if changed and exist or the repository is empty
737737
updateRepoLicense := false
738738
if opts.DefaultBranch != nil && repo.DefaultBranch != *opts.DefaultBranch && (repo.IsEmpty || gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, *opts.DefaultBranch)) {
739+
repo.DefaultBranch = *opts.DefaultBranch
739740
if !repo.IsEmpty {
740-
if err := gitrepo.SetDefaultBranch(ctx, ctx.Repo.Repository, *opts.DefaultBranch); err != nil {
741+
if err := gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
741742
ctx.APIErrorInternal(err)
742743
return err
743744
}
744745
updateRepoLicense = true
745746
}
746-
repo.DefaultBranch = *opts.DefaultBranch
747747
}
748748

749749
if err := repo_service.UpdateRepository(ctx, repo, visibilityChanged); err != nil {

services/wiki/wiki.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func InitWiki(ctx context.Context, repo *repo_model.Repository) error {
4343
return fmt.Errorf("InitRepository: %w", err)
4444
} else if err = gitrepo.CreateDelegateHooks(ctx, repo.WikiStorageRepo()); err != nil {
4545
return fmt.Errorf("createDelegateHooks: %w", err)
46-
} else if _, _, err = git.NewCommand("symbolic-ref", "HEAD").AddDynamicArguments(git.BranchPrefix+repo.DefaultWikiBranch).RunStdString(ctx, &git.RunOpts{Dir: repo.WikiPath()}); err != nil {
46+
} else if err = gitrepo.SetDefaultBranch(ctx, repo.WikiStorageRepo(), repo.DefaultWikiBranch); err != nil {
4747
return fmt.Errorf("unable to set default wiki branch to %q: %w", repo.DefaultWikiBranch, err)
4848
}
4949
return nil

0 commit comments

Comments
 (0)