Monorepo for Tangled — https://tangled.org

reporesolver: compile path regexps at package init

Move blobPattern, treePattern, and pathAfterRefRE to package-level
vars so they are compiled once and reused across GetRepoInfo and
path resolution calls instead of recompiling on every request.

Signed-off-by: Matías Insaurralde <matias@insaurral.de>

authored by matias.tngl.sh and committed by tangled.org 6b3ca476 e326cc53

+9 -9
+9 -9
appview/reporesolver/resolver.go
··· 18 18 "tangled.org/core/rbac" 19 19 ) 20 20 21 + var ( 22 + blobPattern = regexp.MustCompile(`blob/[^/]+/(.*)$`) 23 + treePattern = regexp.MustCompile(`tree/[^/]+/(.*)$`) 24 + pathAfterRefRE = regexp.MustCompile(`(?:blob|tree|raw)/[^/]+/(.*)$`) 25 + ) 26 + 21 27 type RepoResolver struct { 22 28 config *config.Config 23 29 enforcer *rbac.Enforcer ··· 140 146 func extractCurrentDir(fullPath string) string { 141 147 fullPath = strings.TrimPrefix(fullPath, "/") 142 148 143 - blobPattern := regexp.MustCompile(`blob/[^/]+/(.*)$`) 144 149 if matches := blobPattern.FindStringSubmatch(fullPath); len(matches) > 1 { 145 150 return path.Dir(matches[1]) 146 151 } 147 152 148 - treePattern := regexp.MustCompile(`tree/[^/]+/(.*)$`) 149 153 if matches := treePattern.FindStringSubmatch(fullPath); len(matches) > 1 { 150 154 dir := strings.TrimSuffix(matches[1], "/") 151 155 if dir == "" { ··· 164 168 func extractPathAfterRef(fullPath string) string { 165 169 fullPath = strings.TrimPrefix(fullPath, "/") 166 170 167 - // match blob/, tree/, or raw/ followed by any ref and then a slash 168 - // 169 - // captures everything after the final slash 170 - pattern := `(?:blob|tree|raw)/[^/]+/(.*)$` 171 - 172 - re := regexp.MustCompile(pattern) 173 - matches := re.FindStringSubmatch(fullPath) 171 + // pathAfterRefRE matches blob/, tree/, or raw/ followed by any ref and then a slash; 172 + // it captures everything after the final slash. 173 + matches := pathAfterRefRE.FindStringSubmatch(fullPath) 174 174 175 175 if len(matches) > 1 { 176 176 return matches[1]