Skip to content

Commit 9783a19

Browse files
committed
define extension priority
1 parent 9a3947c commit 9783a19

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

modules/repository/init.go

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ var (
4747
)
4848

4949
type optionFileList struct {
50-
builtin []string
51-
custom []string
50+
all []string
51+
custom []string
5252
}
5353

54-
// mergeBuiltinCustomFiles merges the builtin and custom files, de-duplicates and returns a sorted list of files
55-
func mergeBuiltinCustomFiles(fl optionFileList) []string {
56-
files := fl.builtin
54+
// mergeCustomFiles merges the custom files, de-duplicates and returns a sorted list of files
55+
func mergeCustomFiles(fl optionFileList) []string {
56+
files := fl.all
5757
for _, f := range fl.custom {
5858
// for most cases, the SliceContainsString is good enough because there are only a few custom files
5959
if !util.SliceContainsString(files, f) {
@@ -64,12 +64,24 @@ func mergeBuiltinCustomFiles(fl optionFileList) []string {
6464
return files
6565
}
6666

67-
// mergeBuiltinCustomLabels merges the builtin and custom files. Always use the file's main name as the key to de-duplicate.
68-
func mergeBuiltinCustomLabels(fl optionFileList) []string {
69-
files := fl.builtin
67+
// mergeCustomLabels merges the custom label files. Always use the file's main name as the key to de-duplicate.
68+
func mergeCustomLabels(fl optionFileList) []string {
69+
exts := map[string]int{
70+
"": 0,
71+
".yml": 1,
72+
".yaml": 2,
73+
}
74+
sort.Slice(fl.all, func(i, j int) bool {
75+
return exts[filepath.Ext(fl.all[i])] < exts[filepath.Ext(fl.all[j])]
76+
})
77+
sort.Slice(fl.custom, func(i, j int) bool {
78+
return exts[filepath.Ext(fl.custom[i])] < exts[filepath.Ext(fl.custom[j])]
79+
})
80+
81+
files := fl.all
7082
if len(fl.custom) > 0 {
7183
m := map[string]string{}
72-
for _, f := range fl.builtin {
84+
for _, f := range fl.all {
7385
m[strings.TrimSuffix(f, filepath.Ext(f))] = f
7486
}
7587
for _, f := range fl.custom {
@@ -92,7 +104,7 @@ func LoadRepoConfig() error {
92104

93105
for i, t := range types {
94106
var err error
95-
if optionTypeFiles[i].builtin, err = options.Dir(t); err != nil {
107+
if optionTypeFiles[i].all, err = options.Dir(t); err != nil {
96108
return fmt.Errorf("failed to list %s files: %w", t, err)
97109
}
98110
customPath := filepath.Join(setting.CustomPath, "options", t)
@@ -106,10 +118,10 @@ func LoadRepoConfig() error {
106118
}
107119
}
108120

109-
Gitignores = mergeBuiltinCustomFiles(optionTypeFiles[0])
110-
Licenses = mergeBuiltinCustomFiles(optionTypeFiles[1])
111-
Readmes = mergeBuiltinCustomFiles(optionTypeFiles[2])
112-
labelTemplatesFiles := mergeBuiltinCustomLabels(optionTypeFiles[3])
121+
Gitignores = mergeCustomFiles(optionTypeFiles[0])
122+
Licenses = mergeCustomFiles(optionTypeFiles[1])
123+
Readmes = mergeCustomFiles(optionTypeFiles[2])
124+
labelTemplatesFiles := mergeCustomLabels(optionTypeFiles[3])
113125

114126
// Load label templates
115127
for _, templateFile := range labelTemplatesFiles {

0 commit comments

Comments
 (0)