Skip to content

Commit 3fe449c

Browse files
authored
Use filepath.Join instead of path.Join for file system file operations (#33978)
1 parent 82bc8b8 commit 3fe449c

File tree

12 files changed

+23
-56
lines changed

12 files changed

+23
-56
lines changed

models/repo/upload.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"io"
1111
"mime/multipart"
1212
"os"
13-
"path"
13+
"path/filepath"
1414

1515
"code.gitea.io/gitea/models/db"
1616
"code.gitea.io/gitea/modules/log"
@@ -53,7 +53,7 @@ func init() {
5353

5454
// UploadLocalPath returns where uploads is stored in local file system based on given UUID.
5555
func UploadLocalPath(uuid string) string {
56-
return path.Join(setting.Repository.Upload.TempPath, uuid[0:1], uuid[1:2], uuid)
56+
return filepath.Join(setting.Repository.Upload.TempPath, uuid[0:1], uuid[1:2], uuid)
5757
}
5858

5959
// LocalPath returns where uploads are temporarily stored in local file system.
@@ -69,7 +69,7 @@ func NewUpload(ctx context.Context, name string, buf []byte, file multipart.File
6969
}
7070

7171
localPath := upload.LocalPath()
72-
if err = os.MkdirAll(path.Dir(localPath), os.ModePerm); err != nil {
72+
if err = os.MkdirAll(filepath.Dir(localPath), os.ModePerm); err != nil {
7373
return nil, fmt.Errorf("MkdirAll: %w", err)
7474
}
7575

modules/git/hook.go

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ package git
77
import (
88
"errors"
99
"os"
10-
"path"
1110
"path/filepath"
1211
"strings"
1312

14-
"code.gitea.io/gitea/modules/log"
1513
"code.gitea.io/gitea/modules/util"
1614
)
1715

@@ -51,7 +49,7 @@ func GetHook(repoPath, name string) (*Hook, error) {
5149
}
5250
h := &Hook{
5351
name: name,
54-
path: path.Join(repoPath, "hooks", name+".d", name),
52+
path: filepath.Join(repoPath, "hooks", name+".d", name),
5553
}
5654
samplePath := filepath.Join(repoPath, "hooks", name+".sample")
5755
if isFile(h.path) {
@@ -103,7 +101,7 @@ func (h *Hook) Update() error {
103101

104102
// ListHooks returns a list of Git hooks of given repository.
105103
func ListHooks(repoPath string) (_ []*Hook, err error) {
106-
if !isDir(path.Join(repoPath, "hooks")) {
104+
if !isDir(filepath.Join(repoPath, "hooks")) {
107105
return nil, errors.New("hooks path does not exist")
108106
}
109107

@@ -116,28 +114,3 @@ func ListHooks(repoPath string) (_ []*Hook, err error) {
116114
}
117115
return hooks, nil
118116
}
119-
120-
const (
121-
// HookPathUpdate hook update path
122-
HookPathUpdate = "hooks/update"
123-
)
124-
125-
// SetUpdateHook writes given content to update hook of the repository.
126-
func SetUpdateHook(repoPath, content string) (err error) {
127-
log.Debug("Setting update hook: %s", repoPath)
128-
hookPath := path.Join(repoPath, HookPathUpdate)
129-
isExist, err := util.IsExist(hookPath)
130-
if err != nil {
131-
log.Debug("Unable to check if %s exists. Error: %v", hookPath, err)
132-
return err
133-
}
134-
if isExist {
135-
err = util.Remove(hookPath)
136-
} else {
137-
err = os.MkdirAll(path.Dir(hookPath), os.ModePerm)
138-
}
139-
if err != nil {
140-
return err
141-
}
142-
return os.WriteFile(hookPath, []byte(content), 0o777)
143-
}

modules/git/repo_commitgraph_gogit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package git
88

99
import (
1010
"os"
11-
"path"
11+
"path/filepath"
1212

1313
gitealog "code.gitea.io/gitea/modules/log"
1414

@@ -18,7 +18,7 @@ import (
1818

1919
// CommitNodeIndex returns the index for walking commit graph
2020
func (r *Repository) CommitNodeIndex() (cgobject.CommitNodeIndex, *os.File) {
21-
indexPath := path.Join(r.Path, "objects", "info", "commit-graph")
21+
indexPath := filepath.Join(r.Path, "objects", "info", "commit-graph")
2222

2323
file, err := os.Open(indexPath)
2424
if err == nil {

modules/repository/create.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"context"
88
"fmt"
99
"os"
10-
"path"
1110
"path/filepath"
1211
"strings"
1312

@@ -72,7 +71,7 @@ func CheckDaemonExportOK(ctx context.Context, repo *repo_model.Repository) error
7271
}
7372

7473
// Create/Remove git-daemon-export-ok for git-daemon...
75-
daemonExportFile := path.Join(repo.RepoPath(), `git-daemon-export-ok`)
74+
daemonExportFile := filepath.Join(repo.RepoPath(), `git-daemon-export-ok`)
7675

7776
isExist, err := util.IsExist(daemonExportFile)
7877
if err != nil {

modules/repository/temp.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package repository
66
import (
77
"fmt"
88
"os"
9-
"path"
109
"path/filepath"
1110

1211
"code.gitea.io/gitea/modules/log"
@@ -19,7 +18,7 @@ func LocalCopyPath() string {
1918
if filepath.IsAbs(setting.Repository.Local.LocalCopyPath) {
2019
return setting.Repository.Local.LocalCopyPath
2120
}
22-
return path.Join(setting.AppDataPath, setting.Repository.Local.LocalCopyPath)
21+
return filepath.Join(setting.AppDataPath, setting.Repository.Local.LocalCopyPath)
2322
}
2423

2524
// CreateTemporaryPath creates a temporary path

modules/setting/log.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
golog "log"
99
"os"
10-
"path"
1110
"path/filepath"
1211
"strings"
1312

@@ -41,7 +40,7 @@ func loadLogGlobalFrom(rootCfg ConfigProvider) {
4140
Log.BufferLen = sec.Key("BUFFER_LEN").MustInt(10000)
4241
Log.Mode = sec.Key("MODE").MustString("console")
4342

44-
Log.RootPath = sec.Key("ROOT_PATH").MustString(path.Join(AppWorkPath, "log"))
43+
Log.RootPath = sec.Key("ROOT_PATH").MustString(filepath.Join(AppWorkPath, "log"))
4544
if !filepath.IsAbs(Log.RootPath) {
4645
Log.RootPath = filepath.Join(AppWorkPath, Log.RootPath)
4746
}

modules/setting/repository.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package setting
55

66
import (
77
"os/exec"
8-
"path"
98
"path/filepath"
109
"strings"
1110

@@ -284,7 +283,7 @@ func loadRepositoryFrom(rootCfg ConfigProvider) {
284283
Repository.GoGetCloneURLProtocol = sec.Key("GO_GET_CLONE_URL_PROTOCOL").MustString("https")
285284
Repository.MaxCreationLimit = sec.Key("MAX_CREATION_LIMIT").MustInt(-1)
286285
Repository.DefaultBranch = sec.Key("DEFAULT_BRANCH").MustString(Repository.DefaultBranch)
287-
RepoRootPath = sec.Key("ROOT").MustString(path.Join(AppDataPath, "gitea-repositories"))
286+
RepoRootPath = sec.Key("ROOT").MustString(filepath.Join(AppDataPath, "gitea-repositories"))
288287
if !filepath.IsAbs(RepoRootPath) {
289288
RepoRootPath = filepath.Join(AppWorkPath, RepoRootPath)
290289
} else {
@@ -363,7 +362,7 @@ func loadRepositoryFrom(rootCfg ConfigProvider) {
363362
}
364363

365364
if !filepath.IsAbs(Repository.Upload.TempPath) {
366-
Repository.Upload.TempPath = path.Join(AppWorkPath, Repository.Upload.TempPath)
365+
Repository.Upload.TempPath = filepath.Join(AppWorkPath, Repository.Upload.TempPath)
367366
}
368367

369368
if err := loadRepoArchiveFrom(rootCfg); err != nil {

modules/setting/ssh.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package setting
55

66
import (
77
"os"
8-
"path"
98
"path/filepath"
109
"strings"
1110
"text/template"
@@ -111,7 +110,7 @@ func loadSSHFrom(rootCfg ConfigProvider) {
111110
}
112111
homeDir = strings.ReplaceAll(homeDir, "\\", "/")
113112

114-
SSH.RootPath = path.Join(homeDir, ".ssh")
113+
SSH.RootPath = filepath.Join(homeDir, ".ssh")
115114
serverCiphers := sec.Key("SSH_SERVER_CIPHERS").Strings(",")
116115
if len(serverCiphers) > 0 {
117116
SSH.ServerCiphers = serverCiphers

services/doctor/misc.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"fmt"
99
"os"
1010
"os/exec"
11-
"path"
11+
"path/filepath"
1212
"strings"
1313

1414
"code.gitea.io/gitea/models"
@@ -148,7 +148,7 @@ func checkDaemonExport(ctx context.Context, logger log.Logger, autofix bool) err
148148
}
149149

150150
// Create/Remove git-daemon-export-ok for git-daemon...
151-
daemonExportFile := path.Join(repo.RepoPath(), `git-daemon-export-ok`)
151+
daemonExportFile := filepath.Join(repo.RepoPath(), `git-daemon-export-ok`)
152152
isExist, err := util.IsExist(daemonExportFile)
153153
if err != nil {
154154
log.Error("Unable to check if %s exists. Error: %v", daemonExportFile, err)
@@ -196,15 +196,15 @@ func checkCommitGraph(ctx context.Context, logger log.Logger, autofix bool) erro
196196

197197
commitGraphExists := func() (bool, error) {
198198
// Check commit-graph exists
199-
commitGraphFile := path.Join(repo.RepoPath(), `objects/info/commit-graph`)
199+
commitGraphFile := filepath.Join(repo.RepoPath(), `objects/info/commit-graph`)
200200
isExist, err := util.IsExist(commitGraphFile)
201201
if err != nil {
202202
logger.Error("Unable to check if %s exists. Error: %v", commitGraphFile, err)
203203
return false, err
204204
}
205205

206206
if !isExist {
207-
commitGraphsDir := path.Join(repo.RepoPath(), `objects/info/commit-graphs`)
207+
commitGraphsDir := filepath.Join(repo.RepoPath(), `objects/info/commit-graphs`)
208208
isExist, err = util.IsExist(commitGraphsDir)
209209
if err != nil {
210210
logger.Error("Unable to check if %s exists. Error: %v", commitGraphsDir, err)

services/repository/adopt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestListUnadoptedRepositories_ListOptions(t *testing.T) {
7171
username := "user2"
7272
unadoptedList := []string{path.Join(username, "unadopted1"), path.Join(username, "unadopted2")}
7373
for _, unadopted := range unadoptedList {
74-
_ = os.Mkdir(path.Join(setting.RepoRootPath, unadopted+".git"), 0o755)
74+
_ = os.Mkdir(filepath.Join(setting.RepoRootPath, unadopted+".git"), 0o755)
7575
}
7676

7777
opts := db.ListOptions{Page: 1, PageSize: 1}

tests/integration/git_helper_for_declarative_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"net/http"
1111
"net/url"
1212
"os"
13-
"path"
1413
"path/filepath"
1514
"strconv"
1615
"testing"
@@ -35,12 +34,12 @@ func withKeyFile(t *testing.T, keyname string, callback func(string)) {
3534
err = ssh.GenKeyPair(keyFile)
3635
assert.NoError(t, err)
3736

38-
err = os.WriteFile(path.Join(tmpDir, "ssh"), []byte("#!/bin/bash\n"+
37+
err = os.WriteFile(filepath.Join(tmpDir, "ssh"), []byte("#!/bin/bash\n"+
3938
"ssh -o \"UserKnownHostsFile=/dev/null\" -o \"StrictHostKeyChecking=no\" -o \"IdentitiesOnly=yes\" -i \""+keyFile+"\" \"$@\""), 0o700)
4039
assert.NoError(t, err)
4140

4241
// Setup ssh wrapper
43-
t.Setenv("GIT_SSH", path.Join(tmpDir, "ssh"))
42+
t.Setenv("GIT_SSH", filepath.Join(tmpDir, "ssh"))
4443
t.Setenv("GIT_SSH_COMMAND",
4544
"ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentitiesOnly=yes -i \""+keyFile+"\"")
4645
t.Setenv("GIT_SSH_VARIANT", "ssh")

tests/integration/migration-test/migration_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var currentEngine *xorm.Engine
3838
func initMigrationTest(t *testing.T) func() {
3939
testlogger.Init()
4040
giteaRoot := test.SetupGiteaRoot()
41-
setting.AppPath = path.Join(giteaRoot, "gitea")
41+
setting.AppPath = filepath.Join(giteaRoot, "gitea")
4242
if _, err := os.Stat(setting.AppPath); err != nil {
4343
testlogger.Fatalf(fmt.Sprintf("Could not find gitea binary at %s\n", setting.AppPath))
4444
}
@@ -47,15 +47,15 @@ func initMigrationTest(t *testing.T) func() {
4747
if giteaConf == "" {
4848
testlogger.Fatalf("Environment variable $GITEA_CONF not set\n")
4949
} else if !path.IsAbs(giteaConf) {
50-
setting.CustomConf = path.Join(giteaRoot, giteaConf)
50+
setting.CustomConf = filepath.Join(giteaRoot, giteaConf)
5151
} else {
5252
setting.CustomConf = giteaConf
5353
}
5454

5555
unittest.InitSettings()
5656

5757
assert.NotEmpty(t, setting.RepoRootPath)
58-
assert.NoError(t, unittest.SyncDirs(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
58+
assert.NoError(t, unittest.SyncDirs(filepath.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
5959
assert.NoError(t, git.InitFull(t.Context()))
6060
setting.LoadDBSetting()
6161
setting.InitLoggersForTest()

0 commit comments

Comments
 (0)