Skip to content

Use filepath.Join instead of path.Join for file system file operations #33978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions models/repo/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"io"
"mime/multipart"
"os"
"path"
"path/filepath"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/log"
Expand Down Expand Up @@ -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.
Expand All @@ -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)
}

Expand Down
31 changes: 2 additions & 29 deletions modules/git/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
}

Expand All @@ -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) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is unused.

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)
}
4 changes: 2 additions & 2 deletions modules/git/repo_commitgraph_gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package git

import (
"os"
"path"
"path/filepath"

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

Expand All @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions modules/repository/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"fmt"
"os"
"path"
"path/filepath"
"strings"

Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions modules/repository/temp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package repository
import (
"fmt"
"os"
"path"
"path/filepath"

"code.gitea.io/gitea/modules/log"
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions modules/setting/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
golog "log"
"os"
"path"
"path/filepath"
"strings"

Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 2 additions & 3 deletions modules/setting/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package setting

import (
"os/exec"
"path"
"path/filepath"
"strings"

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions modules/setting/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package setting

import (
"os"
"path"
"path/filepath"
"strings"
"text/template"
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions services/doctor/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strings"

"code.gitea.io/gitea/models"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -196,15 +196,15 @@ 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)
return false, err
}

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)
Expand Down
2 changes: 1 addition & 1 deletion services/repository/adopt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
5 changes: 2 additions & 3 deletions tests/integration/git_helper_for_declarative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
"testing"
Expand All @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/migration-test/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -47,15 +47,15 @@ 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
}

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()
Expand Down