mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 16:40:24 +08:00 
			
		
		
		
	Simplify IsVendor (#19626)
				
					
				
			The changes in this file were upstreamed directly into go-enry as https://github.com/go-enry/go-enry/pull/44 and therefore they are no longer needed.
This commit is contained in:
		@@ -5,66 +5,10 @@
 | 
				
			|||||||
package analyze
 | 
					package analyze
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"regexp"
 | 
						"github.com/go-enry/go-enry/v2"
 | 
				
			||||||
	"sort"
 | 
					 | 
				
			||||||
	"strings"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	"github.com/go-enry/go-enry/v2/data"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var isVendorRegExp *regexp.Regexp
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func init() {
 | 
					 | 
				
			||||||
	matchers := data.VendorMatchers
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	caretStrings := make([]string, 0, 10)
 | 
					 | 
				
			||||||
	caretShareStrings := make([]string, 0, 10)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	matcherStrings := make([]string, 0, len(matchers))
 | 
					 | 
				
			||||||
	for _, matcher := range matchers {
 | 
					 | 
				
			||||||
		str := matcher.String()
 | 
					 | 
				
			||||||
		if str[0] == '^' {
 | 
					 | 
				
			||||||
			caretStrings = append(caretStrings, str[1:])
 | 
					 | 
				
			||||||
		} else if str[0:5] == "(^|/)" {
 | 
					 | 
				
			||||||
			caretShareStrings = append(caretShareStrings, str[5:])
 | 
					 | 
				
			||||||
		} else {
 | 
					 | 
				
			||||||
			matcherStrings = append(matcherStrings, str)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	sort.Strings(caretShareStrings)
 | 
					 | 
				
			||||||
	sort.Strings(caretStrings)
 | 
					 | 
				
			||||||
	sort.Strings(matcherStrings)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	sb := &strings.Builder{}
 | 
					 | 
				
			||||||
	sb.WriteString("(?:^(?:")
 | 
					 | 
				
			||||||
	sb.WriteString(caretStrings[0])
 | 
					 | 
				
			||||||
	for _, matcher := range caretStrings[1:] {
 | 
					 | 
				
			||||||
		sb.WriteString(")|(?:")
 | 
					 | 
				
			||||||
		sb.WriteString(matcher)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	sb.WriteString("))")
 | 
					 | 
				
			||||||
	sb.WriteString("|")
 | 
					 | 
				
			||||||
	sb.WriteString("(?:(?:^|/)(?:")
 | 
					 | 
				
			||||||
	sb.WriteString(caretShareStrings[0])
 | 
					 | 
				
			||||||
	for _, matcher := range caretShareStrings[1:] {
 | 
					 | 
				
			||||||
		sb.WriteString(")|(?:")
 | 
					 | 
				
			||||||
		sb.WriteString(matcher)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	sb.WriteString("))")
 | 
					 | 
				
			||||||
	sb.WriteString("|")
 | 
					 | 
				
			||||||
	sb.WriteString("(?:")
 | 
					 | 
				
			||||||
	sb.WriteString(matcherStrings[0])
 | 
					 | 
				
			||||||
	for _, matcher := range matcherStrings[1:] {
 | 
					 | 
				
			||||||
		sb.WriteString(")|(?:")
 | 
					 | 
				
			||||||
		sb.WriteString(matcher)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	sb.WriteString(")")
 | 
					 | 
				
			||||||
	combined := sb.String()
 | 
					 | 
				
			||||||
	isVendorRegExp = regexp.MustCompile(combined)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// IsVendor returns whether or not path is a vendor path.
 | 
					// IsVendor returns whether or not path is a vendor path.
 | 
				
			||||||
func IsVendor(path string) bool {
 | 
					func IsVendor(path string) bool {
 | 
				
			||||||
	return isVendorRegExp.MatchString(path)
 | 
						return enry.IsVendor(path)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user