2021-09-16 16:34:54 +03:00
// Copyright 2021 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2021-09-16 16:34:54 +03:00
package private
import (
"bufio"
"context"
"fmt"
"io"
"os"
2021-12-10 11:14:24 +03:00
asymkey_model "code.gitea.io/gitea/models/asymkey"
2021-09-16 16:34:54 +03:00
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
)
// This file contains commit verification functions for refs passed across in hooks
func verifyCommits ( oldCommitID , newCommitID string , repo * git . Repository , env [ ] string ) error {
stdoutReader , stdoutWriter , err := os . Pipe ( )
if err != nil {
log . Error ( "Unable to create os.Pipe for %s" , repo . Path )
return err
}
defer func ( ) {
_ = stdoutReader . Close ( )
_ = stdoutWriter . Close ( )
} ( )
2023-08-30 05:27:53 +03:00
var command * git . Command
2023-12-14 00:02:00 +03:00
objectFormat , _ := repo . GetObjectFormat ( )
2023-12-17 14:56:08 +03:00
if oldCommitID == objectFormat . EmptyObjectID ( ) . String ( ) {
2023-08-30 05:27:53 +03:00
// When creating a new branch, the oldCommitID is empty, by using "newCommitID --not --all":
// List commits that are reachable by following the newCommitID, exclude "all" existing heads/tags commits
// So, it only lists the new commits received, doesn't list the commits already present in the receiving repository
command = git . NewCommand ( repo . Ctx , "rev-list" ) . AddDynamicArguments ( newCommitID ) . AddArguments ( "--not" , "--all" )
} else {
command = git . NewCommand ( repo . Ctx , "rev-list" ) . AddDynamicArguments ( oldCommitID + "..." + newCommitID )
}
2021-09-16 16:34:54 +03:00
// This is safe as force pushes are already forbidden
2023-08-30 05:27:53 +03:00
err = command . Run ( & git . RunOpts {
Env : env ,
Dir : repo . Path ,
Stdout : stdoutWriter ,
PipelineFunc : func ( ctx context . Context , cancel context . CancelFunc ) error {
_ = stdoutWriter . Close ( )
err := readAndVerifyCommitsFromShaReader ( stdoutReader , repo , env )
if err != nil {
log . Error ( "%v" , err )
cancel ( )
}
_ = stdoutReader . Close ( )
return err
} ,
} )
2021-09-16 16:34:54 +03:00
if err != nil && ! isErrUnverifiedCommit ( err ) {
log . Error ( "Unable to check commits from %s to %s in %s: %v" , oldCommitID , newCommitID , repo . Path , err )
}
return err
}
func readAndVerifyCommitsFromShaReader ( input io . ReadCloser , repo * git . Repository , env [ ] string ) error {
scanner := bufio . NewScanner ( input )
for scanner . Scan ( ) {
line := scanner . Text ( )
err := readAndVerifyCommit ( line , repo , env )
if err != nil {
log . Error ( "%v" , err )
return err
}
}
return scanner . Err ( )
}
func readAndVerifyCommit ( sha string , repo * git . Repository , env [ ] string ) error {
stdoutReader , stdoutWriter , err := os . Pipe ( )
if err != nil {
log . Error ( "Unable to create pipe for %s: %v" , repo . Path , err )
return err
}
defer func ( ) {
_ = stdoutReader . Close ( )
_ = stdoutWriter . Close ( )
} ( )
2023-12-19 10:20:47 +03:00
commitID := git . MustIDFromString ( sha )
2021-09-16 16:34:54 +03:00
2022-10-23 17:44:45 +03:00
return git . NewCommand ( repo . Ctx , "cat-file" , "commit" ) . AddDynamicArguments ( sha ) .
2022-04-01 05:55:30 +03:00
Run ( & git . RunOpts {
Env : env ,
Dir : repo . Path ,
Stdout : stdoutWriter ,
2022-02-11 15:47:22 +03:00
PipelineFunc : func ( ctx context . Context , cancel context . CancelFunc ) error {
2021-09-16 16:34:54 +03:00
_ = stdoutWriter . Close ( )
2023-12-14 00:02:00 +03:00
commit , err := git . CommitFromReader ( repo , commitID , stdoutReader )
2021-09-16 16:34:54 +03:00
if err != nil {
return err
}
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 16:37:34 +03:00
verification := asymkey_model . ParseCommitWithSignature ( ctx , commit )
2021-09-16 16:34:54 +03:00
if ! verification . Verified {
cancel ( )
return & errUnverifiedCommit {
commit . ID . String ( ) ,
}
}
return nil
2022-02-11 15:47:22 +03:00
} ,
} )
2021-09-16 16:34:54 +03:00
}
type errUnverifiedCommit struct {
sha string
}
func ( e * errUnverifiedCommit ) Error ( ) string {
return fmt . Sprintf ( "Unverified commit: %s" , e . sha )
}
func isErrUnverifiedCommit ( err error ) bool {
_ , ok := err . ( * errUnverifiedCommit )
return ok
}