mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-23 19:47:07 +09:00
refactor: clarify GOOS detection (#39620)
Introduce `consts.IsWindows`, now it's clearer to see how Windows build works
This commit is contained in:
1 parent
052f660ba5
commit
1065f03454
16 files changed
+44
-37
No files matched your search
@@ -8,6 +8,8 @@ import (
|
||||
"os"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"gitea.dev/modules/consts"
|
||||
)
|
||||
|
||||
// On Windows, when a file or directory is in use (opened), the file or directory is not able to be removed or renamed.
|
||||
@@ -32,7 +34,7 @@ func retryWhenFileBusyInternal(count int, delay time.Duration, f func() error) (
|
||||
break
|
||||
}
|
||||
isErrBusy := errors.Is(err, syscall.EBUSY)
|
||||
isErrBusy = isErrBusy || (isOSWindows && errors.Is(err, errWindowsSharingViolationError))
|
||||
isErrBusy = isErrBusy || (consts.IsWindows && errors.Is(err, errWindowsSharingViolationError))
|
||||
if !isErrBusy {
|
||||
break
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"gitea.dev/modules/consts"
|
||||
)
|
||||
|
||||
var PathNameValidator = sync.OnceValue(func() (ret struct {
|
||||
@@ -93,7 +95,7 @@ const filepathSeparator = string(os.PathSeparator)
|
||||
func FilePathJoinAbs(base string, sub ...string) string {
|
||||
// POSIX filesystem can have `\` in file names. Windows: `\` and `/` are both used for path separators
|
||||
// to keep the behavior consistent, we do not allow `\` in file names, replace all `\` with `/`
|
||||
if !isOSWindows {
|
||||
if !consts.IsWindows {
|
||||
base = strings.ReplaceAll(base, "\\", filepathSeparator)
|
||||
}
|
||||
if !filepath.IsAbs(base) {
|
||||
@@ -110,7 +112,7 @@ func FilePathJoinAbs(base string, sub ...string) string {
|
||||
if s == "" {
|
||||
continue
|
||||
}
|
||||
if isOSWindows {
|
||||
if consts.IsWindows {
|
||||
elems = append(elems, filepath.Clean(filepathSeparator+s))
|
||||
} else {
|
||||
elems = append(elems, filepath.Clean(filepathSeparator+strings.ReplaceAll(s, "\\", filepathSeparator)))
|
||||
@@ -284,7 +286,7 @@ func fileURLToPathInternal(u *url.URL, isWindows bool) (string, error) {
|
||||
// FileURLToPath extracts the path information from a file://... url.
|
||||
// It returns an error only if the URL is not a file URL.
|
||||
func FileURLToPath(u *url.URL) (string, error) {
|
||||
return fileURLToPathInternal(u, isOSWindows)
|
||||
return fileURLToPathInternal(u, consts.IsWindows)
|
||||
}
|
||||
|
||||
// HomeDir returns path of '~'(in Linux) on Windows,
|
||||
@@ -293,7 +295,7 @@ func HomeDir() (home string, err error) {
|
||||
// TODO: some users run Gitea with mismatched uid and "HOME=xxx" (they set HOME=xxx by environment manually)
|
||||
// TODO: when running gitea as a sub command inside git, the HOME directory is not the user's home directory
|
||||
// so at the moment we can not use `user.Current().HomeDir`
|
||||
if isOSWindows {
|
||||
if consts.IsWindows {
|
||||
home = os.Getenv("USERPROFILE")
|
||||
if home == "" {
|
||||
home = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
|
||||
|
||||
@@ -9,6 +9,8 @@ import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"gitea.dev/modules/consts"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -182,7 +184,7 @@ func TestCleanPath(t *testing.T) {
|
||||
}
|
||||
|
||||
// for POSIX only, but the result is similar on Windows, because the first element must be an absolute path
|
||||
if isOSWindows {
|
||||
if consts.IsWindows {
|
||||
cases = []struct {
|
||||
elems []string
|
||||
expected string
|
||||
|
||||
@@ -8,8 +8,6 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const isOSWindows = runtime.GOOS == "windows"
|
||||
|
||||
func CallerFuncName(optSkipParent ...int) string {
|
||||
pc := make([]uintptr, 1)
|
||||
skipParent := 0
|
||||
|
||||
Reference in new issue
Block a user