grim/hgkeeper

Add a caching layer to the hgweb portion. This should take some strain off of mercurial anf our cpu quota
package access
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
)
func refreshHgWeb(reposPath, adminRepoPath string) error {
fp, err := os.OpenFile(hgwebConfigPath, os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer fp.Close()
absAdminRepoPath, err := filepath.Abs(adminRepoPath)
if err != nil {
return err
}
fmt.Fprintf(fp, "%%include %s\n\n", filepath.Join(absAdminRepoPath, "site.hgrc"))
fmt.Fprintf(fp, "[paths]\n")
absReposPath, err := filepath.Abs(reposPath)
if err != nil {
return err
}
// 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.Walk(absReposPath, func(filename string, info os.FileInfo, err error) error {
// check if we're looking at a directory
if !info.IsDir() {
return nil
}
// check if it is a .hg directory
if !strings.HasSuffix(info.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)
// check if it's publicly readable
if CanRead("public", relativeRepoPath) {
fmt.Fprintf(fp, "%s = %s\n", relativeRepoPath, repoPath)
}
return filepath.SkipDir
})
return nil
}