From 3fe449c21a53371987db06effe7c19dd5a17cd24 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 24 Mar 2025 14:50:28 -0700 Subject: [PATCH] Use filepath.Join instead of path.Join for file system file operations (#33978) --- models/repo/upload.go | 6 ++-- modules/git/hook.go | 31 ++----------------- modules/git/repo_commitgraph_gogit.go | 4 +-- modules/repository/create.go | 3 +- modules/repository/temp.go | 3 +- modules/setting/log.go | 3 +- modules/setting/repository.go | 5 ++- modules/setting/ssh.go | 3 +- services/doctor/misc.go | 8 ++--- services/repository/adopt_test.go | 2 +- .../git_helper_for_declarative_test.go | 5 ++- .../migration-test/migration_test.go | 6 ++-- 12 files changed, 23 insertions(+), 56 deletions(-) diff --git a/models/repo/upload.go b/models/repo/upload.go index 18834f6b83..cae11df96a 100644 --- a/models/repo/upload.go +++ b/models/repo/upload.go @@ -10,7 +10,7 @@ import ( "io" "mime/multipart" "os" - "path" + "path/filepath" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/log" @@ -53,7 +53,7 @@ func init() { // UploadLocalPath returns where uploads is stored in local file system based on given UUID. func UploadLocalPath(uuid string) string { - return path.Join(setting.Repository.Upload.TempPath, uuid[0:1], uuid[1:2], uuid) + return filepath.Join(setting.Repository.Upload.TempPath, uuid[0:1], uuid[1:2], uuid) } // 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 } localPath := upload.LocalPath() - if err = os.MkdirAll(path.Dir(localPath), os.ModePerm); err != nil { + if err = os.MkdirAll(filepath.Dir(localPath), os.ModePerm); err != nil { return nil, fmt.Errorf("MkdirAll: %w", err) } diff --git a/modules/git/hook.go b/modules/git/hook.go index 46f93ce13e..8d5760d890 100644 --- a/modules/git/hook.go +++ b/modules/git/hook.go @@ -7,11 +7,9 @@ package git import ( "errors" "os" - "path" "path/filepath" "strings" - "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/util" ) @@ -51,7 +49,7 @@ func GetHook(repoPath, name string) (*Hook, error) { } h := &Hook{ name: name, - path: path.Join(repoPath, "hooks", name+".d", name), + path: filepath.Join(repoPath, "hooks", name+".d", name), } samplePath := filepath.Join(repoPath, "hooks", name+".sample") if isFile(h.path) { @@ -103,7 +101,7 @@ func (h *Hook) Update() error { // ListHooks returns a list of Git hooks of given repository. func ListHooks(repoPath string) (_ []*Hook, err error) { - if !isDir(path.Join(repoPath, "hooks")) { + if !isDir(filepath.Join(repoPath, "hooks")) { return nil, errors.New("hooks path does not exist") } @@ -116,28 +114,3 @@ func ListHooks(repoPath string) (_ []*Hook, err error) { } return hooks, nil } - -const ( - // HookPathUpdate hook update path - HookPathUpdate = "hooks/update" -) - -// SetUpdateHook writes given content to update hook of the repository. -func SetUpdateHook(repoPath, content string) (err error) { - log.Debug("Setting update hook: %s", repoPath) - hookPath := path.Join(repoPath, HookPathUpdate) - isExist, err := util.IsExist(hookPath) - if err != nil { - log.Debug("Unable to check if %s exists. Error: %v", hookPath, err) - return err - } - if isExist { - err = util.Remove(hookPath) - } else { - err = os.MkdirAll(path.Dir(hookPath), os.ModePerm) - } - if err != nil { - return err - } - return os.WriteFile(hookPath, []byte(content), 0o777) -} diff --git a/modules/git/repo_commitgraph_gogit.go b/modules/git/repo_commitgraph_gogit.go index d3182f15c6..c0082b62c8 100644 --- a/modules/git/repo_commitgraph_gogit.go +++ b/modules/git/repo_commitgraph_gogit.go @@ -8,7 +8,7 @@ package git import ( "os" - "path" + "path/filepath" gitealog "code.gitea.io/gitea/modules/log" @@ -18,7 +18,7 @@ import ( // CommitNodeIndex returns the index for walking commit graph func (r *Repository) CommitNodeIndex() (cgobject.CommitNodeIndex, *os.File) { - indexPath := path.Join(r.Path, "objects", "info", "commit-graph") + indexPath := filepath.Join(r.Path, "objects", "info", "commit-graph") file, err := os.Open(indexPath) if err == nil { diff --git a/modules/repository/create.go b/modules/repository/create.go index b4f7033bd7..a53632bb57 100644 --- a/modules/repository/create.go +++ b/modules/repository/create.go @@ -7,7 +7,6 @@ import ( "context" "fmt" "os" - "path" "path/filepath" "strings" @@ -72,7 +71,7 @@ func CheckDaemonExportOK(ctx context.Context, repo *repo_model.Repository) error } // Create/Remove git-daemon-export-ok for git-daemon... - daemonExportFile := path.Join(repo.RepoPath(), `git-daemon-export-ok`) + daemonExportFile := filepath.Join(repo.RepoPath(), `git-daemon-export-ok`) isExist, err := util.IsExist(daemonExportFile) if err != nil { diff --git a/modules/repository/temp.go b/modules/repository/temp.go index 04faa9db3d..76b9bda4ad 100644 --- a/modules/repository/temp.go +++ b/modules/repository/temp.go @@ -6,7 +6,6 @@ package repository import ( "fmt" "os" - "path" "path/filepath" "code.gitea.io/gitea/modules/log" @@ -19,7 +18,7 @@ func LocalCopyPath() string { if filepath.IsAbs(setting.Repository.Local.LocalCopyPath) { return setting.Repository.Local.LocalCopyPath } - return path.Join(setting.AppDataPath, setting.Repository.Local.LocalCopyPath) + return filepath.Join(setting.AppDataPath, setting.Repository.Local.LocalCopyPath) } // CreateTemporaryPath creates a temporary path diff --git a/modules/setting/log.go b/modules/setting/log.go index 50c5779994..614d9ee75a 100644 --- a/modules/setting/log.go +++ b/modules/setting/log.go @@ -7,7 +7,6 @@ import ( "fmt" golog "log" "os" - "path" "path/filepath" "strings" @@ -41,7 +40,7 @@ func loadLogGlobalFrom(rootCfg ConfigProvider) { Log.BufferLen = sec.Key("BUFFER_LEN").MustInt(10000) Log.Mode = sec.Key("MODE").MustString("console") - Log.RootPath = sec.Key("ROOT_PATH").MustString(path.Join(AppWorkPath, "log")) + Log.RootPath = sec.Key("ROOT_PATH").MustString(filepath.Join(AppWorkPath, "log")) if !filepath.IsAbs(Log.RootPath) { Log.RootPath = filepath.Join(AppWorkPath, Log.RootPath) } diff --git a/modules/setting/repository.go b/modules/setting/repository.go index c5619d0f04..43bfb3256d 100644 --- a/modules/setting/repository.go +++ b/modules/setting/repository.go @@ -5,7 +5,6 @@ package setting import ( "os/exec" - "path" "path/filepath" "strings" @@ -284,7 +283,7 @@ func loadRepositoryFrom(rootCfg ConfigProvider) { Repository.GoGetCloneURLProtocol = sec.Key("GO_GET_CLONE_URL_PROTOCOL").MustString("https") Repository.MaxCreationLimit = sec.Key("MAX_CREATION_LIMIT").MustInt(-1) Repository.DefaultBranch = sec.Key("DEFAULT_BRANCH").MustString(Repository.DefaultBranch) - RepoRootPath = sec.Key("ROOT").MustString(path.Join(AppDataPath, "gitea-repositories")) + RepoRootPath = sec.Key("ROOT").MustString(filepath.Join(AppDataPath, "gitea-repositories")) if !filepath.IsAbs(RepoRootPath) { RepoRootPath = filepath.Join(AppWorkPath, RepoRootPath) } else { @@ -363,7 +362,7 @@ func loadRepositoryFrom(rootCfg ConfigProvider) { } if !filepath.IsAbs(Repository.Upload.TempPath) { - Repository.Upload.TempPath = path.Join(AppWorkPath, Repository.Upload.TempPath) + Repository.Upload.TempPath = filepath.Join(AppWorkPath, Repository.Upload.TempPath) } if err := loadRepoArchiveFrom(rootCfg); err != nil { diff --git a/modules/setting/ssh.go b/modules/setting/ssh.go index ea387e521f..46eb49bfd4 100644 --- a/modules/setting/ssh.go +++ b/modules/setting/ssh.go @@ -5,7 +5,6 @@ package setting import ( "os" - "path" "path/filepath" "strings" "text/template" @@ -111,7 +110,7 @@ func loadSSHFrom(rootCfg ConfigProvider) { } homeDir = strings.ReplaceAll(homeDir, "\\", "/") - SSH.RootPath = path.Join(homeDir, ".ssh") + SSH.RootPath = filepath.Join(homeDir, ".ssh") serverCiphers := sec.Key("SSH_SERVER_CIPHERS").Strings(",") if len(serverCiphers) > 0 { SSH.ServerCiphers = serverCiphers diff --git a/services/doctor/misc.go b/services/doctor/misc.go index d934640af5..1269d088c3 100644 --- a/services/doctor/misc.go +++ b/services/doctor/misc.go @@ -8,7 +8,7 @@ import ( "fmt" "os" "os/exec" - "path" + "path/filepath" "strings" "code.gitea.io/gitea/models" @@ -148,7 +148,7 @@ func checkDaemonExport(ctx context.Context, logger log.Logger, autofix bool) err } // Create/Remove git-daemon-export-ok for git-daemon... - daemonExportFile := path.Join(repo.RepoPath(), `git-daemon-export-ok`) + daemonExportFile := filepath.Join(repo.RepoPath(), `git-daemon-export-ok`) isExist, err := util.IsExist(daemonExportFile) if err != nil { log.Error("Unable to check if %s exists. Error: %v", daemonExportFile, err) @@ -196,7 +196,7 @@ func checkCommitGraph(ctx context.Context, logger log.Logger, autofix bool) erro commitGraphExists := func() (bool, error) { // Check commit-graph exists - commitGraphFile := path.Join(repo.RepoPath(), `objects/info/commit-graph`) + commitGraphFile := filepath.Join(repo.RepoPath(), `objects/info/commit-graph`) isExist, err := util.IsExist(commitGraphFile) if err != nil { logger.Error("Unable to check if %s exists. Error: %v", commitGraphFile, err) @@ -204,7 +204,7 @@ func checkCommitGraph(ctx context.Context, logger log.Logger, autofix bool) erro } if !isExist { - commitGraphsDir := path.Join(repo.RepoPath(), `objects/info/commit-graphs`) + commitGraphsDir := filepath.Join(repo.RepoPath(), `objects/info/commit-graphs`) isExist, err = util.IsExist(commitGraphsDir) if err != nil { logger.Error("Unable to check if %s exists. Error: %v", commitGraphsDir, err) diff --git a/services/repository/adopt_test.go b/services/repository/adopt_test.go index 123cedc1f2..294185ea1f 100644 --- a/services/repository/adopt_test.go +++ b/services/repository/adopt_test.go @@ -71,7 +71,7 @@ func TestListUnadoptedRepositories_ListOptions(t *testing.T) { username := "user2" unadoptedList := []string{path.Join(username, "unadopted1"), path.Join(username, "unadopted2")} for _, unadopted := range unadoptedList { - _ = os.Mkdir(path.Join(setting.RepoRootPath, unadopted+".git"), 0o755) + _ = os.Mkdir(filepath.Join(setting.RepoRootPath, unadopted+".git"), 0o755) } opts := db.ListOptions{Page: 1, PageSize: 1} diff --git a/tests/integration/git_helper_for_declarative_test.go b/tests/integration/git_helper_for_declarative_test.go index 435253c9c7..fce9b8f247 100644 --- a/tests/integration/git_helper_for_declarative_test.go +++ b/tests/integration/git_helper_for_declarative_test.go @@ -10,7 +10,6 @@ import ( "net/http" "net/url" "os" - "path" "path/filepath" "strconv" "testing" @@ -35,12 +34,12 @@ func withKeyFile(t *testing.T, keyname string, callback func(string)) { err = ssh.GenKeyPair(keyFile) assert.NoError(t, err) - err = os.WriteFile(path.Join(tmpDir, "ssh"), []byte("#!/bin/bash\n"+ + err = os.WriteFile(filepath.Join(tmpDir, "ssh"), []byte("#!/bin/bash\n"+ "ssh -o \"UserKnownHostsFile=/dev/null\" -o \"StrictHostKeyChecking=no\" -o \"IdentitiesOnly=yes\" -i \""+keyFile+"\" \"$@\""), 0o700) assert.NoError(t, err) // Setup ssh wrapper - t.Setenv("GIT_SSH", path.Join(tmpDir, "ssh")) + t.Setenv("GIT_SSH", filepath.Join(tmpDir, "ssh")) t.Setenv("GIT_SSH_COMMAND", "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentitiesOnly=yes -i \""+keyFile+"\"") t.Setenv("GIT_SSH_VARIANT", "ssh") diff --git a/tests/integration/migration-test/migration_test.go b/tests/integration/migration-test/migration_test.go index 1b42f00604..c319fd20a7 100644 --- a/tests/integration/migration-test/migration_test.go +++ b/tests/integration/migration-test/migration_test.go @@ -38,7 +38,7 @@ var currentEngine *xorm.Engine func initMigrationTest(t *testing.T) func() { testlogger.Init() giteaRoot := test.SetupGiteaRoot() - setting.AppPath = path.Join(giteaRoot, "gitea") + setting.AppPath = filepath.Join(giteaRoot, "gitea") if _, err := os.Stat(setting.AppPath); err != nil { testlogger.Fatalf(fmt.Sprintf("Could not find gitea binary at %s\n", setting.AppPath)) } @@ -47,7 +47,7 @@ func initMigrationTest(t *testing.T) func() { if giteaConf == "" { testlogger.Fatalf("Environment variable $GITEA_CONF not set\n") } else if !path.IsAbs(giteaConf) { - setting.CustomConf = path.Join(giteaRoot, giteaConf) + setting.CustomConf = filepath.Join(giteaRoot, giteaConf) } else { setting.CustomConf = giteaConf } @@ -55,7 +55,7 @@ func initMigrationTest(t *testing.T) func() { unittest.InitSettings() assert.NotEmpty(t, setting.RepoRootPath) - assert.NoError(t, unittest.SyncDirs(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath)) + assert.NoError(t, unittest.SyncDirs(filepath.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath)) assert.NoError(t, git.InitFull(t.Context())) setting.LoadDBSetting() setting.InitLoggersForTest()