grim/hgkeeper

Use Go 1.22 and update dependencies
default tip
2 months ago, aklitzing
f33f223bc8fe
Use Go 1.22 and update dependencies

Reviewed at https://reviews.imfreedom.org/r/2949/
package access
import (
"os"
"path"
"path/filepath"
"strings"
"go.uber.org/zap"
)
func NormalizeRepo(reposPath, repoName string) (string, string) {
absPath := filepath.Join(reposPath, filepath.Clean(repoName))
realRepoName := strings.Trim(strings.TrimPrefix(absPath, reposPath), "\\/")
if strings.HasPrefix(absPath, reposPath) && len(realRepoName) > 0 && !strings.Contains(repoName, "/../") {
return filepath.Join(reposPath, realRepoName), realRepoName
}
zap.S().Infof("repository invalid: %q", repoName)
return "", ""
}
func IsExistingRepo(repo string) bool {
_, ok := repositories[repo]
return ok
}
func IsInExistingRepo(repo string) bool {
for existingRepo := range repositories {
existingRepo += "/"
if strings.HasPrefix(repo, existingRepo) || strings.HasPrefix(existingRepo, repo) {
return true
}
}
return false
}
func refreshRepositories() error {
absReposPath, err := filepath.Abs(ReposPath())
if err != nil {
return err
}
absReposPath, err = filepath.EvalSymlinks(absReposPath)
if err != nil {
return err
}
repositories = make(map[string]string)
// walk the reposPath, looking for .hg directories, when one is found,
// check if it is publicly readable, and if so, add it to the config file.
filepath.WalkDir(absReposPath, func(filename string, entry os.DirEntry, err error) error {
// check if we're looking at a directory
if !entry.IsDir() {
return nil
}
// check if it is a .hg directory
if !strings.HasSuffix(entry.Name(), ".hg") {
return nil
}
// figure out the repo path that we will be checking against the casbin
// stuff. That means it needs to be the exact path of the repo and not
// include the parent directory.
repoPath := path.Dir(filename)
relativeRepoPath := strings.TrimPrefix(repoPath, absReposPath)
repositories[strings.Trim(relativeRepoPath, "\\/")] = repoPath
return filepath.SkipDir
})
return nil
}