mirror of
https://github.com/go-gitea/gitea.git
synced 2025-07-01 11:05:03 +02:00
uniform all temporary directories
This commit is contained in:
parent
d70af38447
commit
7b0f3e6473
@ -15,6 +15,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/modules/util"
|
"code.gitea.io/gitea/modules/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -32,7 +33,7 @@ func main() {
|
|||||||
flag.StringVar(&githubApiToken, "token", "", "github api token")
|
flag.StringVar(&githubApiToken, "token", "", "github api token")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
file, err := os.CreateTemp(os.TempDir(), prefix)
|
file, err := os.CreateTemp(setting.TempDir(), prefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to create temp file. %s", err)
|
log.Fatalf("Failed to create temp file. %s", err)
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@ import (
|
|||||||
|
|
||||||
"code.gitea.io/gitea/build/license"
|
"code.gitea.io/gitea/build/license"
|
||||||
"code.gitea.io/gitea/modules/json"
|
"code.gitea.io/gitea/modules/json"
|
||||||
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/modules/util"
|
"code.gitea.io/gitea/modules/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -39,7 +40,7 @@ func main() {
|
|||||||
flag.StringVar(&githubApiToken, "token", "", "github api token")
|
flag.StringVar(&githubApiToken, "token", "", "github api token")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
file, err := os.CreateTemp(os.TempDir(), prefix)
|
file, err := os.CreateTemp(setting.TempDir(), prefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to create temp file. %s", err)
|
log.Fatalf("Failed to create temp file. %s", err)
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,7 @@ var CmdDump = &cli.Command{
|
|||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "tempdir",
|
Name: "tempdir",
|
||||||
Aliases: []string{"t"},
|
Aliases: []string{"t"},
|
||||||
Value: os.TempDir(),
|
Value: setting.TempDir(),
|
||||||
Usage: "Temporary dir path",
|
Usage: "Temporary dir path",
|
||||||
},
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
|
@ -141,7 +141,7 @@ func MainTest(m *testing.M) {
|
|||||||
setting.CustomConf = giteaConf
|
setting.CustomConf = giteaConf
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpDataPath, err := os.MkdirTemp("", "data")
|
tmpDataPath, err := os.MkdirTemp(setting.TempDir(), "data")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Unable to create temporary data path %v\n", err)
|
fmt.Printf("Unable to create temporary data path %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -124,12 +124,12 @@ func MainTest(m *testing.M, testOpts ...*TestOptions) {
|
|||||||
setting.SSH.Domain = "try.gitea.io"
|
setting.SSH.Domain = "try.gitea.io"
|
||||||
setting.Database.Type = "sqlite3"
|
setting.Database.Type = "sqlite3"
|
||||||
setting.Repository.DefaultBranch = "master" // many test code still assume that default branch is called "master"
|
setting.Repository.DefaultBranch = "master" // many test code still assume that default branch is called "master"
|
||||||
repoRootPath, err := os.MkdirTemp(os.TempDir(), "repos")
|
repoRootPath, err := os.MkdirTemp(setting.TempDir(), "repos")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fatalTestError("TempDir: %v\n", err)
|
fatalTestError("TempDir: %v\n", err)
|
||||||
}
|
}
|
||||||
setting.RepoRootPath = repoRootPath
|
setting.RepoRootPath = repoRootPath
|
||||||
appDataPath, err := os.MkdirTemp(os.TempDir(), "appdata")
|
appDataPath, err := os.MkdirTemp(setting.TempDir(), "appdata")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fatalTestError("TempDir: %v\n", err)
|
fatalTestError("TempDir: %v\n", err)
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"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"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -195,7 +196,7 @@ func tryCreateBlameIgnoreRevsFile(commit *Commit) *string {
|
|||||||
}
|
}
|
||||||
defer r.Close()
|
defer r.Close()
|
||||||
|
|
||||||
f, err := os.CreateTemp("", "gitea_git-blame-ignore-revs")
|
f, err := os.CreateTemp(setting.TempDir(), "gitea_git-blame-ignore-revs")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,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("", "index")
|
tmpDir, err = os.MkdirTemp(os.TempDir(), "index")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return filename, tmpDir, cancel, err
|
return filename, tmpDir, cancel, err
|
||||||
}
|
}
|
||||||
|
2
modules/markup/external/external.go
vendored
2
modules/markup/external/external.go
vendored
@ -89,7 +89,7 @@ func (p *Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.
|
|||||||
|
|
||||||
if p.IsInputFile {
|
if p.IsInputFile {
|
||||||
// write to temp file
|
// write to temp file
|
||||||
f, err := os.CreateTemp("", "gitea_input")
|
f, err := os.CreateTemp(setting.TempDir(), "gitea_input")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("%s create temp file when rendering %s failed: %w", p.Name(), p.Command, err)
|
return fmt.Errorf("%s create temp file when rendering %s failed: %w", p.Name(), p.Command, err)
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ package repository
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
@ -14,30 +13,37 @@ import (
|
|||||||
"code.gitea.io/gitea/modules/util"
|
"code.gitea.io/gitea/modules/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LocalCopyPath returns the local repository temporary copy path.
|
// localCopyPath returns the local repository temporary copy path.
|
||||||
func LocalCopyPath() string {
|
func localCopyPath() string {
|
||||||
if filepath.IsAbs(setting.Repository.Local.LocalCopyPath) {
|
return filepath.Join(setting.TempDir(), "local-repo")
|
||||||
return setting.Repository.Local.LocalCopyPath
|
}
|
||||||
|
|
||||||
|
func CleanUpTemporaryPaths() {
|
||||||
|
if err := util.RemoveAll(localCopyPath()); err != nil {
|
||||||
|
log.Error("Unable to remove local repository temporary copy path: %s (%v)", localCopyPath(), err)
|
||||||
}
|
}
|
||||||
return path.Join(setting.AppDataPath, setting.Repository.Local.LocalCopyPath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateTemporaryPath creates a temporary path
|
// CreateTemporaryPath creates a temporary path
|
||||||
func CreateTemporaryPath(prefix string) (string, error) {
|
func CreateTemporaryPath(prefix string) (string, func(), 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 "", fmt.Errorf("Failed to create localcopypath directory %s: %w", LocalCopyPath(), err)
|
return "", func() {}, fmt.Errorf("failed to create localcopypath directory %s: %w", localCopyPath(), err)
|
||||||
}
|
}
|
||||||
basePath, err := os.MkdirTemp(LocalCopyPath(), prefix+".git")
|
basePath, err := os.MkdirTemp(localCopyPath(), prefix+".git")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Unable to create temporary directory: %s-*.git (%v)", prefix, err)
|
log.Error("Unable to create temporary directory: %s-*.git (%v)", prefix, err)
|
||||||
return "", fmt.Errorf("Failed to create dir %s-*.git: %w", prefix, err)
|
return "", func() {}, fmt.Errorf("failed to create dir %s-*.git: %w", prefix, err)
|
||||||
}
|
}
|
||||||
return basePath, nil
|
return basePath, func() {
|
||||||
|
if err := removeTemporaryPath(basePath); err != nil {
|
||||||
|
log.Error("Unable to remove temporary directory: %s (%v)", basePath, err)
|
||||||
|
}
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveTemporaryPath removes the temporary path
|
// removeTemporaryPath removes the temporary path
|
||||||
func RemoveTemporaryPath(basePath string) error {
|
func removeTemporaryPath(basePath string) error {
|
||||||
if _, err := os.Stat(basePath); !os.IsNotExist(err) {
|
if _, err := os.Stat(basePath); !os.IsNotExist(err) {
|
||||||
return util.RemoveAll(basePath)
|
return util.RemoveAll(basePath)
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
package setting
|
package setting
|
||||||
|
|
||||||
|
import "os"
|
||||||
|
|
||||||
// Global settings
|
// Global settings
|
||||||
var (
|
var (
|
||||||
// RunUser is the OS user that Gitea is running as. ini:"RUN_USER"
|
// RunUser is the OS user that Gitea is running as. ini:"RUN_USER"
|
||||||
@ -16,3 +18,8 @@ var (
|
|||||||
// AppName is the Application name, used in the page title. ini: "APP_NAME"
|
// AppName is the Application name, used in the page title. ini: "APP_NAME"
|
||||||
AppName string
|
AppName string
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TempDir returns the OS temp directory
|
||||||
|
func TempDir() string {
|
||||||
|
return os.TempDir()
|
||||||
|
}
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
package setting
|
package setting
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@ -124,7 +123,7 @@ func loadSSHFrom(rootCfg ConfigProvider) {
|
|||||||
if len(serverMACs) > 0 {
|
if len(serverMACs) > 0 {
|
||||||
SSH.ServerMACs = serverMACs
|
SSH.ServerMACs = serverMACs
|
||||||
}
|
}
|
||||||
SSH.KeyTestPath = os.TempDir()
|
SSH.KeyTestPath = TempDir()
|
||||||
if err = sec.MapTo(&SSH); err != nil {
|
if err = sec.MapTo(&SSH); err != nil {
|
||||||
log.Fatal("Failed to map SSH settings: %v", err)
|
log.Fatal("Failed to map SSH settings: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ func NewLocalStorage(ctx context.Context, config *setting.Storage) (ObjectStorag
|
|||||||
}
|
}
|
||||||
|
|
||||||
if config.TemporaryPath == "" {
|
if config.TemporaryPath == "" {
|
||||||
config.TemporaryPath = filepath.Join(config.Path, "tmp")
|
config.TemporaryPath = filepath.Join(setting.TempDir(), filepath.Base(config.Path))
|
||||||
}
|
}
|
||||||
if !filepath.IsAbs(config.TemporaryPath) {
|
if !filepath.IsAbs(config.TemporaryPath) {
|
||||||
return nil, fmt.Errorf("LocalStorageConfig.TemporaryPath should be an absolute path, but not: %q", config.TemporaryPath)
|
return nil, fmt.Errorf("LocalStorageConfig.TemporaryPath should be an absolute path, but not: %q", config.TemporaryPath)
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
package storage
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -56,6 +55,6 @@ func TestBuildLocalPath(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalStorageIterator(t *testing.T) {
|
func TestLocalStorageIterator(t *testing.T) {
|
||||||
dir := filepath.Join(os.TempDir(), "TestLocalStorageIteratorTestDir")
|
dir := filepath.Join(t.TempDir(), "TestLocalStorageIteratorTestDir")
|
||||||
testStorageIterator(t, setting.LocalStorageType, &setting.Storage{Path: dir})
|
testStorageIterator(t, setting.LocalStorageType, &setting.Storage{Path: dir})
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,8 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/modules/setting"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -73,7 +75,7 @@ func (b *FileBackedBuffer) Write(p []byte) (int, error) {
|
|||||||
n, err = b.file.Write(p)
|
n, err = b.file.Write(p)
|
||||||
} else {
|
} else {
|
||||||
if b.size+int64(len(p)) > b.maxMemorySize {
|
if b.size+int64(len(p)) > b.maxMemorySize {
|
||||||
b.file, err = os.CreateTemp("", "gitea-buffer-")
|
b.file, err = os.CreateTemp(setting.TempDir(), "gitea-buffer-")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
@ -11,13 +11,15 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/modules/setting"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCopyFile(t *testing.T) {
|
func TestCopyFile(t *testing.T) {
|
||||||
testContent := []byte("hello")
|
testContent := []byte("hello")
|
||||||
|
|
||||||
tmpDir := os.TempDir()
|
tmpDir := setting.TempDir()
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
srcFile := fmt.Sprintf("%s/copy-test-%d-src.txt", tmpDir, now.UnixMicro())
|
srcFile := fmt.Sprintf("%s/copy-test-%d-src.txt", tmpDir, now.UnixMicro())
|
||||||
dstFile := fmt.Sprintf("%s/copy-test-%d-dst.txt", tmpDir, now.UnixMicro())
|
dstFile := fmt.Sprintf("%s/copy-test-%d-dst.txt", tmpDir, now.UnixMicro())
|
||||||
|
@ -303,7 +303,7 @@ var (
|
|||||||
|
|
||||||
func dummyInfoRefs(ctx *context.Context) {
|
func dummyInfoRefs(ctx *context.Context) {
|
||||||
infoRefsOnce.Do(func() {
|
infoRefsOnce.Do(func() {
|
||||||
tmpDir, err := os.MkdirTemp(os.TempDir(), "gitea-info-refs-cache")
|
tmpDir, err := os.MkdirTemp(setting.TempDir(), "gitea-info-refs-cache")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Failed to create temp dir for git-receive-pack cache: %v", err)
|
log.Error("Failed to create temp dir for git-receive-pack cache: %v", err)
|
||||||
return
|
return
|
||||||
|
@ -109,17 +109,13 @@ func LFSLocks(ctx *context.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Clone base repo.
|
// Clone base repo.
|
||||||
tmpBasePath, err := repo_module.CreateTemporaryPath("locks")
|
tmpBasePath, cancel, err := repo_module.CreateTemporaryPath("locks")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Failed to create temporary path: %v", err)
|
log.Error("Failed to create temporary path: %v", err)
|
||||||
ctx.ServerError("LFSLocks", err)
|
ctx.ServerError("LFSLocks", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer func() {
|
defer cancel()
|
||||||
if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
|
|
||||||
log.Error("LFSLocks: RemoveTemporaryPath: %v", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
if err := git.Clone(ctx, ctx.Repo.Repository.RepoPath(), tmpBasePath, git.CloneRepoOptions{
|
if err := git.Clone(ctx, ctx.Repo.Repository.RepoPath(), tmpBasePath, git.CloneRepoOptions{
|
||||||
Bare: true,
|
Bare: true,
|
||||||
|
@ -346,7 +346,7 @@ func checkConflicts(ctx context.Context, pr *issues_model.PullRequest, gitRepo *
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 3b. Create a plain patch from head to base
|
// 3b. Create a plain patch from head to base
|
||||||
tmpPatchFile, err := os.CreateTemp("", "patch")
|
tmpPatchFile, err := os.CreateTemp(setting.TempDir(), "patch")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Unable to create temporary patch file! Error: %v", err)
|
log.Error("Unable to create temporary patch file! Error: %v", err)
|
||||||
return false, fmt.Errorf("unable to create temporary patch file! Error: %w", err)
|
return false, fmt.Errorf("unable to create temporary patch file! Error: %w", err)
|
||||||
|
@ -73,11 +73,13 @@ func createTemporaryRepoForPR(ctx context.Context, pr *issues_model.PullRequest)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Clone base repo.
|
// Clone base repo.
|
||||||
tmpBasePath, err := repo_module.CreateTemporaryPath("pull")
|
tmpBasePath, cancel, err := repo_module.CreateTemporaryPath("pull")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("CreateTemporaryPath[%-v]: %v", pr, err)
|
log.Error("CreateTemporaryPath[%-v]: %v", pr, err)
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
prCtx = &prContext{
|
prCtx = &prContext{
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
tmpBasePath: tmpBasePath,
|
tmpBasePath: tmpBasePath,
|
||||||
@ -85,11 +87,6 @@ func createTemporaryRepoForPR(ctx context.Context, pr *issues_model.PullRequest)
|
|||||||
outbuf: &strings.Builder{},
|
outbuf: &strings.Builder{},
|
||||||
errbuf: &strings.Builder{},
|
errbuf: &strings.Builder{},
|
||||||
}
|
}
|
||||||
cancel = func() {
|
|
||||||
if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
|
|
||||||
log.Error("Error whilst removing removing temporary repo for %-v: %v", pr, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
baseRepoPath := pr.BaseRepo.RepoPath()
|
baseRepoPath := pr.BaseRepo.RepoPath()
|
||||||
headRepoPath := pr.HeadRepo.RepoPath()
|
headRepoPath := pr.HeadRepo.RepoPath()
|
||||||
|
@ -134,6 +134,10 @@ func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func createRepoTempDir(repoName string) (string, error) {
|
||||||
|
return os.MkdirTemp(filepath.Join(setting.TempDir(), "repos"), "gitea-"+repoName)
|
||||||
|
}
|
||||||
|
|
||||||
// InitRepository initializes README and .gitignore if needed.
|
// InitRepository initializes README and .gitignore if needed.
|
||||||
func initRepository(ctx context.Context, repoPath string, u *user_model.User, repo *repo_model.Repository, opts CreateRepoOptions) (err error) {
|
func initRepository(ctx context.Context, repoPath string, u *user_model.User, repo *repo_model.Repository, opts CreateRepoOptions) (err error) {
|
||||||
if err = repo_module.CheckInitRepository(ctx, repo.OwnerName, repo.Name, opts.ObjectFormatName); err != nil {
|
if err = repo_module.CheckInitRepository(ctx, repo.OwnerName, repo.Name, opts.ObjectFormatName); err != nil {
|
||||||
@ -142,9 +146,9 @@ func initRepository(ctx context.Context, repoPath string, u *user_model.User, re
|
|||||||
|
|
||||||
// Initialize repository according to user's choice.
|
// Initialize repository according to user's choice.
|
||||||
if opts.AutoInit {
|
if opts.AutoInit {
|
||||||
tmpDir, err := os.MkdirTemp(os.TempDir(), "gitea-"+repo.Name)
|
tmpDir, err := createRepoTempDir(repo.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to create temp dir for repository %s: %w", repo.RepoPath(), err)
|
return fmt.Errorf("failed to create temp dir for repository %s: %w", repo.RepoPath(), err)
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := util.RemoveAll(tmpDir); err != nil {
|
if err := util.RemoveAll(tmpDir); err != nil {
|
||||||
|
@ -30,23 +30,28 @@ type TemporaryUploadRepository struct {
|
|||||||
repo *repo_model.Repository
|
repo *repo_model.Repository
|
||||||
gitRepo *git.Repository
|
gitRepo *git.Repository
|
||||||
basePath string
|
basePath string
|
||||||
|
cancel func()
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTemporaryUploadRepository creates a new temporary upload repository
|
// NewTemporaryUploadRepository creates a new temporary upload repository
|
||||||
func NewTemporaryUploadRepository(ctx context.Context, repo *repo_model.Repository) (*TemporaryUploadRepository, error) {
|
func NewTemporaryUploadRepository(ctx context.Context, repo *repo_model.Repository) (*TemporaryUploadRepository, error) {
|
||||||
basePath, err := repo_module.CreateTemporaryPath("upload")
|
basePath, cancel, err := repo_module.CreateTemporaryPath("upload")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
t := &TemporaryUploadRepository{ctx: ctx, repo: repo, basePath: basePath}
|
t := &TemporaryUploadRepository{
|
||||||
|
ctx: ctx, repo: repo,
|
||||||
|
basePath: basePath,
|
||||||
|
cancel: cancel,
|
||||||
|
}
|
||||||
return t, nil
|
return t, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close the repository cleaning up all files
|
// Close the repository cleaning up all files
|
||||||
func (t *TemporaryUploadRepository) Close() {
|
func (t *TemporaryUploadRepository) Close() {
|
||||||
defer t.gitRepo.Close()
|
defer t.gitRepo.Close()
|
||||||
if err := repo_module.RemoveTemporaryPath(t.basePath); err != nil {
|
if t.cancel != nil {
|
||||||
log.Error("Failed to remove temporary path %s: %v", t.basePath, err)
|
t.cancel()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,6 +23,7 @@ import (
|
|||||||
"code.gitea.io/gitea/modules/gitrepo"
|
"code.gitea.io/gitea/modules/gitrepo"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
repo_module "code.gitea.io/gitea/modules/repository"
|
repo_module "code.gitea.io/gitea/modules/repository"
|
||||||
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/modules/util"
|
"code.gitea.io/gitea/modules/util"
|
||||||
|
|
||||||
"github.com/gobwas/glob"
|
"github.com/gobwas/glob"
|
||||||
@ -253,7 +254,7 @@ func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *r
|
|||||||
}
|
}
|
||||||
|
|
||||||
func generateGitContent(ctx context.Context, repo, templateRepo, generateRepo *repo_model.Repository) (err error) {
|
func generateGitContent(ctx context.Context, repo, templateRepo, generateRepo *repo_model.Repository) (err error) {
|
||||||
tmpDir, err := os.MkdirTemp(os.TempDir(), "gitea-"+repo.Name)
|
tmpDir, err := os.MkdirTemp(setting.TempDir(), "gitea-"+repo.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to create temp dir for repository %s: %w", repo.RepoPath(), err)
|
return fmt.Errorf("Failed to create temp dir for repository %s: %w", repo.RepoPath(), err)
|
||||||
}
|
}
|
||||||
|
@ -107,7 +107,10 @@ func Init(ctx context.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
system_model.RemoveAllWithNotice(ctx, "Clean up temporary repository uploads", setting.Repository.Upload.TempPath)
|
system_model.RemoveAllWithNotice(ctx, "Clean up temporary repository uploads", setting.Repository.Upload.TempPath)
|
||||||
system_model.RemoveAllWithNotice(ctx, "Clean up temporary repositories", repo_module.LocalCopyPath())
|
repo_module.CleanUpTemporaryPaths()
|
||||||
|
if err := system_model.CreateNotice(db.DefaultContext, system_model.NoticeRepository, "Clean up temporary repositories"); err != nil {
|
||||||
|
log.Error("CreateRepositoryNotice: %v", err)
|
||||||
|
}
|
||||||
if err := initPushQueue(); err != nil {
|
if err := initPushQueue(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -102,15 +102,11 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
|
|||||||
|
|
||||||
hasDefaultBranch := git.IsBranchExist(ctx, repo.WikiPath(), repo.DefaultWikiBranch)
|
hasDefaultBranch := git.IsBranchExist(ctx, repo.WikiPath(), repo.DefaultWikiBranch)
|
||||||
|
|
||||||
basePath, err := repo_module.CreateTemporaryPath("update-wiki")
|
basePath, cancel, err := repo_module.CreateTemporaryPath("update-wiki")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer func() {
|
defer cancel()
|
||||||
if err := repo_module.RemoveTemporaryPath(basePath); err != nil {
|
|
||||||
log.Error("Merge: RemoveTemporaryPath: %s", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
cloneOpts := git.CloneRepoOptions{
|
cloneOpts := git.CloneRepoOptions{
|
||||||
Bare: true,
|
Bare: true,
|
||||||
@ -264,15 +260,11 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
|
|||||||
return fmt.Errorf("InitWiki: %w", err)
|
return fmt.Errorf("InitWiki: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
basePath, err := repo_module.CreateTemporaryPath("update-wiki")
|
basePath, cancel, err := repo_module.CreateTemporaryPath("update-wiki")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer func() {
|
defer cancel()
|
||||||
if err := repo_module.RemoveTemporaryPath(basePath); err != nil {
|
|
||||||
log.Error("Merge: RemoveTemporaryPath: %s", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
if err := git.Clone(ctx, repo.WikiPath(), basePath, git.CloneRepoOptions{
|
if err := git.Clone(ctx, repo.WikiPath(), basePath, git.CloneRepoOptions{
|
||||||
Bare: true,
|
Bare: true,
|
||||||
|
@ -44,7 +44,7 @@ func TestDumpRestore(t *testing.T) {
|
|||||||
|
|
||||||
reponame := "repo1"
|
reponame := "repo1"
|
||||||
|
|
||||||
basePath, err := os.MkdirTemp("", reponame)
|
basePath, err := os.MkdirTemp(setting.TempDir(), reponame)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
defer util.RemoveAll(basePath)
|
defer util.RemoveAll(basePath)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user