Some checks failed
cache-seeder / prune (push) Has been skipped
cache-seeder / frontend (push) Failing after 3s
cache-seeder / python (push) Failing after 3s
cache-seeder / gobuild (push) Failing after 4s
cache-seeder / lint (bindata, lint-backend) (push) Failing after 3s
giteabot backport / giteabot (push) Has been skipped
giteabot / giteabot (push) Has been skipped
cache-prune / prune (push) Has been skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Has been cancelled
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Has been cancelled
release-nightly / nightly-binary (push) Has been cancelled
release-nightly / nightly-container (push) Has been cancelled
cron-renovate / cron-renovate (push) Has been skipped
52 lines
1.6 KiB
Go
52 lines
1.6 KiB
Go
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package git
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
|
|
"gitea.dev/modules/git/gitcmd"
|
|
"gitea.dev/modules/reqctx"
|
|
"gitea.dev/modules/util"
|
|
)
|
|
|
|
// contextKey is a value for use with context.WithValue.
|
|
type contextKey struct {
|
|
key string
|
|
}
|
|
|
|
// RepositoryFromContextOrOpen attempts to get the repository from the context or just opens it
|
|
// The caller must call Closer.Close()
|
|
func RepositoryFromContextOrOpen(ctx context.Context, repo RepositoryFacade) (*Repository, io.Closer, error) {
|
|
reqCtx := reqctx.FromContext(ctx)
|
|
if reqCtx != nil {
|
|
gitRepo, err := RepositoryFromRequestContextOrOpen(reqCtx, repo)
|
|
return gitRepo, util.NopCloser{}, err
|
|
}
|
|
gitRepo, err := OpenRepository(ctx, repo)
|
|
return gitRepo, gitRepo, err
|
|
}
|
|
|
|
// RepositoryFromRequestContextOrOpen opens the repository at the given relative path in the provided request context.
|
|
// Caller shouldn't close the git repo manually, the git repo will be automatically closed when the request context is done.
|
|
func RepositoryFromRequestContextOrOpen(ctx reqctx.RequestContext, repo RepositoryFacade) (*Repository, error) {
|
|
ck := contextKey{key: repo.GitRepoLocation()}
|
|
if gitRepo, ok := ctx.Value(ck).(*Repository); ok {
|
|
return gitRepo, nil
|
|
}
|
|
gitRepo, err := OpenRepository(ctx, repo)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ctx.AddCloser(gitRepo)
|
|
ctx.SetContextValue(ck, gitRepo)
|
|
return gitRepo, nil
|
|
}
|
|
|
|
func UpdateServerInfo(ctx context.Context, repo RepositoryFacade) error {
|
|
_, _, err := gitcmd.NewCommand("update-server-info").WithRepo(repo).RunStdBytes(ctx)
|
|
return err
|
|
}
|