use global configuration temp directories

This commit is contained in:
Lunny Xiao 2024-10-27 16:45:00 -07:00
parent fc25971f57
commit b362ffaa0c
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
3 changed files with 9 additions and 6 deletions

View File

@ -18,6 +18,7 @@ import (
"time" "time"
"code.gitea.io/gitea/modules/proxy" "code.gitea.io/gitea/modules/proxy"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
) )
@ -279,11 +280,11 @@ func GetDivergingCommits(ctx context.Context, repoPath, baseBranch, targetBranch
// CreateBundle create bundle content to the target path // CreateBundle create bundle content to the target path
func (repo *Repository) CreateBundle(ctx context.Context, commit string, out io.Writer) error { func (repo *Repository) CreateBundle(ctx context.Context, commit string, out io.Writer) error {
tmp, err := os.MkdirTemp(os.TempDir(), "gitea-bundle") tmp, err := os.MkdirTemp(setting.TempDir(), "gitea-bundle")
if err != nil { if err != nil {
return err return err
} }
defer os.RemoveAll(tmp) defer util.RemoveAll(tmp)
env := append(os.Environ(), "GIT_OBJECT_DIRECTORY="+filepath.Join(repo.Path, "objects")) env := append(os.Environ(), "GIT_OBJECT_DIRECTORY="+filepath.Join(repo.Path, "objects"))
_, _, err = NewCommand(ctx, "init", "--bare").RunStdString(&RunOpts{Dir: tmp, Env: env}) _, _, err = NewCommand(ctx, "init", "--bare").RunStdString(&RunOpts{Dir: tmp, Env: env})

View File

@ -11,6 +11,7 @@ import (
"strings" "strings"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
) )
@ -51,7 +52,7 @@ func (repo *Repository) readTreeToIndex(id ObjectID, indexFilename ...string) er
// ReadTreeToTemporaryIndex reads a treeish to a temporary index file // ReadTreeToTemporaryIndex reads a treeish to a temporary index file
func (repo *Repository) ReadTreeToTemporaryIndex(treeish string) (filename, tmpDir string, cancel context.CancelFunc, err error) { func (repo *Repository) ReadTreeToTemporaryIndex(treeish string) (filename, tmpDir string, cancel context.CancelFunc, err error) {
tmpDir, err = os.MkdirTemp(os.TempDir(), "index") tmpDir, err = os.MkdirTemp(setting.TempDir(), "index")
if err != nil { if err != nil {
return filename, tmpDir, cancel, err return filename, tmpDir, cancel, err
} }

View File

@ -4,6 +4,7 @@
package repository package repository
import ( import (
"context"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@ -16,9 +17,9 @@ import (
// localCopyPath returns the local repository temporary copy path. // localCopyPath returns the local repository temporary copy path.
func localCopyPath() string { func localCopyPath() string {
if setting.Repository.Local.LocalCopyPath == "" { if setting.Repository.Local.LocalCopyPath == "" {
return filepath.Join(os.TempDir(), "local-repo") return filepath.Join(setting.TempDir(), "local-repo")
} else if !filepath.IsAbs(setting.Repository.Local.LocalCopyPath) { } else if !filepath.IsAbs(setting.Repository.Local.LocalCopyPath) {
return filepath.Join(os.TempDir(), setting.Repository.Local.LocalCopyPath) return filepath.Join(setting.TempDir(), setting.Repository.Local.LocalCopyPath)
} }
return setting.Repository.Local.LocalCopyPath return setting.Repository.Local.LocalCopyPath
} }
@ -30,7 +31,7 @@ func CleanUpTemporaryPaths() {
} }
// CreateTemporaryPath creates a temporary path // CreateTemporaryPath creates a temporary path
func CreateTemporaryPath(prefix string) (string, func(), error) { func CreateTemporaryPath(prefix string) (string, context.CancelFunc, error) {
if err := os.MkdirAll(localCopyPath(), os.ModePerm); err != nil { if err := os.MkdirAll(localCopyPath(), os.ModePerm); err != nil {
log.Error("Unable to create localcopypath directory: %s (%v)", localCopyPath(), err) log.Error("Unable to create localcopypath directory: %s (%v)", localCopyPath(), err)
return "", func() {}, fmt.Errorf("failed to create localcopypath directory %s: %w", localCopyPath(), err) return "", func() {}, fmt.Errorf("failed to create localcopypath directory %s: %w", localCopyPath(), err)