From c4b3671a5335c3526b39f97c5ec4863d5ed8104b Mon Sep 17 00:00:00 2001 From: Catherine Date: Wed, 5 Nov 2025 23:00:29 +0000 Subject: [PATCH] Add `[[wildcard]].index-repo-branch` option (`pages` by default). --- conf/config.example.toml | 1 + src/auth.go | 8 ++++---- src/config.go | 7 +++++++ src/wildcard.go | 3 +++ 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/conf/config.example.toml b/conf/config.example.toml index 971eaca..379e1d9 100644 --- a/conf/config.example.toml +++ b/conf/config.example.toml @@ -13,6 +13,7 @@ metrics = "tcp/:3002" domain = "codeberg.page" clone-url = "https://codeberg.org//.git" index-repos = [".codeberg.page", "pages"] +index-repo-branch = "main" fallback-proxy-to = "https://codeberg.page" [storage] diff --git a/src/auth.go b/src/auth.go index 3dfe723..0890d3c 100644 --- a/src/auth.go +++ b/src/auth.go @@ -229,6 +229,7 @@ func authorizeWildcardMatchSite(r *http.Request, pattern *WildcardPattern) (*Aut if userName, found := pattern.Matches(host); found { var repoURLs []string + var branch string repoURLTemplate := pattern.CloneURL if projectName == ".index" { for _, indexRepoTemplate := range pattern.IndexRepos { @@ -238,16 +239,15 @@ func authorizeWildcardMatchSite(r *http.Request, pattern *WildcardPattern) (*Aut "project": indexRepo, })) } + branch = pattern.IndexBranch } else { repoURLs = append(repoURLs, repoURLTemplate.ExecuteString(map[string]any{ "user": userName, "project": projectName, })) + branch = "pages" } - return &Authorization{ - repoURLs: repoURLs, - branch: "pages", - }, nil + return &Authorization{repoURLs, branch}, nil } else { return nil, AuthError{ http.StatusUnauthorized, diff --git a/src/config.go b/src/config.go index e671efe..8eedd9e 100644 --- a/src/config.go +++ b/src/config.go @@ -55,6 +55,7 @@ type WildcardConfig struct { Domain string `toml:"domain"` CloneURL string `toml:"clone-url"` IndexRepos []string `toml:"index-repos" default:"[]"` + IndexRepoBranch string `toml:"index-repo-branch" default:"pages"` FallbackProxyTo string `toml:"fallback-proxy-to"` FallbackInsecure bool `toml:"fallback-insecure"` } @@ -274,5 +275,11 @@ func Configure(tomlPath string) (config *Config, err error) { return nil }) + // defaults for wildcards aren't set by `defaults.MustSet` call above because the structs + // for them haven't been created yet + for i := range config.Wildcard { + defaults.MustSet(&config.Wildcard[i]) + } + return } diff --git a/src/wildcard.go b/src/wildcard.go index 8cac69f..64033b0 100644 --- a/src/wildcard.go +++ b/src/wildcard.go @@ -17,6 +17,7 @@ type WildcardPattern struct { Domain []string CloneURL *fasttemplate.Template IndexRepos []*fasttemplate.Template + IndexBranch string FallbackURL *url.URL Fallback http.Handler } @@ -71,6 +72,7 @@ func ConfigureWildcards(configs []WildcardConfig) error { } var indexRepoTemplates []*fasttemplate.Template + var indexRepoBranch string = config.IndexRepoBranch for _, indexRepo := range config.IndexRepos { indexRepoTemplate, err := fasttemplate.NewTemplate(indexRepo, "<", ">") if err != nil { @@ -105,6 +107,7 @@ func ConfigureWildcards(configs []WildcardConfig) error { Domain: strings.Split(config.Domain, "."), CloneURL: cloneURLTemplate, IndexRepos: indexRepoTemplates, + IndexBranch: indexRepoBranch, FallbackURL: fallbackURL, Fallback: fallback, })