grim/hgkeeper

Refactor refresh of repositories

12 months ago, aklitzing
1af17fe86235
Parents c10e152f5523
Children ebf8e3bd9e68
Refactor refresh of repositories

Since we use this for other stuff we should split it out from hgweb and
re-use the repository slice for hgweb.

Reviewed at https://reviews.imfreedom.org/r/2434/
--- a/access/access.go Wed Apr 12 23:21:11 2023 -0500
+++ b/access/access.go Wed Apr 12 23:40:09 2023 -0500
@@ -23,6 +23,8 @@
adminRepoName string
hgwebConfigPath string
+
+ repositories map[string]string
)
func Setup(repositoriesPath, adminRepo string) error {
@@ -80,6 +82,10 @@
return err
}
+ if err := refreshRepositories(); err != nil {
+ return err
+ }
+
if err := refreshHgWeb(); err != nil {
return err
}
--- a/access/hgweb.go Wed Apr 12 23:21:11 2023 -0500
+++ b/access/hgweb.go Wed Apr 12 23:40:09 2023 -0500
@@ -3,9 +3,6 @@
import (
"fmt"
"os"
- "path"
- "path/filepath"
- "strings"
)
func refreshHgWeb() error {
@@ -17,37 +14,13 @@
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.WalkDir(absReposPath, func(filename string, entry os.DirEntry, err error) error {
- // check if we're looking at a directory
- if !entry.IsDir() {
- return nil
+ for relativeRepoPath, repoPath := range repositories {
+ rootedPath := "/" + relativeRepoPath
+ // check if it's publicly readable
+ if CanRead("public", rootedPath) {
+ fmt.Fprintf(fp, "%s = %s\n", rootedPath, repoPath)
}
-
- // 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)
-
- // check if it's publicly readable
- if CanRead("public", relativeRepoPath) {
- fmt.Fprintf(fp, "%s = %s\n", relativeRepoPath, repoPath)
- }
-
- return filepath.SkipDir
- })
+ }
return nil
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/access/repositories.go Wed Apr 12 23:40:09 2023 -0500
@@ -0,0 +1,42 @@
+package access
+
+import (
+ "os"
+ "path"
+ "path/filepath"
+ "strings"
+)
+
+func refreshRepositories() error {
+ absReposPath, err := filepath.Abs(ReposPath())
+ 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
+}