Skip to content

Commit 090e753

Browse files
authored
Reduce duplicate and useless code in options (#23369)
Avoid maintaining two copies of code, some functions can be used with both `bindata` and `no bindata`. And removed `GetRepoInitFile`, it's useless now. `Readme`/`Gitignore`/`License`/`Labels` will clean the name and use custom files when available.
1 parent a12f575 commit 090e753

File tree

6 files changed

+74
-158
lines changed

6 files changed

+74
-158
lines changed

modules/label/parser.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ func (err ErrTemplateLoad) Error() string {
3636
// GetTemplateFile loads the label template file by given name,
3737
// then parses and returns a list of name-color pairs and optionally description.
3838
func GetTemplateFile(name string) ([]*Label, error) {
39-
data, err := options.GetRepoInitFile("label", name+".yaml")
39+
data, err := options.Labels(name + ".yaml")
4040
if err == nil && len(data) > 0 {
4141
return parseYamlFormat(name+".yaml", data)
4242
}
4343

44-
data, err = options.GetRepoInitFile("label", name+".yml")
44+
data, err = options.Labels(name + ".yml")
4545
if err == nil && len(data) > 0 {
4646
return parseYamlFormat(name+".yml", data)
4747
}
4848

49-
data, err = options.GetRepoInitFile("label", name)
49+
data, err = options.Labels(name)
5050
if err != nil {
5151
return nil, ErrTemplateLoad{name, fmt.Errorf("GetRepoInitFile: %w", err)}
5252
}

modules/options/base.go

+56
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,52 @@ import (
77
"fmt"
88
"io/fs"
99
"os"
10+
"path"
1011
"path/filepath"
1112

13+
"code.gitea.io/gitea/modules/setting"
1214
"code.gitea.io/gitea/modules/util"
1315
)
1416

17+
// Locale reads the content of a specific locale from static/bindata or custom path.
18+
func Locale(name string) ([]byte, error) {
19+
return fileFromDir(path.Join("locale", path.Clean("/"+name)))
20+
}
21+
22+
// Readme reads the content of a specific readme from static/bindata or custom path.
23+
func Readme(name string) ([]byte, error) {
24+
return fileFromDir(path.Join("readme", path.Clean("/"+name)))
25+
}
26+
27+
// Gitignore reads the content of a gitignore locale from static/bindata or custom path.
28+
func Gitignore(name string) ([]byte, error) {
29+
return fileFromDir(path.Join("gitignore", path.Clean("/"+name)))
30+
}
31+
32+
// License reads the content of a specific license from static/bindata or custom path.
33+
func License(name string) ([]byte, error) {
34+
return fileFromDir(path.Join("license", path.Clean("/"+name)))
35+
}
36+
37+
// Labels reads the content of a specific labels from static/bindata or custom path.
38+
func Labels(name string) ([]byte, error) {
39+
return fileFromDir(path.Join("label", path.Clean("/"+name)))
40+
}
41+
42+
// WalkLocales reads the content of a specific locale
43+
func WalkLocales(callback func(path, name string, d fs.DirEntry, err error) error) error {
44+
if IsDynamic() {
45+
if err := walkAssetDir(filepath.Join(setting.StaticRootPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) {
46+
return fmt.Errorf("failed to walk locales. Error: %w", err)
47+
}
48+
}
49+
50+
if err := walkAssetDir(filepath.Join(setting.CustomPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) {
51+
return fmt.Errorf("failed to walk locales. Error: %w", err)
52+
}
53+
return nil
54+
}
55+
1556
func walkAssetDir(root string, callback func(path, name string, d fs.DirEntry, err error) error) error {
1657
if err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
1758
// name is the path relative to the root
@@ -37,3 +78,18 @@ func walkAssetDir(root string, callback func(path, name string, d fs.DirEntry, e
3778
}
3879
return nil
3980
}
81+
82+
func statDirIfExist(dir string) ([]string, error) {
83+
isDir, err := util.IsDir(dir)
84+
if err != nil {
85+
return nil, fmt.Errorf("unable to check if static directory %s is a directory. %w", dir, err)
86+
}
87+
if !isDir {
88+
return nil, nil
89+
}
90+
files, err := util.StatDir(dir, true)
91+
if err != nil {
92+
return nil, fmt.Errorf("unable to read directory %q. %w", dir, err)
93+
}
94+
return files, nil
95+
}

modules/options/dynamic.go

+6-64
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ package options
77

88
import (
99
"fmt"
10-
"io/fs"
1110
"os"
1211
"path"
13-
"path/filepath"
1412

1513
"code.gitea.io/gitea/modules/log"
1614
"code.gitea.io/gitea/modules/setting"
@@ -27,76 +25,20 @@ func Dir(name string) ([]string, error) {
2725

2826
var result []string
2927

30-
customDir := path.Join(setting.CustomPath, "options", name)
31-
32-
isDir, err := util.IsDir(customDir)
33-
if err != nil {
34-
return []string{}, fmt.Errorf("Unabe to check if custom directory %s is a directory. %w", customDir, err)
35-
}
36-
if isDir {
37-
files, err := util.StatDir(customDir, true)
38-
if err != nil {
39-
return []string{}, fmt.Errorf("Failed to read custom directory. %w", err)
40-
}
41-
42-
result = append(result, files...)
43-
}
44-
45-
staticDir := path.Join(setting.StaticRootPath, "options", name)
46-
47-
isDir, err = util.IsDir(staticDir)
48-
if err != nil {
49-
return []string{}, fmt.Errorf("unable to check if static directory %s is a directory. %w", staticDir, err)
50-
}
51-
if isDir {
52-
files, err := util.StatDir(staticDir, true)
28+
for _, dir := range []string{
29+
path.Join(setting.CustomPath, "options", name), // custom dir
30+
path.Join(setting.StaticRootPath, "options", name), // static dir
31+
} {
32+
files, err := statDirIfExist(dir)
5333
if err != nil {
54-
return []string{}, fmt.Errorf("Failed to read static directory. %w", err)
34+
return nil, err
5535
}
56-
5736
result = append(result, files...)
5837
}
5938

6039
return directories.AddAndGet(name, result), nil
6140
}
6241

63-
// Locale reads the content of a specific locale from static or custom path.
64-
func Locale(name string) ([]byte, error) {
65-
return fileFromDir(path.Join("locale", name))
66-
}
67-
68-
// WalkLocales reads the content of a specific locale from static or custom path.
69-
func WalkLocales(callback func(path, name string, d fs.DirEntry, err error) error) error {
70-
if err := walkAssetDir(filepath.Join(setting.StaticRootPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) {
71-
return fmt.Errorf("failed to walk locales. Error: %w", err)
72-
}
73-
74-
if err := walkAssetDir(filepath.Join(setting.CustomPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) {
75-
return fmt.Errorf("failed to walk locales. Error: %w", err)
76-
}
77-
return nil
78-
}
79-
80-
// Readme reads the content of a specific readme from static or custom path.
81-
func Readme(name string) ([]byte, error) {
82-
return fileFromDir(path.Join("readme", path.Clean("/"+name)))
83-
}
84-
85-
// Gitignore reads the content of a specific gitignore from static or custom path.
86-
func Gitignore(name string) ([]byte, error) {
87-
return fileFromDir(path.Join("gitignore", path.Clean("/"+name)))
88-
}
89-
90-
// License reads the content of a specific license from static or custom path.
91-
func License(name string) ([]byte, error) {
92-
return fileFromDir(path.Join("license", path.Clean("/"+name)))
93-
}
94-
95-
// Labels reads the content of a specific labels from static or custom path.
96-
func Labels(name string) ([]byte, error) {
97-
return fileFromDir(path.Join("label", path.Clean("/"+name)))
98-
}
99-
10042
// fileFromDir is a helper to read files from static or custom path.
10143
func fileFromDir(name string) ([]byte, error) {
10244
customPath := path.Join(setting.CustomPath, "options", name)

modules/options/repo.go

-44
This file was deleted.

modules/options/static.go

+6-44
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ package options
88
import (
99
"fmt"
1010
"io"
11-
"io/fs"
1211
"os"
1312
"path"
14-
"path/filepath"
1513

1614
"code.gitea.io/gitea/modules/log"
1715
"code.gitea.io/gitea/modules/setting"
@@ -28,17 +26,14 @@ func Dir(name string) ([]string, error) {
2826

2927
var result []string
3028

31-
customDir := path.Join(setting.CustomPath, "options", name)
32-
isDir, err := util.IsDir(customDir)
33-
if err != nil {
34-
return []string{}, fmt.Errorf("unable to check if custom directory %q is a directory. %w", customDir, err)
35-
}
36-
if isDir {
37-
files, err := util.StatDir(customDir, true)
29+
for _, dir := range []string{
30+
path.Join(setting.CustomPath, "options", name), // custom dir
31+
// no static dir
32+
} {
33+
files, err := statDirIfExist(dir)
3834
if err != nil {
39-
return []string{}, fmt.Errorf("unable to read custom directory %q. %w", customDir, err)
35+
return nil, err
4036
}
41-
4237
result = append(result, files...)
4338
}
4439

@@ -69,39 +64,6 @@ func AssetDir(dirName string) ([]string, error) {
6964
return results, nil
7065
}
7166

72-
// Locale reads the content of a specific locale from bindata or custom path.
73-
func Locale(name string) ([]byte, error) {
74-
return fileFromDir(path.Join("locale", name))
75-
}
76-
77-
// WalkLocales reads the content of a specific locale from static or custom path.
78-
func WalkLocales(callback func(path, name string, d fs.DirEntry, err error) error) error {
79-
if err := walkAssetDir(filepath.Join(setting.CustomPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) {
80-
return fmt.Errorf("failed to walk locales. Error: %w", err)
81-
}
82-
return nil
83-
}
84-
85-
// Readme reads the content of a specific readme from bindata or custom path.
86-
func Readme(name string) ([]byte, error) {
87-
return fileFromDir(path.Join("readme", path.Clean("/"+name)))
88-
}
89-
90-
// Gitignore reads the content of a gitignore locale from bindata or custom path.
91-
func Gitignore(name string) ([]byte, error) {
92-
return fileFromDir(path.Join("gitignore", path.Clean("/"+name)))
93-
}
94-
95-
// License reads the content of a specific license from bindata or custom path.
96-
func License(name string) ([]byte, error) {
97-
return fileFromDir(path.Join("license", path.Clean("/"+name)))
98-
}
99-
100-
// Labels reads the content of a specific labels from static or custom path.
101-
func Labels(name string) ([]byte, error) {
102-
return fileFromDir(path.Join("label", path.Clean("/"+name)))
103-
}
104-
10567
// fileFromDir is a helper to read files from bindata or custom path.
10668
func fileFromDir(name string) ([]byte, error) {
10769
customPath := path.Join(setting.CustomPath, "options", name)

modules/repository/init.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir,
136136
}
137137

138138
// README
139-
data, err := options.GetRepoInitFile("readme", opts.Readme)
139+
data, err := options.Readme(opts.Readme)
140140
if err != nil {
141141
return fmt.Errorf("GetRepoInitFile[%s]: %w", opts.Readme, err)
142142
}
@@ -164,7 +164,7 @@ func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir,
164164
var buf bytes.Buffer
165165
names := strings.Split(opts.Gitignores, ",")
166166
for _, name := range names {
167-
data, err = options.GetRepoInitFile("gitignore", name)
167+
data, err = options.Gitignore(name)
168168
if err != nil {
169169
return fmt.Errorf("GetRepoInitFile[%s]: %w", name, err)
170170
}
@@ -182,7 +182,7 @@ func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir,
182182

183183
// LICENSE
184184
if len(opts.License) > 0 {
185-
data, err = options.GetRepoInitFile("license", opts.License)
185+
data, err = options.License(opts.License)
186186
if err != nil {
187187
return fmt.Errorf("GetRepoInitFile[%s]: %w", opts.License, err)
188188
}

0 commit comments

Comments
 (0)