From 56d700900fc843df8bec0d6821d51a23fd04b1af Mon Sep 17 00:00:00 2001 From: Jason Song Date: Wed, 8 Mar 2023 11:28:48 +0800 Subject: [PATCH 1/8] chore: move out some funcs --- modules/options/base.go | 26 ++++++++++++++++++++++++++ modules/options/dynamic.go | 25 ------------------------- modules/options/static.go | 25 ------------------------- 3 files changed, 26 insertions(+), 50 deletions(-) diff --git a/modules/options/base.go b/modules/options/base.go index 039e934b3a4d9..72604d8229c69 100644 --- a/modules/options/base.go +++ b/modules/options/base.go @@ -7,11 +7,37 @@ import ( "fmt" "io/fs" "os" + "path" "path/filepath" "code.gitea.io/gitea/modules/util" ) +// Locale reads the content of a specific locale from static/bindata or custom path. +func Locale(name string) ([]byte, error) { + return fileFromDir(path.Join("locale", name)) +} + +// Readme reads the content of a specific readme from static/bindata or custom path. +func Readme(name string) ([]byte, error) { + return fileFromDir(path.Join("readme", name)) +} + +// Gitignore reads the content of a gitignore locale from static/bindata or custom path. +func Gitignore(name string) ([]byte, error) { + return fileFromDir(path.Join("gitignore", name)) +} + +// License reads the content of a specific license from static/bindata or custom path. +func License(name string) ([]byte, error) { + return fileFromDir(path.Join("license", name)) +} + +// Labels reads the content of a specific labels from static/bindata or custom path. +func Labels(name string) ([]byte, error) { + return fileFromDir(path.Join("label", name)) +} + func walkAssetDir(root string, callback func(path, name string, d fs.DirEntry, err error) error) error { if err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { // name is the path relative to the root diff --git a/modules/options/dynamic.go b/modules/options/dynamic.go index a20253676e671..0005406eca7b0 100644 --- a/modules/options/dynamic.go +++ b/modules/options/dynamic.go @@ -60,11 +60,6 @@ func Dir(name string) ([]string, error) { return directories.AddAndGet(name, result), nil } -// Locale reads the content of a specific locale from static or custom path. -func Locale(name string) ([]byte, error) { - return fileFromDir(path.Join("locale", name)) -} - // WalkLocales reads the content of a specific locale from static or custom path. func WalkLocales(callback func(path, name string, d fs.DirEntry, err error) error) error { if err := walkAssetDir(filepath.Join(setting.StaticRootPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) { @@ -77,26 +72,6 @@ func WalkLocales(callback func(path, name string, d fs.DirEntry, err error) erro return nil } -// Readme reads the content of a specific readme from static or custom path. -func Readme(name string) ([]byte, error) { - return fileFromDir(path.Join("readme", name)) -} - -// Gitignore reads the content of a specific gitignore from static or custom path. -func Gitignore(name string) ([]byte, error) { - return fileFromDir(path.Join("gitignore", name)) -} - -// License reads the content of a specific license from static or custom path. -func License(name string) ([]byte, error) { - return fileFromDir(path.Join("license", name)) -} - -// Labels reads the content of a specific labels from static or custom path. -func Labels(name string) ([]byte, error) { - return fileFromDir(path.Join("label", name)) -} - // fileFromDir is a helper to read files from static or custom path. func fileFromDir(name string) ([]byte, error) { customPath := path.Join(setting.CustomPath, "options", name) diff --git a/modules/options/static.go b/modules/options/static.go index ff3c86d3f84f6..16fcc65e3bb21 100644 --- a/modules/options/static.go +++ b/modules/options/static.go @@ -69,11 +69,6 @@ func AssetDir(dirName string) ([]string, error) { return results, nil } -// Locale reads the content of a specific locale from bindata or custom path. -func Locale(name string) ([]byte, error) { - return fileFromDir(path.Join("locale", name)) -} - // WalkLocales reads the content of a specific locale from static or custom path. func WalkLocales(callback func(path, name string, d fs.DirEntry, err error) error) error { if err := walkAssetDir(filepath.Join(setting.CustomPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) { @@ -82,26 +77,6 @@ func WalkLocales(callback func(path, name string, d fs.DirEntry, err error) erro return nil } -// Readme reads the content of a specific readme from bindata or custom path. -func Readme(name string) ([]byte, error) { - return fileFromDir(path.Join("readme", name)) -} - -// Gitignore reads the content of a gitignore locale from bindata or custom path. -func Gitignore(name string) ([]byte, error) { - return fileFromDir(path.Join("gitignore", name)) -} - -// License reads the content of a specific license from bindata or custom path. -func License(name string) ([]byte, error) { - return fileFromDir(path.Join("license", name)) -} - -// Labels reads the content of a specific labels from static or custom path. -func Labels(name string) ([]byte, error) { - return fileFromDir(path.Join("label", name)) -} - // fileFromDir is a helper to read files from bindata or custom path. func fileFromDir(name string) ([]byte, error) { customPath := path.Join(setting.CustomPath, "options", name) From 14483ac1341e39b80bc397609dfaf0fb66c8e3d9 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Wed, 8 Mar 2023 11:38:23 +0800 Subject: [PATCH 2/8] fix: WalkLocales --- modules/options/base.go | 10 ++++++++++ modules/options/dynamic.go | 14 ++++---------- modules/options/static.go | 9 +++------ 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/modules/options/base.go b/modules/options/base.go index 72604d8229c69..699a290860236 100644 --- a/modules/options/base.go +++ b/modules/options/base.go @@ -38,6 +38,16 @@ func Labels(name string) ([]byte, error) { return fileFromDir(path.Join("label", name)) } +// WalkLocales reads the content of a specific locale from static or custom path. +func WalkLocales(callback func(path, name string, d fs.DirEntry, err error) error) error { + for _, v := range pathsForWalkLocales() { + if err := walkAssetDir(v, callback); err != nil && !os.IsNotExist(err) { + return fmt.Errorf("failed to walk locales. Error: %w", err) + } + } + return nil +} + func walkAssetDir(root string, callback func(path, name string, d fs.DirEntry, err error) error) error { if err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { // name is the path relative to the root diff --git a/modules/options/dynamic.go b/modules/options/dynamic.go index 0005406eca7b0..457606e38aa07 100644 --- a/modules/options/dynamic.go +++ b/modules/options/dynamic.go @@ -7,7 +7,6 @@ package options import ( "fmt" - "io/fs" "os" "path" "path/filepath" @@ -60,16 +59,11 @@ func Dir(name string) ([]string, error) { return directories.AddAndGet(name, result), nil } -// WalkLocales reads the content of a specific locale from static or custom path. -func WalkLocales(callback func(path, name string, d fs.DirEntry, err error) error) error { - if err := walkAssetDir(filepath.Join(setting.StaticRootPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) { - return fmt.Errorf("failed to walk locales. Error: %w", err) +func pathsForWalkLocales() []string { + return []string{ + filepath.Join(setting.StaticRootPath, "options", "locale"), + filepath.Join(setting.CustomPath, "options", "locale"), } - - if err := walkAssetDir(filepath.Join(setting.CustomPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) { - return fmt.Errorf("failed to walk locales. Error: %w", err) - } - return nil } // fileFromDir is a helper to read files from static or custom path. diff --git a/modules/options/static.go b/modules/options/static.go index 16fcc65e3bb21..8d59e623e6e52 100644 --- a/modules/options/static.go +++ b/modules/options/static.go @@ -8,7 +8,6 @@ package options import ( "fmt" "io" - "io/fs" "os" "path" "path/filepath" @@ -69,12 +68,10 @@ func AssetDir(dirName string) ([]string, error) { return results, nil } -// WalkLocales reads the content of a specific locale from static or custom path. -func WalkLocales(callback func(path, name string, d fs.DirEntry, err error) error) error { - if err := walkAssetDir(filepath.Join(setting.CustomPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) { - return fmt.Errorf("failed to walk locales. Error: %w", err) +func pathsForWalkLocales() []string { + return []string{ + filepath.Join(setting.CustomPath, "options", "locale"), } - return nil } // fileFromDir is a helper to read files from bindata or custom path. From 3050e39eb97d3635fd8c1b458e93d33dd73304e5 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Wed, 8 Mar 2023 11:39:03 +0800 Subject: [PATCH 3/8] Revert "fix: WalkLocales" This reverts commit 14483ac1341e39b80bc397609dfaf0fb66c8e3d9. --- modules/options/base.go | 10 ---------- modules/options/dynamic.go | 14 ++++++++++---- modules/options/static.go | 9 ++++++--- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/modules/options/base.go b/modules/options/base.go index 699a290860236..72604d8229c69 100644 --- a/modules/options/base.go +++ b/modules/options/base.go @@ -38,16 +38,6 @@ func Labels(name string) ([]byte, error) { return fileFromDir(path.Join("label", name)) } -// WalkLocales reads the content of a specific locale from static or custom path. -func WalkLocales(callback func(path, name string, d fs.DirEntry, err error) error) error { - for _, v := range pathsForWalkLocales() { - if err := walkAssetDir(v, callback); err != nil && !os.IsNotExist(err) { - return fmt.Errorf("failed to walk locales. Error: %w", err) - } - } - return nil -} - func walkAssetDir(root string, callback func(path, name string, d fs.DirEntry, err error) error) error { if err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { // name is the path relative to the root diff --git a/modules/options/dynamic.go b/modules/options/dynamic.go index 457606e38aa07..0005406eca7b0 100644 --- a/modules/options/dynamic.go +++ b/modules/options/dynamic.go @@ -7,6 +7,7 @@ package options import ( "fmt" + "io/fs" "os" "path" "path/filepath" @@ -59,11 +60,16 @@ func Dir(name string) ([]string, error) { return directories.AddAndGet(name, result), nil } -func pathsForWalkLocales() []string { - return []string{ - filepath.Join(setting.StaticRootPath, "options", "locale"), - filepath.Join(setting.CustomPath, "options", "locale"), +// WalkLocales reads the content of a specific locale from static or custom path. +func WalkLocales(callback func(path, name string, d fs.DirEntry, err error) error) error { + if err := walkAssetDir(filepath.Join(setting.StaticRootPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) { + return fmt.Errorf("failed to walk locales. Error: %w", err) } + + if err := walkAssetDir(filepath.Join(setting.CustomPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) { + return fmt.Errorf("failed to walk locales. Error: %w", err) + } + return nil } // fileFromDir is a helper to read files from static or custom path. diff --git a/modules/options/static.go b/modules/options/static.go index 8d59e623e6e52..16fcc65e3bb21 100644 --- a/modules/options/static.go +++ b/modules/options/static.go @@ -8,6 +8,7 @@ package options import ( "fmt" "io" + "io/fs" "os" "path" "path/filepath" @@ -68,10 +69,12 @@ func AssetDir(dirName string) ([]string, error) { return results, nil } -func pathsForWalkLocales() []string { - return []string{ - filepath.Join(setting.CustomPath, "options", "locale"), +// WalkLocales reads the content of a specific locale from static or custom path. +func WalkLocales(callback func(path, name string, d fs.DirEntry, err error) error) error { + if err := walkAssetDir(filepath.Join(setting.CustomPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) { + return fmt.Errorf("failed to walk locales. Error: %w", err) } + return nil } // fileFromDir is a helper to read files from bindata or custom path. From 4793ad1a4212a5fa00b8d592054a4efa9529acaf Mon Sep 17 00:00:00 2001 From: Jason Song Date: Wed, 8 Mar 2023 11:40:38 +0800 Subject: [PATCH 4/8] fix: WalkLocales --- modules/options/base.go | 15 +++++++++++++++ modules/options/dynamic.go | 14 -------------- modules/options/static.go | 10 ---------- 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/modules/options/base.go b/modules/options/base.go index 72604d8229c69..bee736de865d7 100644 --- a/modules/options/base.go +++ b/modules/options/base.go @@ -10,6 +10,7 @@ import ( "path" "path/filepath" + "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" ) @@ -38,6 +39,20 @@ func Labels(name string) ([]byte, error) { return fileFromDir(path.Join("label", name)) } +// WalkLocales reads the content of a specific locale +func WalkLocales(callback func(path, name string, d fs.DirEntry, err error) error) error { + if IsDynamic() { + if err := walkAssetDir(filepath.Join(setting.StaticRootPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) { + return fmt.Errorf("failed to walk locales. Error: %w", err) + } + } + + if err := walkAssetDir(filepath.Join(setting.CustomPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) { + return fmt.Errorf("failed to walk locales. Error: %w", err) + } + return nil +} + func walkAssetDir(root string, callback func(path, name string, d fs.DirEntry, err error) error) error { if err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { // name is the path relative to the root diff --git a/modules/options/dynamic.go b/modules/options/dynamic.go index 0005406eca7b0..927571c79b357 100644 --- a/modules/options/dynamic.go +++ b/modules/options/dynamic.go @@ -7,10 +7,8 @@ package options import ( "fmt" - "io/fs" "os" "path" - "path/filepath" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -60,18 +58,6 @@ func Dir(name string) ([]string, error) { return directories.AddAndGet(name, result), nil } -// WalkLocales reads the content of a specific locale from static or custom path. -func WalkLocales(callback func(path, name string, d fs.DirEntry, err error) error) error { - if err := walkAssetDir(filepath.Join(setting.StaticRootPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) { - return fmt.Errorf("failed to walk locales. Error: %w", err) - } - - if err := walkAssetDir(filepath.Join(setting.CustomPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) { - return fmt.Errorf("failed to walk locales. Error: %w", err) - } - return nil -} - // fileFromDir is a helper to read files from static or custom path. func fileFromDir(name string) ([]byte, error) { customPath := path.Join(setting.CustomPath, "options", name) diff --git a/modules/options/static.go b/modules/options/static.go index 16fcc65e3bb21..2a9a19d78667a 100644 --- a/modules/options/static.go +++ b/modules/options/static.go @@ -8,10 +8,8 @@ package options import ( "fmt" "io" - "io/fs" "os" "path" - "path/filepath" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -69,14 +67,6 @@ func AssetDir(dirName string) ([]string, error) { return results, nil } -// WalkLocales reads the content of a specific locale from static or custom path. -func WalkLocales(callback func(path, name string, d fs.DirEntry, err error) error) error { - if err := walkAssetDir(filepath.Join(setting.CustomPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) { - return fmt.Errorf("failed to walk locales. Error: %w", err) - } - return nil -} - // fileFromDir is a helper to read files from bindata or custom path. func fileFromDir(name string) ([]byte, error) { customPath := path.Join(setting.CustomPath, "options", name) From d61236e43831e1b4620f32660f773e2e1e9c5afe Mon Sep 17 00:00:00 2001 From: Jason Song Date: Wed, 8 Mar 2023 11:59:25 +0800 Subject: [PATCH 5/8] fix: Dir --- modules/options/dynamic.go | 35 ++++++++--------------------------- modules/options/static.go | 19 ++++++++----------- 2 files changed, 16 insertions(+), 38 deletions(-) diff --git a/modules/options/dynamic.go b/modules/options/dynamic.go index 927571c79b357..32b5bba7e5553 100644 --- a/modules/options/dynamic.go +++ b/modules/options/dynamic.go @@ -25,34 +25,15 @@ func Dir(name string) ([]string, error) { var result []string - customDir := path.Join(setting.CustomPath, "options", name) - - isDir, err := util.IsDir(customDir) - if err != nil { - return []string{}, fmt.Errorf("Unabe to check if custom directory %s is a directory. %w", customDir, err) - } - if isDir { - files, err := util.StatDir(customDir, true) - if err != nil { - return []string{}, fmt.Errorf("Failed to read custom directory. %w", err) + for _, dir := range []string{ + path.Join(setting.CustomPath, "options", name), // custom dir + path.Join(setting.StaticRootPath, "options", name), // static dir + } { + if files, err := util.StatDir(dir, true); err != nil { + return nil, fmt.Errorf("unable to read directory %q. %w", dir, err) + } else { + result = append(result, files...) } - - result = append(result, files...) - } - - staticDir := path.Join(setting.StaticRootPath, "options", name) - - isDir, err = util.IsDir(staticDir) - if err != nil { - return []string{}, fmt.Errorf("unable to check if static directory %s is a directory. %w", staticDir, err) - } - if isDir { - files, err := util.StatDir(staticDir, true) - if err != nil { - return []string{}, fmt.Errorf("Failed to read static directory. %w", err) - } - - result = append(result, files...) } return directories.AddAndGet(name, result), nil diff --git a/modules/options/static.go b/modules/options/static.go index 2a9a19d78667a..fa190159815da 100644 --- a/modules/options/static.go +++ b/modules/options/static.go @@ -26,18 +26,15 @@ func Dir(name string) ([]string, error) { var result []string - customDir := path.Join(setting.CustomPath, "options", name) - isDir, err := util.IsDir(customDir) - if err != nil { - return []string{}, fmt.Errorf("unable to check if custom directory %q is a directory. %w", customDir, err) - } - if isDir { - files, err := util.StatDir(customDir, true) - if err != nil { - return []string{}, fmt.Errorf("unable to read custom directory %q. %w", customDir, err) + for _, dir := range []string{ + path.Join(setting.CustomPath, "options", name), // custom dir + // no static dir + } { + if files, err := util.StatDir(dir, true); err != nil { + return nil, fmt.Errorf("unable to read directory %q. %w", dir, err) + } else { + result = append(result, files...) } - - result = append(result, files...) } files, err := AssetDir(name) From 93519bc371d2b154ba0d2be415b1b651cf32c0ca Mon Sep 17 00:00:00 2001 From: Jason Song Date: Wed, 8 Mar 2023 12:19:57 +0800 Subject: [PATCH 6/8] chore: lint code --- modules/options/dynamic.go | 6 +++--- modules/options/static.go | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/options/dynamic.go b/modules/options/dynamic.go index 32b5bba7e5553..5a3a2d0bbf24e 100644 --- a/modules/options/dynamic.go +++ b/modules/options/dynamic.go @@ -29,11 +29,11 @@ func Dir(name string) ([]string, error) { path.Join(setting.CustomPath, "options", name), // custom dir path.Join(setting.StaticRootPath, "options", name), // static dir } { - if files, err := util.StatDir(dir, true); err != nil { + files, err := util.StatDir(dir, true) + if err != nil { return nil, fmt.Errorf("unable to read directory %q. %w", dir, err) - } else { - result = append(result, files...) } + result = append(result, files...) } return directories.AddAndGet(name, result), nil diff --git a/modules/options/static.go b/modules/options/static.go index fa190159815da..068481f739b09 100644 --- a/modules/options/static.go +++ b/modules/options/static.go @@ -30,11 +30,11 @@ func Dir(name string) ([]string, error) { path.Join(setting.CustomPath, "options", name), // custom dir // no static dir } { - if files, err := util.StatDir(dir, true); err != nil { + files, err := util.StatDir(dir, true) + if err != nil { return nil, fmt.Errorf("unable to read directory %q. %w", dir, err) - } else { - result = append(result, files...) } + result = append(result, files...) } files, err := AssetDir(name) From 6c12c4912a8a659851826f7702fd34163c1401d1 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Wed, 8 Mar 2023 15:24:32 +0800 Subject: [PATCH 7/8] fix: statDirIfExist --- modules/options/base.go | 15 +++++++++++++++ modules/options/dynamic.go | 4 ++-- modules/options/static.go | 4 ++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/modules/options/base.go b/modules/options/base.go index bee736de865d7..a1e9073d7f862 100644 --- a/modules/options/base.go +++ b/modules/options/base.go @@ -78,3 +78,18 @@ func walkAssetDir(root string, callback func(path, name string, d fs.DirEntry, e } return nil } + +func statDirIfExist(dir string) ([]string, error) { + isDir, err := util.IsDir(dir) + if err != nil { + return nil, fmt.Errorf("unable to check if static directory %s is a directory. %w", dir, err) + } + if !isDir { + return nil, nil + } + files, err := util.StatDir(dir, true) + if err != nil { + return nil, fmt.Errorf("unable to read directory %q. %w", dir, err) + } + return files, nil +} diff --git a/modules/options/dynamic.go b/modules/options/dynamic.go index 5a3a2d0bbf24e..8c954492ae51f 100644 --- a/modules/options/dynamic.go +++ b/modules/options/dynamic.go @@ -29,9 +29,9 @@ func Dir(name string) ([]string, error) { path.Join(setting.CustomPath, "options", name), // custom dir path.Join(setting.StaticRootPath, "options", name), // static dir } { - files, err := util.StatDir(dir, true) + files, err := statDirIfExist(dir) if err != nil { - return nil, fmt.Errorf("unable to read directory %q. %w", dir, err) + return nil, err } result = append(result, files...) } diff --git a/modules/options/static.go b/modules/options/static.go index 068481f739b09..549f4e25b11a3 100644 --- a/modules/options/static.go +++ b/modules/options/static.go @@ -30,9 +30,9 @@ func Dir(name string) ([]string, error) { path.Join(setting.CustomPath, "options", name), // custom dir // no static dir } { - files, err := util.StatDir(dir, true) + files, err := statDirIfExist(dir) if err != nil { - return nil, fmt.Errorf("unable to read directory %q. %w", dir, err) + return nil, err } result = append(result, files...) } From aa61e0c42b532b93cff6ed18973d7eacb29dabca Mon Sep 17 00:00:00 2001 From: Jason Song Date: Wed, 8 Mar 2023 15:32:28 +0800 Subject: [PATCH 8/8] refactor: remvoe GetRepoInitFile --- modules/label/parser.go | 6 +++--- modules/options/repo.go | 44 -------------------------------------- modules/repository/init.go | 6 +++--- 3 files changed, 6 insertions(+), 50 deletions(-) delete mode 100644 modules/options/repo.go diff --git a/modules/label/parser.go b/modules/label/parser.go index 768c72a61b014..55bf570de6b95 100644 --- a/modules/label/parser.go +++ b/modules/label/parser.go @@ -36,17 +36,17 @@ func (err ErrTemplateLoad) Error() string { // GetTemplateFile loads the label template file by given name, // then parses and returns a list of name-color pairs and optionally description. func GetTemplateFile(name string) ([]*Label, error) { - data, err := options.GetRepoInitFile("label", name+".yaml") + data, err := options.Labels(name + ".yaml") if err == nil && len(data) > 0 { return parseYamlFormat(name+".yaml", data) } - data, err = options.GetRepoInitFile("label", name+".yml") + data, err = options.Labels(name + ".yml") if err == nil && len(data) > 0 { return parseYamlFormat(name+".yml", data) } - data, err = options.GetRepoInitFile("label", name) + data, err = options.Labels(name) if err != nil { return nil, ErrTemplateLoad{name, fmt.Errorf("GetRepoInitFile: %w", err)} } diff --git a/modules/options/repo.go b/modules/options/repo.go deleted file mode 100644 index 1480f7808176c..0000000000000 --- a/modules/options/repo.go +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright 2023 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package options - -import ( - "fmt" - "os" - "path" - "strings" - - "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/setting" - "code.gitea.io/gitea/modules/util" -) - -// GetRepoInitFile returns repository init files -func GetRepoInitFile(tp, name string) ([]byte, error) { - cleanedName := strings.TrimLeft(path.Clean("/"+name), "/") - relPath := path.Join("options", tp, cleanedName) - - // Use custom file when available. - customPath := path.Join(setting.CustomPath, relPath) - isFile, err := util.IsFile(customPath) - if err != nil { - log.Error("Unable to check if %s is a file. Error: %v", customPath, err) - } - if isFile { - return os.ReadFile(customPath) - } - - switch tp { - case "readme": - return Readme(cleanedName) - case "gitignore": - return Gitignore(cleanedName) - case "license": - return License(cleanedName) - case "label": - return Labels(cleanedName) - default: - return []byte{}, fmt.Errorf("Invalid init file type") - } -} diff --git a/modules/repository/init.go b/modules/repository/init.go index 49c8d2a904d1a..f9a33cd4f68c9 100644 --- a/modules/repository/init.go +++ b/modules/repository/init.go @@ -136,7 +136,7 @@ func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir, } // README - data, err := options.GetRepoInitFile("readme", opts.Readme) + data, err := options.Readme(opts.Readme) if err != nil { return fmt.Errorf("GetRepoInitFile[%s]: %w", opts.Readme, err) } @@ -164,7 +164,7 @@ func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir, var buf bytes.Buffer names := strings.Split(opts.Gitignores, ",") for _, name := range names { - data, err = options.GetRepoInitFile("gitignore", name) + data, err = options.Gitignore(name) if err != nil { return fmt.Errorf("GetRepoInitFile[%s]: %w", name, err) } @@ -182,7 +182,7 @@ func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir, // LICENSE if len(opts.License) > 0 { - data, err = options.GetRepoInitFile("license", opts.License) + data, err = options.License(opts.License) if err != nil { return fmt.Errorf("GetRepoInitFile[%s]: %w", opts.License, err) }