@@ -46,41 +46,70 @@ var (
46
46
LabelTemplateFiles []OptionFile
47
47
)
48
48
49
+ type optionFileList struct {
50
+ builtin []string
51
+ custom []string
52
+ }
53
+
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
57
+ for _ , f := range fl .custom {
58
+ // for most cases, the SliceContainsString is good enough because there are only a few custom files
59
+ if ! util .SliceContainsString (files , f ) {
60
+ files = append (files , f )
61
+ }
62
+ }
63
+ sort .Strings (files )
64
+ return files
65
+ }
66
+
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
70
+ if len (fl .custom ) > 0 {
71
+ m := map [string ]string {}
72
+ for _ , f := range fl .builtin {
73
+ m [strings .TrimSuffix (f , filepath .Ext (f ))] = f
74
+ }
75
+ for _ , f := range fl .custom {
76
+ m [strings .TrimSuffix (f , filepath .Ext (f ))] = f
77
+ }
78
+ files = make ([]string , 0 , len (m ))
79
+ for _ , f := range m {
80
+ files = append (files , f )
81
+ }
82
+ }
83
+ sort .Strings (files )
84
+ return files
85
+ }
86
+
49
87
// LoadRepoConfig loads the repository config
50
88
func LoadRepoConfig () error {
51
89
// Load .gitignore and license files and readme templates.
52
90
types := []string {"gitignore" , "license" , "readme" , "label" }
53
- typeFiles := make ([][]string , 4 )
91
+ optionTypeFiles := make ([]optionFileList , len (types ))
92
+
54
93
for i , t := range types {
55
- files , err := options . Dir ( t )
56
- if err != nil {
94
+ var err error
95
+ if optionTypeFiles [ i ]. builtin , err = options . Dir ( t ); err != nil {
57
96
return fmt .Errorf ("failed to list %s files: %w" , t , err )
58
97
}
59
98
customPath := filepath .Join (setting .CustomPath , "options" , t )
60
99
if isDir , err := util .IsDir (customPath ); err != nil {
61
100
return fmt .Errorf ("failed to check custom %s dir: %w" , t , err )
62
101
} else if isDir {
63
- customFiles , err : = util .StatDir (customPath )
102
+ optionTypeFiles [ i ]. custom , err = util .StatDir (customPath )
64
103
if err != nil {
65
104
return fmt .Errorf ("failed to list custom %s files: %w" , t , err )
66
105
}
67
- for _ , f := range customFiles {
68
- if ! util .SliceContainsString (files , f ) {
69
- files = append (files , f )
70
- }
71
- }
72
106
}
73
- typeFiles [i ] = files
74
107
}
75
108
76
- Gitignores = typeFiles [0 ]
77
- Licenses = typeFiles [1 ]
78
- Readmes = typeFiles [2 ]
79
- labelTemplatesFiles := typeFiles [3 ]
80
- sort .Strings (Gitignores )
81
- sort .Strings (Licenses )
82
- sort .Strings (Readmes )
83
- sort .Strings (labelTemplatesFiles )
109
+ Gitignores = mergeBuiltinCustomFiles (optionTypeFiles [0 ])
110
+ Licenses = mergeBuiltinCustomFiles (optionTypeFiles [1 ])
111
+ Readmes = mergeBuiltinCustomFiles (optionTypeFiles [2 ])
112
+ labelTemplatesFiles := mergeBuiltinCustomLabels (optionTypeFiles [3 ])
84
113
85
114
// Load label templates
86
115
for _ , templateFile := range labelTemplatesFiles {
0 commit comments