repo/repo.go
func MergeRules(genRules []*rule.Rule, existingRules map[*rule.File][]string, destFile *rule.File, kinds map[string]rule.KindInfo) []*rule.File {
	sort.Stable(byRuleName(genRules))

	repoMap := make(map[string]*rule.File)
	for file, repoNames := range existingRules {
		if file.Path == destFile.Path && file.MacroName() != "" && file.MacroName() == destFile.MacroName() {
			file = destFile
		}
		for _, name := range repoNames {
			repoMap[name] = file
		}
	}

	rulesByFile := make(map[*rule.File][]*rule.Rule)
	for _, rule := range genRules {
		dest := destFile
		if file, ok := repoMap[rule.Name()]; ok {
			dest = file
		}
		rulesByFile[dest] = append(rulesByFile[dest], rule)
	}

	updatedFiles := make(map[string]*rule.File)
	for f, rules := range rulesByFile {
		merger.MergeFile(f, nil, rules, merger.PreResolve, kinds)
		f.Sync()
		if uf, ok := updatedFiles[f.Path]; ok {
			uf.SyncMacroFile(f)
		} else {
			updatedFiles[f.Path] = f
		}
	}
	files := make([]*rule.File, 0, len(updatedFiles))
	for _, f := range updatedFiles {
		files = append(files, f)
	}
	return files
}
func MergeRules(genRules []*rule.Rule, existingRules map[*rule.File][]string, destFile *rule.File, kinds map[string]rule.KindInfo) []*rule.File {
	sort.Stable(byRuleName(genRules))

	repoMap := make(map[string]*rule.File)
	for file, repoNames := range existingRules {
		if file.Path == destFile.Path && file.MacroName() != "" && file.MacroName() == destFile.MacroName() {
			file = destFile
		}
		for _, name := range repoNames {
			repoMap[name] = file
		}
	}

	rulesByFile := make(map[*rule.File][]*rule.Rule)
	for _, rule := range genRules {
		dest := destFile
		if file, ok := repoMap[rule.Name()]; ok {
			dest = file
		}
		rulesByFile[dest] = append(rulesByFile[dest], rule)
	}

	updatedFiles := make(map[string]*rule.File)
	for f, rules := range rulesByFile {
		merger.MergeFile(f, nil, rules, merger.PreResolve, kinds)
		f.Sync()
		if uf, ok := updatedFiles[f.Path]; ok {
			uf.SyncMacroFile(f)
		} else {
			updatedFiles[f.Path] = f
		}
	}
	files := make([]*rule.File, 0, len(updatedFiles))
	for _, f := range updatedFiles {
		files = append(files, f)
	}
	return files
}
// MergeRules merges a list of generated repo rules with the already defined repo rules,
// and then updates each rule's underlying file. If the generated rule matches an existing
// one, then it inherits the file where the existing rule was defined. If the rule is new then
// its file is set as the destFile parameter. A list of the updated files is returned.