Skip to content

Commit 9a3947c

Browse files
committed
de-duplicate Label sets
1 parent 5cb8967 commit 9a3947c

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
lines changed

modules/repository/init.go

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,41 +46,70 @@ var (
4646
LabelTemplateFiles []OptionFile
4747
)
4848

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+
4987
// LoadRepoConfig loads the repository config
5088
func LoadRepoConfig() error {
5189
// Load .gitignore and license files and readme templates.
5290
types := []string{"gitignore", "license", "readme", "label"}
53-
typeFiles := make([][]string, 4)
91+
optionTypeFiles := make([]optionFileList, len(types))
92+
5493
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 {
5796
return fmt.Errorf("failed to list %s files: %w", t, err)
5897
}
5998
customPath := filepath.Join(setting.CustomPath, "options", t)
6099
if isDir, err := util.IsDir(customPath); err != nil {
61100
return fmt.Errorf("failed to check custom %s dir: %w", t, err)
62101
} else if isDir {
63-
customFiles, err := util.StatDir(customPath)
102+
optionTypeFiles[i].custom, err = util.StatDir(customPath)
64103
if err != nil {
65104
return fmt.Errorf("failed to list custom %s files: %w", t, err)
66105
}
67-
for _, f := range customFiles {
68-
if !util.SliceContainsString(files, f) {
69-
files = append(files, f)
70-
}
71-
}
72106
}
73-
typeFiles[i] = files
74107
}
75108

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])
84113

85114
// Load label templates
86115
for _, templateFile := range labelTemplatesFiles {

0 commit comments

Comments
 (0)