Skip to content

Commit e40bfb8

Browse files
committed
refactor
1 parent 834bb59 commit e40bfb8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+230
-238
lines changed

cmd/migrate_storage_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func TestMigratePackages(t *testing.T) {
6868

6969
entries, err := os.ReadDir(p)
7070
assert.NoError(t, err)
71-
assert.Len(t, entries, 1) // tmp directory should not be under storage any more
71+
assert.Len(t, entries, 2)
7272
assert.Equal(t, "01", entries[0].Name())
73+
assert.Equal(t, "tmp", entries[1].Name())
7374
}

cmd/web.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ func serveInstalled(ctx *cli.Context) error {
213213
log.Fatal("Can not find APP_DATA_PATH %q", setting.AppDataPath)
214214
}
215215

216+
setting.AppDataTempDir("").RemoveOutdated()
217+
216218
// Override the provided port number within the configuration
217219
if ctx.IsSet("port") {
218220
if err := setPort(ctx.String("port")); err != nil {

custom/conf/app.example.ini

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ RUN_USER = ; git
5353
;; The working directory, see the comment of AppWorkPath above
5454
;WORK_PATH =
5555

56-
;; The temporary directory, defaults to a directory named gitea under the system temporary directory
57-
;; All other temporary directories are relative to this directory by default
58-
;TEMP_PATH =
59-
6056
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
6157
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
6258
[server]

models/issues/issue_update.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,7 @@ func DeleteOrphanedIssues(ctx context.Context) error {
846846

847847
// Remove issue attachment files.
848848
for i := range attachmentPaths {
849+
// FIXME: it's not right, because the attachment might not be on local filesystem
849850
system_model.RemoveAllWithNotice(ctx, "Delete issue attachment", attachmentPaths[i])
850851
}
851852
return nil

models/migrations/base/tests.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"code.gitea.io/gitea/models/unittest"
1616
"code.gitea.io/gitea/modules/git"
1717
"code.gitea.io/gitea/modules/setting"
18-
"code.gitea.io/gitea/modules/temp"
18+
"code.gitea.io/gitea/modules/tempdir"
1919
"code.gitea.io/gitea/modules/test"
2020
"code.gitea.io/gitea/modules/testlogger"
2121

@@ -115,7 +115,7 @@ func MainTest(m *testing.M) {
115115
setting.CustomConf = giteaConf
116116
}
117117

118-
tmpDataPath, cleanup, err := temp.MkdirTemp("data")
118+
tmpDataPath, cleanup, err := tempdir.OsTempDir("gitea-test").MkdirTempRandom("data")
119119
if err != nil {
120120
testlogger.Fatalf("Unable to create temporary data path %v\n", err)
121121
}
@@ -124,7 +124,7 @@ func MainTest(m *testing.M) {
124124
setting.CustomPath = filepath.Join(setting.AppWorkPath, "custom")
125125
setting.AppDataPath = tmpDataPath
126126

127-
unittest.InitSettings()
127+
unittest.InitSettingsForTesting()
128128
if err = git.InitFull(context.Background()); err != nil {
129129
testlogger.Fatalf("Unable to InitFull: %v\n", err)
130130
}

models/repo/upload.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,10 @@ func init() {
5151
db.RegisterModel(new(Upload))
5252
}
5353

54-
// UploadLocalPath returns where uploads is stored in local file system based on given UUID.
55-
func UploadLocalPath(uuid string) string {
56-
return filepath.Join(setting.GetRepositoryUploadTempPath(), uuid[0:1], uuid[1:2], uuid)
57-
}
58-
59-
// LocalPath returns where uploads are temporarily stored in local file system.
54+
// LocalPath returns where uploads are temporarily stored in local file system based on given UUID.
6055
func (upload *Upload) LocalPath() string {
61-
return UploadLocalPath(upload.UUID)
56+
uuid := upload.UUID
57+
return setting.AppDataTempDir("repo-uploads").JoinPath(uuid[0:1], uuid[1:2], uuid)
6258
}
6359

6460
// NewUpload creates a new upload object.

models/unittest/testdb.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"code.gitea.io/gitea/modules/setting"
2121
"code.gitea.io/gitea/modules/setting/config"
2222
"code.gitea.io/gitea/modules/storage"
23-
"code.gitea.io/gitea/modules/temp"
23+
"code.gitea.io/gitea/modules/tempdir"
2424
"code.gitea.io/gitea/modules/test"
2525
"code.gitea.io/gitea/modules/util"
2626

@@ -36,8 +36,8 @@ func fatalTestError(fmtStr string, args ...any) {
3636
os.Exit(1)
3737
}
3838

39-
// InitSettings initializes config provider and load common settings for tests
40-
func InitSettings() {
39+
// InitSettingsForTesting initializes config provider and load common settings for tests
40+
func InitSettingsForTesting() {
4141
setting.IsInTesting = true
4242
log.OsExiter = func(code int) {
4343
if code != 0 {
@@ -76,7 +76,7 @@ func MainTest(m *testing.M, testOptsArg ...*TestOptions) {
7676
testOpts := util.OptionalArg(testOptsArg, &TestOptions{})
7777
giteaRoot = test.SetupGiteaRoot()
7878
setting.CustomPath = filepath.Join(giteaRoot, "custom")
79-
InitSettings()
79+
InitSettingsForTesting()
8080

8181
fixturesOpts := FixturesOptions{Dir: filepath.Join(giteaRoot, "models", "fixtures"), Files: testOpts.FixtureFiles}
8282
if err := CreateTestEngine(fixturesOpts); err != nil {
@@ -93,14 +93,14 @@ func MainTest(m *testing.M, testOptsArg ...*TestOptions) {
9393
setting.SSH.Domain = "try.gitea.io"
9494
setting.Database.Type = "sqlite3"
9595
setting.Repository.DefaultBranch = "master" // many test code still assume that default branch is called "master"
96-
repoRootPath, cleanup1, err := temp.MkdirTemp("repos")
96+
repoRootPath, cleanup1, err := tempdir.OsTempDir("gitea-test").MkdirTempRandom("repos")
9797
if err != nil {
9898
fatalTestError("TempDir: %v\n", err)
9999
}
100100
defer cleanup1()
101101

102102
setting.RepoRootPath = repoRootPath
103-
appDataPath, cleanup2, err := temp.MkdirTemp("appdata")
103+
appDataPath, cleanup2, err := tempdir.OsTempDir("gitea-test").MkdirTempRandom("appdata")
104104
if err != nil {
105105
fatalTestError("TempDir: %v\n", err)
106106
}

modules/git/blame.go

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import (
1111
"os"
1212

1313
"code.gitea.io/gitea/modules/log"
14-
"code.gitea.io/gitea/modules/temp"
15-
"code.gitea.io/gitea/modules/util"
14+
"code.gitea.io/gitea/modules/setting"
1615
)
1716

1817
// BlamePart represents block of blame - continuous lines with one sha
@@ -30,12 +29,13 @@ type BlameReader struct {
3029
bufferedReader *bufio.Reader
3130
done chan error
3231
lastSha *string
33-
ignoreRevsFile *string
32+
ignoreRevsFile string
3433
objectFormat ObjectFormat
34+
cleanupFuncs []func()
3535
}
3636

3737
func (r *BlameReader) UsesIgnoreRevs() bool {
38-
return r.ignoreRevsFile != nil
38+
return r.ignoreRevsFile != ""
3939
}
4040

4141
// NextPart returns next part of blame (sequential code lines with the same commit)
@@ -123,36 +123,37 @@ func (r *BlameReader) Close() error {
123123
r.bufferedReader = nil
124124
_ = r.reader.Close()
125125
_ = r.output.Close()
126-
if r.ignoreRevsFile != nil {
127-
_ = util.Remove(*r.ignoreRevsFile)
126+
for _, cleanup := range r.cleanupFuncs {
127+
if cleanup != nil {
128+
cleanup()
129+
}
128130
}
129131
return err
130132
}
131133

132134
// CreateBlameReader creates reader for given repository, commit and file
133135
func CreateBlameReader(ctx context.Context, objectFormat ObjectFormat, repoPath string, commit *Commit, file string, bypassBlameIgnore bool) (*BlameReader, error) {
134-
var ignoreRevsFile *string
135-
if DefaultFeatures().CheckVersionAtLeast("2.23") && !bypassBlameIgnore {
136-
ignoreRevsFile = tryCreateBlameIgnoreRevsFile(commit)
136+
reader, stdout, err := os.Pipe()
137+
if err != nil {
138+
return nil, err
137139
}
138140

139141
cmd := NewCommandNoGlobals("blame", "--porcelain")
140-
if ignoreRevsFile != nil {
141-
// Possible improvement: use --ignore-revs-file /dev/stdin on unix
142-
// There is no equivalent on Windows. May be implemented if Gitea uses an external git backend.
143-
cmd.AddOptionValues("--ignore-revs-file", *ignoreRevsFile)
144-
}
145-
cmd.AddDynamicArguments(commit.ID.String()).AddDashesAndList(file)
146-
reader, stdout, err := os.Pipe()
147-
if err != nil {
148-
if ignoreRevsFile != nil {
149-
_ = util.Remove(*ignoreRevsFile)
142+
143+
var ignoreRevsFileName string
144+
var ignoreRevsFileCleanup func() // TODO: maybe it should check the returned err in a defer func to make sure the cleanup could always be executed correctly
145+
if DefaultFeatures().CheckVersionAtLeast("2.23") && !bypassBlameIgnore {
146+
ignoreRevsFileName, ignoreRevsFileCleanup = tryCreateBlameIgnoreRevsFile(commit)
147+
if ignoreRevsFileName != "" {
148+
// Possible improvement: use --ignore-revs-file /dev/stdin on unix
149+
// There is no equivalent on Windows. May be implemented if Gitea uses an external git backend.
150+
cmd.AddOptionValues("--ignore-revs-file", ignoreRevsFileName)
150151
}
151-
return nil, err
152152
}
153153

154-
done := make(chan error, 1)
154+
cmd.AddDynamicArguments(commit.ID.String()).AddDashesAndList(file)
155155

156+
done := make(chan error, 1)
156157
go func() {
157158
stderr := bytes.Buffer{}
158159
// TODO: it doesn't work for directories (the directories shouldn't be "blamed"), and the "err" should be returned by "Read" but not by "Close"
@@ -170,40 +171,44 @@ func CreateBlameReader(ctx context.Context, objectFormat ObjectFormat, repoPath
170171
}()
171172

172173
bufferedReader := bufio.NewReader(reader)
173-
174174
return &BlameReader{
175175
output: stdout,
176176
reader: reader,
177177
bufferedReader: bufferedReader,
178178
done: done,
179-
ignoreRevsFile: ignoreRevsFile,
179+
ignoreRevsFile: ignoreRevsFileName,
180180
objectFormat: objectFormat,
181+
cleanupFuncs: []func(){ignoreRevsFileCleanup},
181182
}, nil
182183
}
183184

184-
func tryCreateBlameIgnoreRevsFile(commit *Commit) *string {
185+
func tryCreateBlameIgnoreRevsFile(commit *Commit) (string, func()) {
185186
entry, err := commit.GetTreeEntryByPath(".git-blame-ignore-revs")
186187
if err != nil {
187-
return nil
188+
log.Error("Unable to get .git-blame-ignore-revs file: GetTreeEntryByPath: %v", err)
189+
return "", nil
188190
}
189191

190192
r, err := entry.Blob().DataAsync()
191193
if err != nil {
192-
return nil
194+
log.Error("Unable to get .git-blame-ignore-revs file data: DataAsync: %v", err)
195+
return "", nil
193196
}
194197
defer r.Close()
195198

196-
f, err := temp.CreateTemp("gitea_git-blame-ignore-revs")
199+
f, cleanup, err := setting.AppDataTempDir("git-repo-content").CreateTempFileRandom("git-blame-ignore-revs")
197200
if err != nil {
198-
return nil
201+
log.Error("Unable to get .git-blame-ignore-revs file data: CreateTempFileRandom: %v", err)
202+
return "", nil
199203
}
200-
204+
filename := f.Name()
201205
_, err = io.Copy(f, r)
202206
_ = f.Close()
203207
if err != nil {
204-
_ = util.Remove(f.Name())
205-
return nil
208+
cleanup()
209+
log.Error("Unable to get .git-blame-ignore-revs file data: Copy: %v", err)
210+
return "", nil
206211
}
207212

208-
return util.ToPointer(f.Name())
213+
return filename, cleanup
209214
}

modules/git/blame_sha256_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ import (
77
"context"
88
"testing"
99

10+
"code.gitea.io/gitea/modules/setting"
11+
1012
"github.com/stretchr/testify/assert"
1113
)
1214

1315
func TestReadingBlameOutputSha256(t *testing.T) {
16+
setting.AppDataPath = t.TempDir()
1417
ctx, cancel := context.WithCancel(t.Context())
1518
defer cancel()
1619

modules/git/blame_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ import (
77
"context"
88
"testing"
99

10+
"code.gitea.io/gitea/modules/setting"
11+
1012
"github.com/stretchr/testify/assert"
1113
)
1214

1315
func TestReadingBlameOutput(t *testing.T) {
16+
setting.AppDataPath = t.TempDir()
1417
ctx, cancel := context.WithCancel(t.Context())
1518
defer cancel()
1619

0 commit comments

Comments
 (0)